generated from CS3219-AY2324S1/course-assessment-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Link user profile settings to backend (firebase and supabase) (#210)
- Loading branch information
Showing
25 changed files
with
709 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getAuth, updateProfile } from "firebase/auth"; | ||
|
||
export const useUpdateProfile = () => { | ||
|
||
const updateUserProfile = async ({displayName, photoURL}: {displayName?: string, photoURL?: string}) => { | ||
const auth = getAuth(); | ||
try { | ||
await updateProfile(auth.currentUser!, { displayName, photoURL }); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
return { updateUserProfile }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,44 @@ | ||
import Profile from "../_profile"; | ||
import { useContext, useEffect, useState } from "react"; | ||
import { AuthContext } from "@/contexts/AuthContext"; | ||
import { Attempt } from "@/types/UserTypes"; | ||
import { useHistory } from "@/hooks/useHistory"; | ||
import { useRouter } from "next/router"; | ||
import { User } from "firebase/auth"; | ||
import { useUser } from "@/hooks/useUser"; | ||
|
||
export default function Page() { | ||
// TODO: retrieve selected user from user id in url | ||
const router = useRouter(); | ||
const id = router.query.id; | ||
const { getAppUser } = useUser(); | ||
const { user: currentUser } = useContext(AuthContext); | ||
const { fetchAttempts } = useHistory(); | ||
|
||
const selectedUser = { | ||
displayName: "John Doe", | ||
email: "[email protected]", | ||
photoURL: "https://www.gravatar.com/avatar/00", | ||
} | ||
const [attempts, setAttempts] = useState<Attempt[]>([]); | ||
const [user, setUser] = useState<User>(); | ||
const [loadingState, setLoadingState] = useState<"loading" | "error" | "success">("loading"); | ||
|
||
useEffect(() => { | ||
if (currentUser && (typeof id === "string")) { | ||
Promise.all([getAppUser(id), fetchAttempts(id)]).then(([user, attempts]) => { | ||
if (user && attempts) { | ||
user["photoURL"] = user["photoUrl"]; | ||
console.log(user); | ||
setUser(user); | ||
setAttempts(attempts); | ||
setLoadingState("success"); | ||
} else { | ||
throw new Error("User or attempts not found"); | ||
} | ||
}).catch((err: any) => { | ||
setLoadingState("error"); | ||
console.log(err); | ||
}); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [currentUser]); | ||
|
||
// TODO: if selected user is null, redirect to 404 page | ||
return ( | ||
<Profile selectedUser={selectedUser as User} isCurrentUser={false} attempts={[]}/> | ||
(user && <Profile selectedUser={user} isCurrentUser={user.uid === currentUser?.uid} loadingState={loadingState} attempts={attempts}/>) | ||
) | ||
} |
Oops, something went wrong.