[FIX] Message parser switch not updating field properly (#3576)

This commit is contained in:
Gerzon Z 2022-01-12 10:05:30 -04:00 committed by GitHub
parent bff99d5c0b
commit e68485bccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 9 deletions

View File

@ -1,8 +1,9 @@
import { StackNavigationProp } from '@react-navigation/stack';
import React, { useEffect, useState } from 'react';
import React, { useEffect } from 'react';
import { Switch } from 'react-native';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { setUser } from '../../actions/login';
import I18n from '../../i18n';
import log, { logEvent, events } from '../../utils/log';
import SafeAreaView from '../../containers/SafeAreaView';
@ -18,8 +19,8 @@ interface IUserPreferencesViewProps {
}
const UserPreferencesView = ({ navigation }: IUserPreferencesViewProps): JSX.Element => {
const user = useSelector(state => getUserSelector(state));
const [enableParser, setEnableParser] = useState(user.enableMessageParserEarlyAdoption);
const { enableMessageParserEarlyAdoption, id } = useSelector(state => getUserSelector(state));
const dispatch = useDispatch();
useEffect(() => {
navigation.setOptions({
@ -34,15 +35,15 @@ const UserPreferencesView = ({ navigation }: IUserPreferencesViewProps): JSX.Ele
const toggleMessageParser = async (value: boolean) => {
try {
await RocketChat.saveUserPreferences({ id: user.id, enableMessageParserEarlyAdoption: value });
setEnableParser(value);
dispatch(setUser({ enableMessageParserEarlyAdoption: value }));
await RocketChat.saveUserPreferences({ id, enableMessageParserEarlyAdoption: value });
} catch (e) {
log(e);
}
};
const renderMessageParserSwitch = () => (
<Switch value={enableParser} trackColor={SWITCH_TRACK_COLOR} onValueChange={toggleMessageParser} />
const renderMessageParserSwitch = (value: boolean) => (
<Switch value={value} trackColor={SWITCH_TRACK_COLOR} onValueChange={toggleMessageParser} />
);
return (
@ -64,7 +65,7 @@ const UserPreferencesView = ({ navigation }: IUserPreferencesViewProps): JSX.Ele
<List.Item
title='Enable_Message_Parser'
testID='preferences-view-enable-message-parser'
right={() => renderMessageParserSwitch()}
right={() => renderMessageParserSwitch(enableMessageParserEarlyAdoption)}
/>
<List.Separator />
</List.Section>