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

@ -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.' );
}
} )