Now we are past the initial sign in bugs

This commit is contained in:
Gabriel Brown 2024-10-18 13:24:12 -05:00
parent 770d04540c
commit 7fbaad97a1

View File

@ -1,15 +1,15 @@
import React, { useState, useEffect, useRef, ErrorInfo } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { Alert, Platform } from 'react-native'; import { Alert, Platform } from 'react-native';
import * as Device from 'expo-device'; import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications'; import * as Notifications from 'expo-notifications';
import Constants from 'expo-constants'; import Constants from 'expo-constants';
import type { NotificationMessage } from '@/constants/Types'; import { NotificationMessage } from '@/constants/Types';
Notifications.setNotificationHandler({ Notifications.setNotificationHandler({
handleNotification: async () => ({ handleNotification: async () => ({
shouldShowAlert: true, shouldShowAlert: true,
shouldPlaySound: true, shouldPlaySound: false,
shouldSetBadge: true, shouldSetBadge: false,
}), }),
}); });
@ -48,7 +48,7 @@ const handleRegistrationError = (errorMessage: string) => {
throw new Error(errorMessage); throw new Error(errorMessage);
}; };
const registerforPushNotificationsAsync = async () => { async function registerForPushNotificationsAsync() {
let token; let token;
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
@ -73,69 +73,40 @@ const registerforPushNotificationsAsync = async () => {
} }
const projectId = Constants.expoConfig?.extra?.eas?.projectId; const projectId = Constants.expoConfig?.extra?.eas?.projectId;
if (!projectId) { if (!projectId) {
alert('Project ID not found in eas.json'); alert('Project ID not found');
return; return;
} }
token = (await Notifications.getExpoPushTokenAsync({ projectId })).data; token = (await Notifications.getExpoPushTokenAsync({ projectId })).data;
} else { } else {
alert('Must use physical device for Push Notifications'); alert('Must use physical device for Push Notifications');
} }
return token;
};
export const PushNotificationManager = ({children}: {children: React.ReactNode}) => { return token;
const [expoPushToken, setExpoPushToken] = useState(''); }
const [notification, setNotification] =
useState<Notifications.Notification | undefined>(undefined); export function PushNotificationManager({ children }: { children: React.ReactNode }) {
const [expoPushToken, setExpoPushToken] = useState<string | undefined>('');
const [notification, setNotification] = useState<Notifications.Notification | undefined>(undefined);
const notificationListener = useRef<Notifications.Subscription>(); const notificationListener = useRef<Notifications.Subscription>();
const responseListener = useRef<Notifications.Subscription>(); const responseListener = useRef<Notifications.Subscription>();
useEffect(() => { useEffect(() => {
registerforPushNotificationsAsync() registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
.then(token => setExpoPushToken(token ?? ''))
.catch((error: any) => {
setExpoPushToken('');
console.error(error);
});
notificationListener.current = Notifications.addNotificationReceivedListener( notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
notification => { setNotification(notification);
setNotification(notification);
}); });
responseListener.current = Notifications.addNotificationResponseReceivedListener(
response => { responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log(response); console.log(response);
// Handle notification response here // Handle notification response here
}); });
return () => { return () => {
notificationListener.current && Notifications.removeNotificationSubscription(notificationListener.current!);
Notifications.removeNotificationSubscription(notificationListener.current); Notifications.removeNotificationSubscription(responseListener.current!);
responseListener.current &&
Notifications.removeNotificationSubscription(responseListener.current);
}; };
}, []); }, []);
return ( <ErrorBoundary>{children}</ErrorBoundary> );
};
class ErrorBoundary extends React.Component<{children: React.ReactNode}, {hasError: boolean}> { return <>{children}</>;
constructor(props: {children: React.ReactNode}) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(_: Error) {
return { hasError: true };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.log('Error caught by ErrorBoundary:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return <ThemedText>Something went wrong.</ThemedText>;
}
return this.props.children;
}
} }