diff --git a/compass/components/RootLayout.tsx b/compass/components/RootLayout.tsx new file mode 100644 index 0000000..8b0307e --- /dev/null +++ b/compass/components/RootLayout.tsx @@ -0,0 +1,67 @@ +"use client"; + +import Sidebar from "@/components/Sidebar/Sidebar"; +import React, { useState, useEffect } from "react"; +import { createClient } from "@/utils/supabase/client"; +import { useRouter } from "next/navigation"; +import User, { Role } from "@/utils/models/User"; +import Loading from "@/components/auth/Loading"; + +export default function RootLayout({ + children, +}: { + children: React.ReactNode; +}) { + const [isSidebarOpen, setIsSidebarOpen] = useState(true); + const [user, setUser] = useState(null); // Initialize user as null + const router = useRouter(); + + useEffect(() => { + async function getUser() { + const supabase = createClient(); + + const { data, error } = await supabase.auth.getUser(); + + if (error || !data?.user) { + console.log("User not logged in or error fetching user"); + router.push("/auth/login"); + return; + } + + const userData = await fetch( + `${process.env.NEXT_PUBLIC_HOST}/api/user?uuid=${data.user.id}` + ); + + const user: User = await userData.json(); + setUser(user); // Set user data after fetching + } + + getUser(); + }, [router]); + + if (!user) { + return ; // Show loading screen while the user is being fetched + } + + return ( +
+ {/* Sidebar is shared across all pages */} + + + {/* Page content */} +
+ {children} {/* Render page-specific content here */} +
+
+ ); +} diff --git a/compass/components/auth/LoggingOut.tsx b/compass/components/auth/LoggingOut.tsx new file mode 100644 index 0000000..8915e23 --- /dev/null +++ b/compass/components/auth/LoggingOut.tsx @@ -0,0 +1,22 @@ +// components/LoggingOut.js +import styles from "./Loading.module.css"; +import Image from "next/image"; + +const LoggingOut = () => { + return ( +
+
+ Compass Center logo. +

Signing out...

+
+
+
+ ); +}; + +export default LoggingOut; diff --git a/compass/components/resource/UserProfile.tsx b/compass/components/resource/UserProfile.tsx index d2780a6..ee88eae 100644 --- a/compass/components/resource/UserProfile.tsx +++ b/compass/components/resource/UserProfile.tsx @@ -1,29 +1,49 @@ +import { useState } from "react"; import { signOut } from "@/app/auth/actions"; +import LoggingOut from "../auth/LoggingOut"; interface UserProfileProps { name: string; email: string; } -const handleClick = async (event: React.MouseEvent) => { +const handleClick = async ( + event: React.MouseEvent, + setLoading: React.Dispatch> +) => { + setLoading(true); // Set loading state to true + + // Call signOut and wait for the process to complete before redirecting await signOut(); + + // Once signOut is complete, the redirect should happen, but let's delay it to allow the loading state to be visible for a short time + setTimeout(() => { + // The signOut already handles the redirect, so we don't need to redirect here again. + setLoading(false); // Reset the loading state after the timeout (just in case) + }, 1000); // You can adjust this delay as needed }; export const UserProfile = ({ name, email }: UserProfileProps) => { + const [loading, setLoading] = useState(false); + + if (loading) { + // Show the "Logging out" screen while the sign-out process is in progress + return ; + } + return (
- - {name} - + {name} {email}
); -}; +}; \ No newline at end of file