import { useAuth } from "../../contexts/authContext"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import UserService from "../../services/userService"; import { User } from "../../models/user"; import { GetServerSidePropsContext } from "next"; import firebaseAdmin from "firebase-admin"; import { FaPeopleCarry, FaArrowRight, FaBullhorn } from "react-icons/fa"; import BackgroundLayout from "../../components/Layouts/BackgroundLayout"; import TeamService from "../../services/teamService"; import Loading from "../../components/Loading"; import { TeamMemberStatus } from "../../models/teamMember"; /** * figure out where to take the user based on everything */ const userService: UserService = new UserService(); const teamService: TeamService = new TeamService(); function RouteHandler() { const { currUser } = useAuth(); const router = useRouter(); const [loading, setLoading] = useState(true); const [user, setUser] = useState(null); useEffect(() => { (async function () { try { // if not authenticated, take user to the login if (!currUser) { console.log( "not authenticated...routing from dashboard to teams home" ); router.push("/teams/login"); return; } // get user const returnedUser: User | null = await userService.getUser( currUser.uid ); // if user has no profile, then go to create profile if ( !returnedUser || !returnedUser.firstName || !returnedUser.lastName || !returnedUser.nickName ) { console.log("no profile for the user, routing him/her there"); router.push("/teams/profile"); return; } setUser(returnedUser); // check if user is in a team go to the team dashboard const returnedTeamMembers = await teamService.getTeamMembersByUserId( currUser.uid ); // if the user is deleted from a team, don't let them go there var teamId = ""; returnedTeamMembers.map((tm) => { if (tm.status != TeamMemberStatus.deleted) { console.log( "in a team! going to the team dashboard of the first one!" ); teamId = returnedTeamMembers[0].teamId; } }); if (teamId) { router.push("/teams/" + teamId); return; } // last chance, if they are invited to a team, check for it const invitedTeamMember = await teamService.getTeamMembersByEmailInvite( currUser.email ); if (invitedTeamMember.length > 0) { router.push("/teams/" + invitedTeamMember[0].teamId); return; } setLoading(false); } catch (error) { console.log(error); router.push("/teams/login"); } })(); }, [currUser]); if (loading || !user) { return ; } // not in a team yet, and have not created a team yet return (
{/* header */}
πŸ‘‹Hey, {user.firstName}
Let's get you started.
{/* create team hover thing */} {/* tell your manager to add your email */} or learn more about{" "}
); } export async function getServerSideProps(context: GetServerSidePropsContext) { // const cookies = nookies.get(context); // var user: User | null = null // if (cookies.token) { // try { // const headers: HeadersInit = { // 'Content-Type': 'application/json', // Authorization: JSON.stringify({ token: cookies.token }) // }; // const result = await unfetch('/api/auth/validateToken', { headers }); // console.log(result) // // get user data // const userService: UserService = new UserService() // // the user is authenticated! // // FETCH STUFF HERE!! πŸš€ // // user = await userService.getUser("asdf") // } catch (e) { // // let exceptions fail silently // // could be invalid token, just let client-side deal with that // console.log(e) // } // } // // Pass data to the page via props // return { props: { // user // } // } return { props: {} }; } export default RouteHandler;