Skip to content

Commit

Permalink
Merge branch 'rbeggs/imp-107-notification-login-issues' of https://gi…
Browse files Browse the repository at this point in the history
…thub.com/calblueprint/impact-fund into stephanie/deployment
  • Loading branch information
ronniebeggs committed Aug 22, 2024
2 parents 2d66f07 + 32eddb0 commit f82371b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
38 changes: 37 additions & 1 deletion src/app/(BottomTabNavigation)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Notifications from 'expo-notifications';
import { Tabs } from 'expo-router/tabs';
import React from 'react';
import React, { useEffect } from 'react';

import GreyHomeIcon from '../../../assets/bottom-tab-home-inactive.svg';
import RedHomeIcon from '../../../assets/bottom-tab-home.svg';
Expand All @@ -9,8 +10,43 @@ import GreyGearIcon from '../../../assets/bottom-tab-settings-gear-inactive.svg'
import RedGearIcon from '../../../assets/bottom-tab-settings-gear.svg';
import TabBarItem from '../../Components/TabBarItem/TabBarItem';
import { CaseContextProvider } from '../../context/CaseContext';
import { routeUserToUpdate } from '../../supabase/pushNotifications';

Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});

function useNotificationObserver() {
useEffect(() => {
let isMounted = true;

Notifications.getLastNotificationResponseAsync().then(response => {
if (!isMounted || !response?.notification) {
return;
}
routeUserToUpdate(response);
});

const subscription = Notifications.addNotificationResponseReceivedListener(
response => {
routeUserToUpdate(response);
},
);

return () => {
isMounted = false;
subscription.remove();
};
}, []);
}

export default function AppLayout() {
useNotificationObserver();

return (
<CaseContextProvider>
<Tabs
Expand Down
36 changes: 17 additions & 19 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import React, {
useEffect,
useMemo,
useState,
useRef,
// useRef,
} from 'react';

import supabaseAdmin from '../supabase/createAdminClient';
import supabase from '../supabase/createClient';
import {
removePushToken,
routeUserToUpdate,
// routeUserToUpdate,
} from '../supabase/pushNotifications';

/**
Expand Down Expand Up @@ -56,14 +56,6 @@ export interface AuthState {

const AuthContext = createContext({} as AuthState);

Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});

export function useSession() {
return useContext(AuthContext);
}
Expand All @@ -76,7 +68,7 @@ export function AuthContextProvider({
const [session, setSession] = useState<Session | null>(null);
const [user, setUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const responseListener = useRef<Notifications.Subscription>();
// const responseListener = useRef<Notifications.Subscription>();

useEffect(() => {
supabase.auth
Expand All @@ -89,19 +81,25 @@ export function AuthContextProvider({
setIsLoading(false);
});

responseListener.current =
Notifications.addNotificationResponseReceivedListener(response => {
supabase.auth.getSession().then(() => {
routeUserToUpdate(response);
});
});
// Notifications.getLastNotificationResponseAsync()
// .then(response => {
// if (!isMounted || !response?.notification) {
// return;
// }
// routeUserToUpdate(response);
// });

// responseListener.current =
// Notifications.addNotificationResponseReceivedListener(response => {
// routeUserToUpdate(response);
// });

supabase.auth.onAuthStateChange((_event, newSession) => {
setSession(newSession);
});

return () =>
Notifications.removeNotificationSubscription(responseListener.current!);
// return () =>
// Notifications.removeNotificationSubscription(responseListener.current!);
}, []);

const signInWithEmail = async (
Expand Down

0 comments on commit f82371b

Please sign in to comment.