Skip to content

Commit

Permalink
settings: Clear all PerAccountSettingsState on RESET_ACCOUNT_DATA
Browse files Browse the repository at this point in the history
Really an instance of #4446 that I forgot about in #5606, oops.

Related: #4446
  • Loading branch information
chrisbobbe committed Dec 21, 2022
1 parent 47883e4 commit e8ad88c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
31 changes: 30 additions & 1 deletion src/settings/__tests__/settingsReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,41 @@ import {
import type { InitialData } from '../../api/initialDataTypes';
import type { UserSettings } from '../../api/modelTypes';
import { EventTypes } from '../../api/eventTypes';
import settingsReducer from '../settingsReducer';
import settingsReducer, { initialPerAccountSettingsState } from '../settingsReducer';
import * as eg from '../../__tests__/lib/exampleData';

describe('settingsReducer', () => {
const baseState = eg.baseReduxState.settings;

describe('RESET_ACCOUNT_DATA', () => {
test('resets per-account state without touching global state', () => {
const prevState = [
// per-account
eg.mkActionRegisterComplete({
user_settings: {
/* $FlowIgnore[incompatible-cast] - testing modern servers, which
send user_settings. */
...(eg.action.register_complete.data.user_settings: $NonMaybeType<
InitialData['user_settings'],
>),
enable_offline_push_notifications: false,
enable_online_push_notifications: false,
enable_stream_push_notifications: true,
display_emoji_reaction_users: true,
},
}),

// global
{ type: SET_GLOBAL_SETTINGS, update: { theme: 'night' } },
{ type: SET_GLOBAL_SETTINGS, update: { language: 'fr' } },
].reduce(settingsReducer, eg.baseReduxState.settings);
expect(settingsReducer(prevState, eg.action.reset_account_data)).toEqual({
...prevState,
...initialPerAccountSettingsState,
});
});
});

describe('REGISTER_COMPLETE', () => {
test('changes value of all notification settings (legacy, without user_settings)', () => {
const prevState = deepFreeze({
Expand Down
23 changes: 14 additions & 9 deletions src/settings/settingsReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* @flow strict-local */
import type { GlobalSettingsState, PerAccountSettingsState } from '../reduxTypes';
import type { SettingsState, Action } from '../types';
import {
RESET_ACCOUNT_DATA,
SET_GLOBAL_SETTINGS,
REGISTER_COMPLETE,
EVENT_UPDATE_GLOBAL_NOTIFICATIONS_SETTINGS,
Expand All @@ -9,30 +11,33 @@ import {
import { EventTypes } from '../api/eventTypes';
import { ensureUnreachable } from '../types';

const initialState: SettingsState = {
//
// GlobalSettingsState
//

const initialGlobalSettingsState: $Exact<GlobalSettingsState> = {
language: 'en',
theme: 'default',
browser: 'default',
experimentalFeaturesEnabled: false,
markMessagesReadOnScroll: 'always',
};

//
// PerAccountSettingsState
//

/** PRIVATE; exported only for tests. */
export const initialPerAccountSettingsState: $Exact<PerAccountSettingsState> = {
offlineNotification: true,
onlineNotification: true,
streamNotification: false,
displayEmojiReactionUsers: false,
};

const initialState: SettingsState = {
...initialGlobalSettingsState,
...initialPerAccountSettingsState,
};

// eslint-disable-next-line default-param-last
export default (state: SettingsState = initialState, action: Action): SettingsState => {
switch (action.type) {
case RESET_ACCOUNT_DATA:
return { ...state, ...initialPerAccountSettingsState };

case REGISTER_COMPLETE: {
const { data } = action;

Expand Down

0 comments on commit e8ad88c

Please sign in to comment.