From 15ec8c121d0198fcfe2b1264ebcb72f66f732e2d Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Wed, 19 Jul 2023 16:47:01 -0300 Subject: [PATCH] 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 --- .../UserNotificationPreferencesView/ListPicker.tsx | 11 ++++------- app/views/UserNotificationPreferencesView/index.tsx | 13 ++++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/app/views/UserNotificationPreferencesView/ListPicker.tsx b/app/views/UserNotificationPreferencesView/ListPicker.tsx index 4ff76292b..f43bcf98d 100644 --- a/app/views/UserNotificationPreferencesView/ListPicker.tsx +++ b/app/views/UserNotificationPreferencesView/ListPicker.tsx @@ -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 ? () => : undefined })); diff --git a/app/views/UserNotificationPreferencesView/index.tsx b/app/views/UserNotificationPreferencesView/index.tsx index f624f5d92..1dcfc5fe4 100644 --- a/app/views/UserNotificationPreferencesView/index.tsx +++ b/app/views/UserNotificationPreferencesView/index.tsx @@ -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(); } };