|
| 1 | +"use client"; |
| 2 | +import { FC, useEffect, useState } from "react"; |
| 3 | +import { getCurrentUserInfo, PassageUserInfo } from "@/actions/getCurrentUserInfo"; |
| 4 | +import LogoutButton from "./LogoutButton"; |
| 5 | +import Link from "next/link"; |
| 6 | +import { format } from "date-fns"; |
| 7 | + |
| 8 | +interface DashboardContentProps {} |
| 9 | + |
| 10 | +const DashboardContent: FC<DashboardContentProps> = ({}) => { |
| 11 | + const [userInfo, setUserInfo] = useState<PassageUserInfo | undefined>( |
| 12 | + undefined |
| 13 | + ); |
| 14 | + const [isLoading, setIsLoading] = useState(true); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const fetchSessionInfo = async () => { |
| 18 | + const sessionInfo = await getCurrentUserInfo(); |
| 19 | + setUserInfo(sessionInfo.userInfo); |
| 20 | + setIsLoading(false); |
| 21 | + }; |
| 22 | + |
| 23 | + fetchSessionInfo(); |
| 24 | + }, []); |
| 25 | + |
| 26 | + if (isLoading) { |
| 27 | + // Render loading state if the session information is still being fetched |
| 28 | + return <div>Loading...</div>; |
| 29 | + } |
| 30 | + |
| 31 | + if (!userInfo) { |
| 32 | + // Render the message if the session doesn't exist |
| 33 | + return ( |
| 34 | + <main className="flex justify-center p-24 "> |
| 35 | + <div className="border flex justify-center border-black rounded-xl w-96"> |
| 36 | + <div className="p-10 pb-5"> |
| 37 | + <div className="font-bold text-2xl mb-5"> |
| 38 | + <h1>Unauthorized</h1> |
| 39 | + </div> |
| 40 | + <div className="break-normal"></div> |
| 41 | + <p> |
| 42 | + You have not logged in and cannot view the dashboard. |
| 43 | + <br /> |
| 44 | + <Link |
| 45 | + href="/" |
| 46 | + className="underline font-bold hover:text-blue-600 " |
| 47 | + > |
| 48 | + Login to continue. |
| 49 | + </Link> |
| 50 | + </p> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + </main> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const formattedCreatedAt = format( |
| 58 | + new Date(userInfo.created_at), |
| 59 | + "yyyy-MM-dd HH:mm:ss" |
| 60 | + ); |
| 61 | + |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <main className="flex justify-center p-24 "> |
| 65 | + <div className="border flex justify-center border-black rounded-xl w-96"> |
| 66 | + <div className="p-10 pb-5"> |
| 67 | + <div className="font-bold text-2xl mb-5"> |
| 68 | + <h1>Welcome</h1> |
| 69 | + </div> |
| 70 | + <div className="break-normal"></div> |
| 71 | + <p> |
| 72 | + You successfully signed in with Passage. |
| 73 | + <br /> |
| 74 | + Your username is: <b> {userInfo.email}</b> |
| 75 | + <br /> |
| 76 | + Account created at: |
| 77 | + <br /> <b> {formattedCreatedAt}</b> |
| 78 | + </p> |
| 79 | + <LogoutButton /> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </main> |
| 83 | + </> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +export default DashboardContent; |
0 commit comments