Fix saving notification data

This commit is contained in:
Maarten 2024-08-09 13:47:21 +02:00
parent 85dc440fe8
commit ad990ca528
5 changed files with 43 additions and 28 deletions

View file

@ -2,7 +2,7 @@ import React from 'react';
import { Stack } from 'expo-router'; import { Stack } from 'expo-router';
import { StyleSheet, TextInput, TouchableOpacity } from 'react-native'; import { StyleSheet, TextInput, TouchableOpacity } from 'react-native';
import { router } from 'expo-router'; 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 { ThemedText } from '@/lib/components/ThemedText';
import { ThemedView } from '@/lib/components/ThemedView'; import { ThemedView } from '@/lib/components/ThemedView';
@ -12,7 +12,7 @@ import { useColorScheme } from '@/lib/hooks/useColorScheme';
import { Message } from '@/lib/services/message'; import { Message } from '@/lib/services/message';
import { Request } from '@/lib/services/request'; import { Request } from '@/lib/services/request';
import { store } from '@/lib/store/store'; import { store } from '@/lib/store/store';
import { setSession, setReloadCalendar } from '@/lib/store/dataStore'; import { setSession } from '@/lib/store/dataStore';
export default function OnboardStartScreen() { export default function OnboardStartScreen() {
const colorScheme = useColorScheme() ?? 'light'; const colorScheme = useColorScheme() ?? 'light';
@ -29,7 +29,7 @@ export default function OnboardStartScreen() {
} }
// Get device name info // Get device name info
const deviceName = ''; // DeviceInfo.getModel(); const deviceName = DeviceInfo.getModel();
Request Request
.post( 'sessions/create', { .post( 'sessions/create', {

View file

@ -12,6 +12,8 @@ import { ThemedText } from '@/lib/components/ThemedText';
import { Request } from '@/lib/services/request'; import { Request } from '@/lib/services/request';
import { useToken } from '@/lib/context/AppProvider'; import { useToken } from '@/lib/context/AppProvider';
import { Message } from '@/lib/services/message'; import { Message } from '@/lib/services/message';
import { store } from '@/lib/store/store';
import { setSession } from '@/lib/store/dataStore';
export default function CategoryScreen() { export default function CategoryScreen() {
const colorScheme = useColorScheme() ?? 'light'; const colorScheme = useColorScheme() ?? 'light';
@ -19,6 +21,8 @@ export default function CategoryScreen() {
const { token } = useToken(); const { token } = useToken();
const session = useSelector( (state: any) => state.data.session ); const session = useSelector( (state: any) => state.data.session );
const [ sessionSet, setSessionSet ] = useState( false );
const [ isDayBeforeEnabled, setIsDayBeforeEnabled ] = useState( true ); const [ isDayBeforeEnabled, setIsDayBeforeEnabled ] = useState( true );
const [ dayBefore, setDayBefore ] = useState( '19:30' ); const [ dayBefore, setDayBefore ] = useState( '19:30' );
@ -35,19 +39,23 @@ export default function CategoryScreen() {
// Set session data // Set session data
useEffect( () => { useEffect( () => {
if (session.notifications.dayBefore === 'off') { if (!sessionSet) {
setIsDayBeforeEnabled( false ); if (session.notifications.dayBefore === 'off') {
} else { setIsDayBeforeEnabled( false );
setIsDayBeforeEnabled( true ); } else {
setDayBefore( session.notifications.dayBefore ); 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') { setSessionSet( true );
setIsSameDayEnabled( false );
} else {
setIsSameDayEnabled( true );
setSameDay( session.notifications.dayBefore );
}
}, [ session ] ); }, [ session ] );
// Update session when something changes // Update session when something changes
@ -101,6 +109,10 @@ export default function CategoryScreen() {
} }
const updateSession = () => { const updateSession = () => {
if (!sessionSet) {
return;
}
const postData = { const postData = {
token: token, token: token,
notification_day_before: isDayBeforeEnabled ? dayBefore : 'off', notification_day_before: isDayBeforeEnabled ? dayBefore : 'off',
@ -108,7 +120,9 @@ export default function CategoryScreen() {
}; };
Request.post( 'sessions/update', postData ).then( (response) => { 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.' ); Message.error( 'Er ging iets mis. Probeer het later opnieuw.' );
} }
} ) } )

View file

@ -35,17 +35,17 @@ export default function HomeScreen() {
OneSignal.initialize("6ef15aa7-9dc0-4d8b-b6b1-e2005bcd3dc6"); OneSignal.initialize("6ef15aa7-9dc0-4d8b-b6b1-e2005bcd3dc6");
// Request permission // Request permission
OneSignal.Notifications.requestPermission(true) OneSignal.Notifications.requestPermission(true).then(() => {
// Retrieve one signal user token
// Retrieve one signal user token OneSignal.User.pushSubscription.getIdAsync().then((notificationsToken) => {
OneSignal.User.pushSubscription.getIdAsync().then((notificationsToken) => { if (notificationsToken) {
if (notificationsToken) { Request.post( 'sessions/update', { token: token, notifications_token: notificationsToken } ).then( (response) => {
Request.post( 'sessions/update', { token: token, notifications_token: notificationsToken } ).then( (response) => { if (!response.success) {
if (!response.success) { Message.error( 'Notificaties zijn uitgeschakeld door een onbekende error' );
Message.error( 'Notificaties zijn uitgeschakeld door een onbekende error' ); }
} } );
} ); }
} })
}) })
// Disable loading again // Disable loading again

View file

@ -157,6 +157,7 @@ export default function SettingsScreen() {
</ThemedView> </ThemedView>
<TouchableOpacity style={styles.listEdit} onPress={() => router.push( '/(settings)/notifications' )}> <TouchableOpacity style={styles.listEdit} onPress={() => router.push( '/(settings)/notifications' )}>
<ThemedText style={styles.listEditText}>Wijzigen</ThemedText>
<Ionicons size={18} name="chevron-forward" style={styles.listEditIcon}/> <Ionicons size={18} name="chevron-forward" style={styles.listEditIcon}/>
</TouchableOpacity> </TouchableOpacity>
</ThemedView> </ThemedView>

View file

@ -20,8 +20,8 @@ const dataStore = createSlice( {
longitude: '', longitude: '',
}, },
notifications: { notifications: {
dayBefore: '', dayBefore: 'off',
sameDay: '', sameDay: 'off',
}, },
}, },
reloadCalendar: true, reloadCalendar: true,