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