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

View File

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