fix: user notification preference state not reflecting the real state (#5138)

* fix: user notification preference state

* explicit that the preferences state is the previous one
This commit is contained in:
Reinaldo Neto 2023-07-19 16:47:01 -03:00 committed by GitHub
parent 456344029c
commit 15ec8c121d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import React, { useState } from 'react'; import React from 'react';
import { StyleSheet, Text } from 'react-native'; import { StyleSheet, Text } from 'react-native';
import * as List from '../../containers/List'; import * as List from '../../containers/List';
@ -21,7 +21,7 @@ type TKey = 'desktopNotifications' | 'pushNotifications' | 'emailNotificationMod
interface IBaseParams { interface IBaseParams {
preference: TKey; preference: TKey;
value: string; value: string;
onChangeValue: (param: { [key: string]: string }, onError: () => void) => void; onChangeValue: (param: { [key: string]: string }) => void;
} }
const ListPicker = ({ const ListPicker = ({
@ -36,17 +36,14 @@ const ListPicker = ({
} & IBaseParams) => { } & IBaseParams) => {
const { showActionSheet, hideActionSheet } = useActionSheet(); const { showActionSheet, hideActionSheet } = useActionSheet();
const { colors } = useTheme(); const { colors } = useTheme();
const [option, setOption] = useState( const option = value ? OPTIONS[preference].find(option => option.value === value) : OPTIONS[preference][0];
value ? OPTIONS[preference].find(option => option.value === value) : OPTIONS[preference][0]
);
const getOptions = () => const getOptions = () =>
OPTIONS[preference].map(i => ({ OPTIONS[preference].map(i => ({
title: I18n.t(i.label, { defaultValue: i.label }), title: I18n.t(i.label, { defaultValue: i.label }),
onPress: () => { onPress: () => {
hideActionSheet(); hideActionSheet();
onChangeValue({ [preference]: i.value.toString() }, () => setOption(option)); onChangeValue({ [preference]: i.value.toString() });
setOption(i);
}, },
right: option?.value === i.value ? () => <CustomIcon name={'check'} size={20} color={colors.tintActive} /> : undefined right: option?.value === i.value ? () => <CustomIcon name={'check'} size={20} color={colors.tintActive} /> : undefined
})); }));

View File

@ -44,18 +44,17 @@ const UserNotificationPreferencesView = () => {
getPreferences(); getPreferences();
}, [userId]); }, [userId]);
const onValueChangePicker = async (param: { [key: string]: string }, onError: () => void) => { const onValueChangePicker = async (param: { [key: string]: string }) => {
const previousPreferences = preferences;
try { try {
setPreferences({ ...previousPreferences, ...param });
const result = await Services.setUserPreferences(userId, param); const result = await Services.setUserPreferences(userId, param);
if (result.success) { if (!result.success) {
const { setPreferences(previousPreferences);
user: { settings }
} = result;
setPreferences(settings.preferences);
} }
} catch (error) { } catch (error) {
setPreferences(previousPreferences);
log(error); log(error);
onError();
} }
}; };