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 { 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', {

View file

@ -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,6 +39,7 @@ export default function CategoryScreen() {
// Set session data
useEffect( () => {
if (!sessionSet) {
if (session.notifications.dayBefore === 'off') {
setIsDayBeforeEnabled( false );
} else {
@ -46,8 +51,11 @@ export default function CategoryScreen() {
setIsSameDayEnabled( false );
} else {
setIsSameDayEnabled( true );
setSameDay( session.notifications.dayBefore );
setSameDay( session.notifications.sameDay );
}
}
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.' );
}
} )

View file

@ -35,8 +35,7 @@ export default function HomeScreen() {
OneSignal.initialize("6ef15aa7-9dc0-4d8b-b6b1-e2005bcd3dc6");
// Request permission
OneSignal.Notifications.requestPermission(true)
OneSignal.Notifications.requestPermission(true).then(() => {
// Retrieve one signal user token
OneSignal.User.pushSubscription.getIdAsync().then((notificationsToken) => {
if (notificationsToken) {
@ -47,6 +46,7 @@ export default function HomeScreen() {
} );
}
})
})
// Disable loading again
setOnesignalLoaded(true);

View file

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

View file

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