Skip to content

Commit

Permalink
[FIX] Message parser switch not updating field properly (#3576)
Browse files Browse the repository at this point in the history
  • Loading branch information
gerzonc authored Jan 12, 2022
1 parent bff99d5 commit e68485b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions app/views/UserPreferencesView/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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({
Expand All @@ -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 (
Expand All @@ -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>
Expand Down

0 comments on commit e68485b

Please sign in to comment.