From 4249eef6539cd2f5b9e05852f4e56e36a5a25853 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 14 Jan 2022 18:00:04 -0300 Subject: [PATCH 1/3] chore: migrate settings to typescript --- app/actions/settings.js | 21 -------------- app/actions/settings.ts | 34 +++++++++++++++++++++++ app/reducers/settings.test.ts | 31 +++++++++++++++++++++ app/reducers/{settings.js => settings.ts} | 8 ++++-- 4 files changed, 71 insertions(+), 23 deletions(-) delete mode 100644 app/actions/settings.js create mode 100644 app/actions/settings.ts create mode 100644 app/reducers/settings.test.ts rename app/reducers/{settings.js => settings.ts} (57%) diff --git a/app/actions/settings.js b/app/actions/settings.js deleted file mode 100644 index 6fae375b..00000000 --- a/app/actions/settings.js +++ /dev/null @@ -1,21 +0,0 @@ -import { SETTINGS } from './actionsTypes'; - -export function addSettings(settings) { - return { - type: SETTINGS.ADD, - payload: settings - }; -} - -export function updateSettings(id, value) { - return { - type: SETTINGS.UPDATE, - payload: { id, value } - }; -} - -export function clearSettings() { - return { - type: SETTINGS.CLEAR - }; -} diff --git a/app/actions/settings.ts b/app/actions/settings.ts new file mode 100644 index 00000000..6f28570b --- /dev/null +++ b/app/actions/settings.ts @@ -0,0 +1,34 @@ +import { Action } from 'redux'; + +import { ISettings } from '../reducers/settings'; +import { SETTINGS } from './actionsTypes'; + +interface IAddSettings extends Action { + payload: ISettings; +} + +interface IUpdateSettings extends Action { + payload: { id: string; value: string }; +} + +export type IActionSettings = IAddSettings & IUpdateSettings; + +export function addSettings(settings: ISettings): IAddSettings { + return { + type: SETTINGS.ADD, + payload: settings + }; +} + +export function updateSettings(id: string, value: string): IUpdateSettings { + return { + type: SETTINGS.UPDATE, + payload: { id, value } + }; +} + +export function clearSettings(): Action { + return { + type: SETTINGS.CLEAR + }; +} diff --git a/app/reducers/settings.test.ts b/app/reducers/settings.test.ts new file mode 100644 index 00000000..da93ce3c --- /dev/null +++ b/app/reducers/settings.test.ts @@ -0,0 +1,31 @@ +import { addSettings, clearSettings, updateSettings } from '../actions/settings'; +import { mockedStore } from './mockedStore'; +import { initialState } from './settings'; + +describe('test settings reducer', () => { + it('should return initial state', () => { + const state = mockedStore.getState().settings; + expect(state).toEqual(initialState); + }); + + const settings = { API_Use_REST_For_DDP_Calls: true, FileUpload_MaxFileSize: 600857600, Jitsi_URL_Room_Prefix: 'RocketChat' }; + + it('should return modified store after call addSettings action', () => { + mockedStore.dispatch(addSettings(settings)); + const state = mockedStore.getState().settings; + expect(state).toEqual(settings); + }); + + it('should return correctly settings after call updateSettings action', () => { + const id = 'Jitsi_URL_Room_Prefix'; + mockedStore.dispatch(updateSettings(id, 'ChatRocket')); + const state = mockedStore.getState().settings; + expect(state[id]).toEqual('ChatRocket'); + }); + + it('should return initial state after clearSettings', () => { + mockedStore.dispatch(clearSettings()); + const state = mockedStore.getState().settings; + expect(state).toEqual({}); + }); +}); diff --git a/app/reducers/settings.js b/app/reducers/settings.ts similarity index 57% rename from app/reducers/settings.js rename to app/reducers/settings.ts index 6e9ab500..bcaad301 100644 --- a/app/reducers/settings.js +++ b/app/reducers/settings.ts @@ -1,8 +1,12 @@ +import { IActionSettings } from '../actions/settings'; import { SETTINGS } from '../actions/actionsTypes'; -const initialState = {}; +// TODO UPDATE SETTINGS TYPE +export type ISettings = Record; -export default (state = initialState, action) => { +export const initialState: ISettings = {}; + +export default (state = initialState, action: IActionSettings): ISettings => { switch (action.type) { case SETTINGS.ADD: return { From 635d6bf6f399d6c1470ea38b38a59bab1726f314 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 14 Jan 2022 18:03:16 -0300 Subject: [PATCH 2/3] chore: add interface to IStateAplication --- app/definitions/redux/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/definitions/redux/index.ts b/app/definitions/redux/index.ts index e95763e2..677519c5 100644 --- a/app/definitions/redux/index.ts +++ b/app/definitions/redux/index.ts @@ -1,11 +1,13 @@ -import { TActionSelectedUsers } from '../../actions/selectedUsers'; import { TActionActiveUsers } from '../../actions/activeUsers'; +import { TActionSelectedUsers } from '../../actions/selectedUsers'; +import { IActionSettings } from '../../actions/settings'; // REDUCERS import { IActiveUsers } from '../../reducers/activeUsers'; import { ISelectedUsers } from '../../reducers/selectedUsers'; +import { ISettings } from '../../reducers/settings'; export interface IApplicationState { - settings: any; + settings: ISettings; login: any; meteor: any; server: any; @@ -28,4 +30,4 @@ export interface IApplicationState { roles: any; } -export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers; +export type TApplicationActions = TActionActiveUsers & TActionSelectedUsers & IActionSettings; From 4fad0cafad08fef1d2aacbdf541ee3a0e1d650e2 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Thu, 20 Jan 2022 14:41:04 -0300 Subject: [PATCH 3/3] chore: update settings value types --- app/actions/settings.ts | 6 +++--- app/reducers/settings.ts | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/actions/settings.ts b/app/actions/settings.ts index 6f28570b..77b8dcc7 100644 --- a/app/actions/settings.ts +++ b/app/actions/settings.ts @@ -1,6 +1,6 @@ import { Action } from 'redux'; -import { ISettings } from '../reducers/settings'; +import { ISettings, TSettings } from '../reducers/settings'; import { SETTINGS } from './actionsTypes'; interface IAddSettings extends Action { @@ -8,7 +8,7 @@ interface IAddSettings extends Action { } interface IUpdateSettings extends Action { - payload: { id: string; value: string }; + payload: { id: string; value: TSettings }; } export type IActionSettings = IAddSettings & IUpdateSettings; @@ -20,7 +20,7 @@ export function addSettings(settings: ISettings): IAddSettings { }; } -export function updateSettings(id: string, value: string): IUpdateSettings { +export function updateSettings(id: string, value: TSettings): IUpdateSettings { return { type: SETTINGS.UPDATE, payload: { id, value } diff --git a/app/reducers/settings.ts b/app/reducers/settings.ts index bcaad301..028431ed 100644 --- a/app/reducers/settings.ts +++ b/app/reducers/settings.ts @@ -1,8 +1,9 @@ import { IActionSettings } from '../actions/settings'; import { SETTINGS } from '../actions/actionsTypes'; -// TODO UPDATE SETTINGS TYPE -export type ISettings = Record; +export type TSettings = string | number | boolean; + +export type ISettings = Record; export const initialState: ISettings = {};