From b6d6575543fe2aea2fc8e3f9b317d5415cddc40f Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Wed, 10 Apr 2019 19:47:55 +0200 Subject: [PATCH] Refactor reducers --- app/src/reducers/Global.ts | 65 +++++++++++++++++++++++++++++ app/src/reducers/index.ts | 83 ++------------------------------------ 2 files changed, 68 insertions(+), 80 deletions(-) create mode 100644 app/src/reducers/Global.ts diff --git a/app/src/reducers/Global.ts b/app/src/reducers/Global.ts new file mode 100644 index 0000000..1afa893 --- /dev/null +++ b/app/src/reducers/Global.ts @@ -0,0 +1,65 @@ +import { Action, Reducer } from 'redux' +import { Record } from 'immutable' +import { trackEvent } from '../utils/tracking' + +export enum ActionTypes { + showUpdateNotification = 'SHOW_UPDATE_NOTIFICATION', + showUpdateDetails = 'SHOW_UPDATE_DETAILS', + showError = 'SHOW_ERROR', + showNotification = 'SHOW_NOTIFICATION', + didLaunch = 'DID_LAUNCH', + toggleSettingsVisibility = 'TOGGLE_SETTINGS_VISIBILITY', +} + +export interface GlobalAction extends Action { + type: ActionTypes, + showUpdateNotification?: boolean + showUpdateDetails?: boolean + error?: string + notification?: string + settingsVisible: boolean +} + +export interface GlobalState { + showUpdateNotification?: boolean + showUpdateDetails: boolean + error?: string + notification?: string + launching: boolean +} + +const initialGlobalState = Record({ + showUpdateNotification: false, + showUpdateDetails: false, + error: undefined, + notification: undefined, + launching: true, +}) + +export const globalState: Reducer, GlobalAction> = (state = initialGlobalState(), action): Record => { + trackEvent(action.type) + console.log(action.type) + + switch (action.type) { + case ActionTypes.showUpdateNotification: + return state.set('showUpdateNotification', action.showUpdateNotification) + + case ActionTypes.showError: + return state.set('error', action.error) + + case ActionTypes.showNotification: + return state.set('notification', action.notification) + + case ActionTypes.didLaunch: + return state.set('launching', false) + + case ActionTypes.showUpdateDetails: + if (action.showUpdateDetails === undefined) { + return state + } + return state.set('showUpdateDetails', action.showUpdateDetails) + + default: + return state + } +} \ No newline at end of file diff --git a/app/src/reducers/index.ts b/app/src/reducers/index.ts index c3a6f44..cf57cf6 100644 --- a/app/src/reducers/index.ts +++ b/app/src/reducers/index.ts @@ -1,28 +1,12 @@ -import { Action, combineReducers, Reducer } from 'redux' +import { combineReducers } from 'redux' import { connectionManagerReducer, ConnectionManagerState } from './ConnectionManager' import { connectionReducer, ConnectionState } from './Connection' +import { GlobalState, globalState } from './Global' import { publishReducer, PublishState } from './Publish' import { Record } from 'immutable' import { settingsReducer, SettingsState } from './Settings' -import { trackEvent } from '../utils/tracking' import { treeReducer, TreeState } from './Tree' -export enum ActionTypes { - showUpdateNotification = 'SHOW_UPDATE_NOTIFICATION', - showUpdateDetails = 'SHOW_UPDATE_DETAILS', - showError = 'SHOW_ERROR', - showNotification = 'SHOW_NOTIFICATION', - didLaunch = 'DID_LAUNCH', -} - -export interface CustomAction extends Action { - type: ActionTypes, - showUpdateNotification?: boolean - showUpdateDetails?: boolean - error?: string - notification?: string -} - export interface AppState { globalState: GlobalState tree: TreeState @@ -32,66 +16,7 @@ export interface AppState { connectionManager: ConnectionManagerState } -export interface GlobalState { - showUpdateNotification?: boolean - showUpdateDetails: boolean - error?: string - notification?: string - launching: boolean -} - -const initialGlobalState: GlobalState = { - showUpdateDetails: false, - launching: true, -} - -const globalState: Reducer = (state = initialGlobalState, action) => { - if (!state) { - throw Error('No initial state') - } - trackEvent(action.type) - console.log(action.type) - switch (action.type) { - case ActionTypes.showUpdateNotification: - return { - ...state, - showUpdateNotification: action.showUpdateNotification, - } - - case ActionTypes.showError: - return { - ...state, - error: action.error, - } - - case ActionTypes.showNotification: - console.log(action) - return { - ...state, - notification: action.notification, - } - - case ActionTypes.didLaunch: - return { - ...state, - launching: false, - } - - case ActionTypes.showUpdateDetails: - if (action.showUpdateDetails === undefined) { - return state - } - return { - ...state, - showUpdateDetails: action.showUpdateDetails, - } - - default: - return state - } -} - -const reducer = combineReducers({ +export default combineReducers({ globalState, publish: publishReducer, connection: connectionReducer, @@ -99,5 +24,3 @@ const reducer = combineReducers({ tree: treeReducer, connectionManager: connectionManagerReducer, }) - -export default reducer