New onboarding flow, calendar sync logic refactor

This commit is contained in:
Milan Paunovic
2024-11-01 03:18:50 +01:00
parent 539cbd9f10
commit 7f68f3acf8
18 changed files with 1153 additions and 671 deletions

View File

@ -8,7 +8,7 @@ import {UserProfile} from "@/hooks/firebase/types/profileTypes";
import * as Notifications from 'expo-notifications';
import * as Device from 'expo-device';
import Constants from 'expo-constants';
import { Platform } from 'react-native';
import {Platform} from 'react-native';
import {useQueryClient} from "react-query";
@ -24,6 +24,7 @@ interface IAuthContext {
profileType?: ProfileType,
profileData?: UserProfile,
setProfileData: (profileData: UserProfile) => void,
setRedirectOverride: (val: boolean) => void,
refreshProfileData: () => Promise<void>
}
@ -50,11 +51,11 @@ async function registerForPushNotificationsAsync() {
}
if (Device.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
const {status: existingStatus} = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
const {status} = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
@ -72,7 +73,7 @@ async function registerForPushNotificationsAsync() {
}
try {
const token = (await Notifications.getExpoPushTokenAsync({ projectId })).data;
const token = (await Notifications.getExpoPushTokenAsync({projectId})).data;
console.log('Push Token:', token);
return token;
} catch (error) {
@ -91,24 +92,28 @@ export const AuthContextProvider: FC<{ children: ReactNode }> = ({children}) =>
const [initializing, setInitializing] = useState(true);
const [profileType, setProfileType] = useState<ProfileType | undefined>(undefined);
const [profileData, setProfileData] = useState<UserProfile | undefined>(undefined);
const [redirectOverride, setRedirectOverride] = useState(false);
const {replace} = useRouter();
const ready = !initializing;
const queryClient = useQueryClient();
const onAuthStateChangedHandler = async (authUser: FirebaseAuthTypes.User | null) => {
setUser(authUser);
if (!redirectOverride) {
setUser(authUser);
if (authUser) {
await refreshProfileData(authUser);
const pushToken = await registerForPushNotificationsAsync();
if (pushToken) {
await savePushTokenToFirestore(authUser.uid, pushToken);
if (authUser) {
await refreshProfileData(authUser);
const pushToken = await registerForPushNotificationsAsync();
if (pushToken) {
await savePushTokenToFirestore(authUser.uid, pushToken);
}
}
}
if (initializing) setInitializing(false);
if (initializing) setInitializing(false);
}
};
const refreshProfileData = async (user?: FirebaseAuthTypes.User) => {
@ -152,12 +157,12 @@ export const AuthContextProvider: FC<{ children: ReactNode }> = ({children}) =>
}, [initializing]);
useEffect(() => {
if (ready && user) {
if (ready && user && !redirectOverride) {
replace({pathname: "/(auth)/calendar"});
} else if (ready && !user) {
} else if (ready && !user && !redirectOverride) {
replace({pathname: "/(unauth)"});
}
}, [user, ready]);
}, [user, ready, redirectOverride]);
useEffect(() => {
const sub = Notifications.addNotificationReceivedListener(notification => {
@ -175,7 +180,8 @@ export const AuthContextProvider: FC<{ children: ReactNode }> = ({children}) =>
}
return (
<AuthContext.Provider value={{user, profileType, profileData, setProfileData, refreshProfileData}}>
<AuthContext.Provider
value={{user, profileType, profileData, setProfileData, refreshProfileData, setRedirectOverride}}>
{children}
</AuthContext.Provider>
);