From ad990ca528159388ffd416a2e1e5e9cceeb60a9b Mon Sep 17 00:00:00 2001 From: Maarten Date: Fri, 9 Aug 2024 13:47:21 +0200 Subject: [PATCH] Fix saving notification data --- app/(onboarding)/start.tsx | 6 ++--- app/(settings)/notifications.tsx | 38 ++++++++++++++++++++++---------- app/(tabs)/index.tsx | 22 +++++++++--------- app/(tabs)/settings.tsx | 1 + lib/store/dataStore.tsx | 4 ++-- 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/app/(onboarding)/start.tsx b/app/(onboarding)/start.tsx index 65db161..a5f7878 100644 --- a/app/(onboarding)/start.tsx +++ b/app/(onboarding)/start.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { Stack } from 'expo-router'; import { StyleSheet, TextInput, TouchableOpacity } from 'react-native'; import { router } from 'expo-router'; -// import DeviceInfo from 'react-native-device-info'; +import DeviceInfo from 'react-native-device-info'; import { ThemedText } from '@/lib/components/ThemedText'; import { ThemedView } from '@/lib/components/ThemedView'; @@ -12,7 +12,7 @@ import { useColorScheme } from '@/lib/hooks/useColorScheme'; import { Message } from '@/lib/services/message'; import { Request } from '@/lib/services/request'; import { store } from '@/lib/store/store'; -import { setSession, setReloadCalendar } from '@/lib/store/dataStore'; +import { setSession } from '@/lib/store/dataStore'; export default function OnboardStartScreen() { const colorScheme = useColorScheme() ?? 'light'; @@ -29,7 +29,7 @@ export default function OnboardStartScreen() { } // Get device name info - const deviceName = ''; // DeviceInfo.getModel(); + const deviceName = DeviceInfo.getModel(); Request .post( 'sessions/create', { diff --git a/app/(settings)/notifications.tsx b/app/(settings)/notifications.tsx index f15d66c..3c04fb9 100644 --- a/app/(settings)/notifications.tsx +++ b/app/(settings)/notifications.tsx @@ -12,6 +12,8 @@ import { ThemedText } from '@/lib/components/ThemedText'; import { Request } from '@/lib/services/request'; import { useToken } from '@/lib/context/AppProvider'; import { Message } from '@/lib/services/message'; +import { store } from '@/lib/store/store'; +import { setSession } from '@/lib/store/dataStore'; export default function CategoryScreen() { const colorScheme = useColorScheme() ?? 'light'; @@ -19,6 +21,8 @@ export default function CategoryScreen() { const { token } = useToken(); const session = useSelector( (state: any) => state.data.session ); + const [ sessionSet, setSessionSet ] = useState( false ); + const [ isDayBeforeEnabled, setIsDayBeforeEnabled ] = useState( true ); const [ dayBefore, setDayBefore ] = useState( '19:30' ); @@ -35,19 +39,23 @@ export default function CategoryScreen() { // Set session data useEffect( () => { - if (session.notifications.dayBefore === 'off') { - setIsDayBeforeEnabled( false ); - } else { - setIsDayBeforeEnabled( true ); - setDayBefore( session.notifications.dayBefore ); + if (!sessionSet) { + if (session.notifications.dayBefore === 'off') { + setIsDayBeforeEnabled( false ); + } else { + setIsDayBeforeEnabled( true ); + setDayBefore( session.notifications.dayBefore ); + } + + if (session.notifications.sameDay === 'off') { + setIsSameDayEnabled( false ); + } else { + setIsSameDayEnabled( true ); + setSameDay( session.notifications.sameDay ); + } } - if (session.notifications.sameDay === 'off') { - setIsSameDayEnabled( false ); - } else { - setIsSameDayEnabled( true ); - setSameDay( session.notifications.dayBefore ); - } + setSessionSet( true ); }, [ session ] ); // Update session when something changes @@ -101,6 +109,10 @@ export default function CategoryScreen() { } const updateSession = () => { + if (!sessionSet) { + return; + } + const postData = { token: token, notification_day_before: isDayBeforeEnabled ? dayBefore : 'off', @@ -108,7 +120,9 @@ export default function CategoryScreen() { }; Request.post( 'sessions/update', postData ).then( (response) => { - if (!response.success) { + if (response.success) { + store.dispatch( setSession( response.session ) ); + } else { Message.error( 'Er ging iets mis. Probeer het later opnieuw.' ); } } ) diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index 588321d..7c092e5 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -35,17 +35,17 @@ export default function HomeScreen() { OneSignal.initialize("6ef15aa7-9dc0-4d8b-b6b1-e2005bcd3dc6"); // Request permission - OneSignal.Notifications.requestPermission(true) - - // Retrieve one signal user token - OneSignal.User.pushSubscription.getIdAsync().then((notificationsToken) => { - if (notificationsToken) { - Request.post( 'sessions/update', { token: token, notifications_token: notificationsToken } ).then( (response) => { - if (!response.success) { - Message.error( 'Notificaties zijn uitgeschakeld door een onbekende error' ); - } - } ); - } + OneSignal.Notifications.requestPermission(true).then(() => { + // Retrieve one signal user token + OneSignal.User.pushSubscription.getIdAsync().then((notificationsToken) => { + if (notificationsToken) { + Request.post( 'sessions/update', { token: token, notifications_token: notificationsToken } ).then( (response) => { + if (!response.success) { + Message.error( 'Notificaties zijn uitgeschakeld door een onbekende error' ); + } + } ); + } + }) }) // Disable loading again diff --git a/app/(tabs)/settings.tsx b/app/(tabs)/settings.tsx index e04dd6e..c4d70f3 100644 --- a/app/(tabs)/settings.tsx +++ b/app/(tabs)/settings.tsx @@ -157,6 +157,7 @@ export default function SettingsScreen() { router.push( '/(settings)/notifications' )}> + Wijzigen diff --git a/lib/store/dataStore.tsx b/lib/store/dataStore.tsx index 9cb91de..ea3943d 100644 --- a/lib/store/dataStore.tsx +++ b/lib/store/dataStore.tsx @@ -20,8 +20,8 @@ const dataStore = createSlice( { longitude: '', }, notifications: { - dayBefore: '', - sameDay: '', + dayBefore: 'off', + sameDay: 'off', }, }, reloadCalendar: true,