From 9f830c7f0a554b95360d2746822edb2bbb905629 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 1 Apr 2020 16:24:42 -0300 Subject: [PATCH] [FIX] Clear settings on server change (#1967) --- app/actions/actionsTypes.js | 1 + app/actions/index.js | 7 ------- app/actions/settings.js | 14 ++++++++++++++ app/constants/types.js | 1 + app/lib/methods/getSettings.js | 9 +++++---- app/reducers/index.js | 2 +- app/reducers/initialState.js | 5 ----- app/reducers/reducers.js | 12 ------------ app/reducers/rootReducer.js | 10 ---------- app/reducers/settings.js | 17 +++++++++++++++++ app/sagas/selectServer.js | 3 +++ 11 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 app/actions/settings.js delete mode 100644 app/reducers/initialState.js delete mode 100644 app/reducers/reducers.js delete mode 100644 app/reducers/rootReducer.js create mode 100644 app/reducers/settings.js diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 478903e09..33e202a56 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -63,3 +63,4 @@ export const INVITE_LINKS = createRequestTypes('INVITE_LINKS', [ 'CLEAR', ...defaultTypes ]); +export const SETTINGS = createRequestTypes('SETTINGS', ['CLEAR', 'ADD']); diff --git a/app/actions/index.js b/app/actions/index.js index 7943fd9d0..9527cac88 100644 --- a/app/actions/index.js +++ b/app/actions/index.js @@ -34,13 +34,6 @@ export function setCurrentServer(server) { }; } -export function addSettings(settings) { - return { - type: types.ADD_SETTINGS, - payload: settings - }; -} - export function login() { return { type: 'LOGIN' diff --git a/app/actions/settings.js b/app/actions/settings.js new file mode 100644 index 000000000..381958c54 --- /dev/null +++ b/app/actions/settings.js @@ -0,0 +1,14 @@ +import { SETTINGS } from './actionsTypes'; + +export function addSettings(settings) { + return { + type: SETTINGS.ADD, + payload: settings + }; +} + +export function clearSettings() { + return { + type: SETTINGS.CLEAR + }; +} diff --git a/app/constants/types.js b/app/constants/types.js index 0a7ec86c8..d6bc3731d 100644 --- a/app/constants/types.js +++ b/app/constants/types.js @@ -1,3 +1,4 @@ export const SET_CURRENT_SERVER = 'SET_CURRENT_SERVER'; export const SET_CUSTOM_EMOJIS = 'SET_CUSTOM_EMOJIS'; export const ADD_SETTINGS = 'ADD_SETTINGS'; +export const CLEAR_SETTINGS = 'CLEAR_SETTINGS'; diff --git a/app/lib/methods/getSettings.js b/app/lib/methods/getSettings.js index 3b0d750be..35cd20ade 100644 --- a/app/lib/methods/getSettings.js +++ b/app/lib/methods/getSettings.js @@ -2,9 +2,9 @@ import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { Q } from '@nozbe/watermelondb'; +import { addSettings, clearSettings } from '../../actions/settings'; import RocketChat from '../rocketchat'; import reduxStore from '../createStore'; -import * as actions from '../../actions'; import settings from '../../constants/settings'; import log from '../../utils/log'; import database from '../database'; @@ -58,7 +58,8 @@ export async function getLoginSettings({ server }) { const result = await fetch(`${ server }/api/v1/settings.public?query={"_id":{"$in":${ settingsParams }}}`).then(response => response.json()); if (result.success && result.settings.length) { - reduxStore.dispatch(actions.addSettings(this.parseSettings(this._prepareSettings(result.settings)))); + reduxStore.dispatch(clearSettings()); + reduxStore.dispatch(addSettings(this.parseSettings(this._prepareSettings(result.settings)))); } } catch (e) { log(e); @@ -77,7 +78,7 @@ export async function setSettings() { valueAsArray: item.valueAsArray, _updatedAt: item._updatedAt })); - reduxStore.dispatch(actions.addSettings(RocketChat.parseSettings(parsed.slice(0, parsed.length)))); + reduxStore.dispatch(addSettings(RocketChat.parseSettings(parsed.slice(0, parsed.length)))); } export default async function() { @@ -94,7 +95,7 @@ export default async function() { const filteredSettings = this._prepareSettings(data); const filteredSettingsIds = filteredSettings.map(s => s._id); - reduxStore.dispatch(actions.addSettings(this.parseSettings(filteredSettings))); + reduxStore.dispatch(addSettings(this.parseSettings(filteredSettings))); InteractionManager.runAfterInteractions(async() => { // filter server info const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id)); diff --git a/app/reducers/index.js b/app/reducers/index.js index 9f94eb7a4..dead110fb 100644 --- a/app/reducers/index.js +++ b/app/reducers/index.js @@ -1,5 +1,5 @@ import { combineReducers } from 'redux'; -import settings from './reducers'; +import settings from './settings'; import login from './login'; import meteor from './connect'; import room from './room'; diff --git a/app/reducers/initialState.js b/app/reducers/initialState.js deleted file mode 100644 index ecf309b54..000000000 --- a/app/reducers/initialState.js +++ /dev/null @@ -1,5 +0,0 @@ -export default { - server: null, - login: {}, - settings: {} -}; diff --git a/app/reducers/reducers.js b/app/reducers/reducers.js deleted file mode 100644 index e865ecd6a..000000000 --- a/app/reducers/reducers.js +++ /dev/null @@ -1,12 +0,0 @@ -import * as types from '../constants/types'; -import initialState from './initialState'; - -export default function settings(state = initialState.settings, action) { - if (action.type === types.ADD_SETTINGS) { - return { - ...state, - ...action.payload - }; - } - return state; -} diff --git a/app/reducers/rootReducer.js b/app/reducers/rootReducer.js deleted file mode 100644 index 7c29d61c9..000000000 --- a/app/reducers/rootReducer.js +++ /dev/null @@ -1,10 +0,0 @@ -import { combineReducers } from 'redux'; -import * as reducers from './reducers'; -import * as login from './login'; -import * as connect from './connect'; - -const rootReducer = combineReducers({ - ...reducers, ...login, ...connect -}); - -export default rootReducer; diff --git a/app/reducers/settings.js b/app/reducers/settings.js new file mode 100644 index 000000000..9e037b1d0 --- /dev/null +++ b/app/reducers/settings.js @@ -0,0 +1,17 @@ +import { SETTINGS } from '../actions/actionsTypes'; + +const initialState = {}; + +export default (state = initialState, action) => { + switch (action.type) { + case SETTINGS.ADD: + return { + ...state, + ...action.payload + }; + case SETTINGS.CLEAR: + return initialState; + default: + return state; + } +}; diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index 9d4c04b1b..23a716bc6 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -13,6 +13,7 @@ import { serverFailure, selectServerRequest, selectServerSuccess, selectServerFailure } from '../actions/server'; import { setUser } from '../actions/login'; +import { clearSettings } from '../actions/settings'; import RocketChat from '../lib/rocketchat'; import database from '../lib/database'; import log, { logServerVersion } from '../utils/log'; @@ -94,6 +95,8 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch const basicAuth = yield RNUserDefaults.get(`${ BASIC_AUTH_KEY }-${ server }`); setBasicAuth(basicAuth); + yield put(clearSettings()); + if (user) { yield RocketChat.connect({ server, user, logoutOnError: true }); yield put(setUser(user));