import { Divider } from "antd"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import User from "@nirvana/common/models/user"; import UserService from "@nirvana/common/services/userService"; import { useAuth } from "../../contexts/authContext"; import { UserStatus } from "../../../common/models/user"; export default function Profile() { const { currUser } = useAuth(); const [user, setUser] = useState(null); const [loading, setLoading] = useState(false); const router = useRouter(); const userService: UserService = new UserService(); 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 the user is null, then go ahead and create the user even though it's not there yet if (!returnedUser) { await userService.createUser( currUser.uid, currUser.email!, currUser.photoURL! ); } console.log("got user from the backend"); setUser(returnedUser); setFirstName( returnedUser?.firstName ?? currUser.displayName!.split(" ")[0] ); setLastName(returnedUser?.lastName ?? ""); } catch (error) { console.log(error); router.push("/teams/login"); } })(); }, []); async function handleSubmit(e) { setLoading(true); e.preventDefault(); if (!firstName) { setError("Must input first name"); return; } else if (!lastName) { setError("Must input last name"); return; } // create the user in the backend or just merge results const newUser = new User( user?.id, currUser!.email!, firstName, lastName, currUser!.photoURL!, UserStatus.online ); newUser.id = currUser!.uid; newUser.firstName = firstName; newUser.lastName = lastName; newUser.avatarUrl = currUser!.photoURL!; try { await userService.updateUser(newUser); window.open("/s", "_self"); } catch (error) { setError( "Something went wrong in saving your information. Please try again." ); } setLoading(false); } const [firstName, setFirstName] = useState( currUser!.displayName?.split(" ")[0] ?? "" ); const [lastName, setLastName] = useState(""); const [nickname, setNickname] = useState(""); const [teamRole, setTeamRole] = useState(""); const [error, setError] = useState(""); return (
{/* header */}
Profile
{error && {error}} Avatar* Please change your google image to change this. asdf Email* This is set from your Google account and cannot be changed.
First Name* setFirstName(e.target.value)} /> Last Name* setLastName(e.target.value)} />
{/* if they already had a nickname, they are probably revisiting this page */} {/* can't cancel if they are a new user */} {loading ? ( ) : ( <> {user && user.firstName ? ( <> ) : ( <> {/* if they already had a nickname, they are probably revisiting this page */} )} )} ); }