Editing calendar events

- Fixed an issue with the profile data not being read from db becaue of the local state not being updated instantly
This commit is contained in:
Dejan
2024-10-11 16:08:39 +02:00
parent 562da49806
commit a05de1b333

View File

@ -30,29 +30,28 @@ export const AuthContextProvider: FC<{ children: ReactNode }> = ({children}) =>
const {replace} = useRouter() const {replace} = useRouter()
const ready = !initializing const ready = !initializing
const onAuthStateChanged = async (user: FirebaseAuthTypes.User | null) => { const onAuthStateChangedHandler = async (authUser: FirebaseAuthTypes.User | null) => {
setUser(user); setUser(authUser);
if (user) { if (authUser) {
await refreshProfileData(); await refreshProfileData(authUser);
} }
if (initializing) setInitializing(false); if (initializing) setInitializing(false);
} }
const refreshProfileData = async () => { const refreshProfileData = async (authUser: FirebaseAuthTypes.User) => {
if (user) { if (authUser) {
try { try {
const documentSnapshot = await firestore() const documentSnapshot = await firestore()
.collection("Profiles") .collection("Profiles")
.doc(user.uid) .doc(authUser.uid)
.get(); .get();
if (documentSnapshot.exists) { if (documentSnapshot.exists) {
setProfileType(documentSnapshot.data()?.userType); setProfileType(documentSnapshot.data()?.userType);
setProfileData(documentSnapshot.data() as UserProfile); setProfileData(documentSnapshot.data() as UserProfile);
} }
} catch (error) { } catch (error) {
console.error("Error fetching user profile data:", error);
setProfileType(undefined); setProfileType(undefined);
setProfileData(undefined); setProfileData(undefined);
} }
@ -60,7 +59,7 @@ export const AuthContextProvider: FC<{ children: ReactNode }> = ({children}) =>
}; };
useEffect(() => { useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged); const subscriber = auth().onAuthStateChanged(onAuthStateChangedHandler);
return subscriber; return subscriber;
}, []); }, []);