From 91c9f9f3dd8eba8587084d1e688fe2b78ac4cc6d Mon Sep 17 00:00:00 2001 From: Gregor Herdmann Date: Tue, 3 Apr 2018 21:59:03 +0200 Subject: [PATCH] refactor: Reducer and action cleanup --- electron/renderer/src/actions/index.js | 66 ++++++++++--------- electron/renderer/src/components/App.js | 12 ++-- electron/renderer/src/components/IsOnline.js | 6 +- .../renderer/src/components/PersonalIcon.js | 2 +- electron/renderer/src/components/Sidebar.js | 6 +- electron/renderer/src/components/TeamIcon.js | 4 +- electron/renderer/src/components/Webview.js | 6 +- electron/renderer/src/components/Webviews.js | 6 +- .../src/components/context/AddAccountMenu.js | 4 +- .../context/AddAccountMenuTrigger.js | 2 +- .../src/components/context/ContextMenu.js | 23 ++++--- .../src/components/context/EditAccountMenu.js | 2 +- .../src/containers/WebviewsContainer.js | 2 +- electron/renderer/src/lib/accentColor.js | 4 +- electron/renderer/src/lib/localStorage.js | 11 ++-- electron/renderer/src/lib/locale.js | 4 +- electron/renderer/src/lib/util.js | 8 +-- .../src/lib/verifyObjectProperties.js | 10 +-- ...ccounts.spec.js => accountReducer.spec.js} | 59 +++++++++-------- .../{accounts.js => accountReducer.js} | 63 ++++++++++-------- .../{contextMenu.js => contextMenuReducer.js} | 24 ++++--- electron/renderer/src/reducers/index.js | 10 +-- 22 files changed, 174 insertions(+), 160 deletions(-) rename electron/renderer/src/reducers/__tests__/{accounts.spec.js => accountReducer.spec.js} (69%) rename electron/renderer/src/reducers/{accounts.js => accountReducer.js} (52%) rename electron/renderer/src/reducers/{contextMenu.js => contextMenuReducer.js} (79%) diff --git a/electron/renderer/src/actions/index.js b/electron/renderer/src/actions/index.js index 6ad42397f40..3a70fba618b 100644 --- a/electron/renderer/src/actions/index.js +++ b/electron/renderer/src/actions/index.js @@ -2,10 +2,10 @@ import uuid from 'uuid/v4'; import verifyObjectProperties from '../lib/verifyObjectProperties'; export const ADD_ACCOUNT = 'ADD_ACCOUNT'; +export const DELETE_ACCOUNT = 'DELETE_ACCOUNT'; export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT'; export const UPDATE_ACCOUNT = 'UPDATE_ACCOUNT'; export const UPDATE_ACCOUNT_BADGE = 'UPDATE_ACCOUNT_BADGE'; -export const DELETE_ACCOUNT = 'DELETE_ACCOUNT'; export const UPDATE_ACCOUNT_LIFECYCLE = 'UPDATE_ACCOUNT_LIFECYCLE'; export const addAccount = (withSession = true) => { @@ -16,54 +16,58 @@ export const addAccount = (withSession = true) => { }; }; -export const updateAccount = (id, data) => { +export const deleteAccount = id => { return { - data, id, - type: UPDATE_ACCOUNT, + type: DELETE_ACCOUNT, }; }; -export const updateAccountLifecycle = (id, data) => { +export const switchAccount = id => { return { - data, id, - type: UPDATE_ACCOUNT_LIFECYCLE, + type: SWITCH_ACCOUNT, }; }; -export const switchAccount = id => { +export const updateAccount = (id, data) => { return { + data, id, - type: SWITCH_ACCOUNT, + type: UPDATE_ACCOUNT, }; }; -export const updateAccountBadge = (id, count) => { +export const updateAccountLifecycle = (id, data) => { return { - count, + data, id, - type: UPDATE_ACCOUNT_BADGE, + type: UPDATE_ACCOUNT_LIFECYCLE, }; }; -export const deleteAccount = id => { +export const updateAccountBadge = (id, count) => { return { + count, id, - type: DELETE_ACCOUNT, + type: UPDATE_ACCOUNT_BADGE, }; }; +export const HIDE_CONTEXT_MENUS = 'HIDE_CONTEXT_MENUS'; +export const TOGGLE_ADD_ACCOUNT_VISIBILITY = 'TOGGLE_ADD_ACCOUNT_VISIBILITY'; +export const TOGGLE_EDIT_ACCOUNT_VISIBILITY = 'TOGGLE_EDIT_ACCOUNT_VISIBILITY'; + export const setAccountContextHidden = () => { return { - type: 'HIDE_CONTEXT_MENUS', + type: HIDE_CONTEXT_MENUS, }; }; export const toggleAddAccountMenuVisibility = (x, y) => { return { - payload: { position: { x, y } }, - type: 'TOGGLE_ADD_ACCOUNT_VISIBILITY', + payload: {position: {x, y}}, + type: TOGGLE_ADD_ACCOUNT_VISIBILITY, }; }; @@ -80,10 +84,10 @@ export const toggleEditAccountMenuVisibility = ( accountId, isAtLeastAdmin, lifecycle, - position: { x, y }, + position: {x, y}, sessionId, }, - type: 'TOGGLE_EDIT_ACCOUNT_VISIBILITY', + type: TOGGLE_EDIT_ACCOUNT_VISIBILITY, }; }; @@ -102,6 +106,18 @@ export const abortAccountCreation = id => { }; }; +export const addAccountWithSession = () => { + return (dispatch, getState) => { + const hasReachedAccountLimit = getState().accounts.length >= 3; + + if (hasReachedAccountLimit) { + console.warn('Reached number of maximum accounts'); + } else { + dispatch(addAccount()); + } + }; +}; + export const updateAccountData = (id, data) => { return (dispatch, getState) => { const validatedAccountData = verifyObjectProperties(data, { @@ -135,15 +151,3 @@ export const updateAccountBadgeCount = (id, count) => { } }; }; - -export const addAccountWithSession = () => { - return (dispatch, getState) => { - const hasReachedAccountLimit = getState().accounts.length >= 3; - - if (hasReachedAccountLimit) { - console.warn('Reached number of maximum accounts'); - } else { - dispatch(addAccount()); - } - }; -}; diff --git a/electron/renderer/src/components/App.js b/electron/renderer/src/components/App.js index 60a89406557..5e208e221fc 100644 --- a/electron/renderer/src/components/App.js +++ b/electron/renderer/src/components/App.js @@ -30,11 +30,11 @@ const App = props => (
{ - const modKeyPressed = (window.isMac && e.metaKey) || e.ctrlKey; - const isValidKey = ['1', '2', '3'].includes(e.key); - if (modKeyPressed && isValidKey && props.accountIds[e.key - 1]) { - props.switchAccount(props.accountIds[e.key - 1]); + onKeyDown={event => { + const modKeyPressed = (window.isMac && event.metaKey) || event.ctrlKey; + const isValidKey = ['1', '2', '3'].includes(event.key); + if (modKeyPressed && isValidKey && props.accountIds[event.key - 1]) { + props.switchAccount(props.accountIds[event.key - 1]); } }} > @@ -44,4 +44,4 @@ const App = props => ( ); -export default connect(({ accounts }) => ({ accountIds: accounts.map(account => account.id) }), { switchAccount })(App); +export default connect(({accounts}) => ({accountIds: accounts.map(account => account.id)}), {switchAccount})(App); diff --git a/electron/renderer/src/components/IsOnline.js b/electron/renderer/src/components/IsOnline.js index 09e9506d4c1..b6dd304b1d8 100644 --- a/electron/renderer/src/components/IsOnline.js +++ b/electron/renderer/src/components/IsOnline.js @@ -32,9 +32,9 @@ class IsOnline extends Component { componentDidMount() { if (this.state.isOnline === false) { - window.addEventListener('online', (event) => { - this.setState({ isOnline: true }); - }, { once: true }); + window.addEventListener('online', event => { + this.setState({isOnline: true}); + }, {once: true}); } } diff --git a/electron/renderer/src/components/PersonalIcon.js b/electron/renderer/src/components/PersonalIcon.js index 6c1170e0636..bb865227a9b 100644 --- a/electron/renderer/src/components/PersonalIcon.js +++ b/electron/renderer/src/components/PersonalIcon.js @@ -24,7 +24,7 @@ import { colorFromId } from '../lib/accentColor'; import './PersonalIcon.css'; -const PersonalIcon = ({ account, accentID, onClick }) => +const PersonalIcon = ({account, accentID, onClick}) =>
{account.visible &&
diff --git a/electron/renderer/src/components/Sidebar.js b/electron/renderer/src/components/Sidebar.js index f1d023fd39c..177593419dc 100644 --- a/electron/renderer/src/components/Sidebar.js +++ b/electron/renderer/src/components/Sidebar.js @@ -17,9 +17,9 @@ * */ -import { colorFromId } from '../lib/accentColor'; -import { connect } from 'react-redux'; -import { preventFocus } from '../lib/util'; +import {colorFromId} from '../lib/accentColor'; +import {connect} from 'react-redux'; +import {preventFocus} from '../lib/util'; import AddAccountMenuTrigger from './context/AddAccountMenuTrigger'; import AddAccountMenu from './context/AddAccountMenu'; import EditAccountMenu from './context/EditAccountMenu'; diff --git a/electron/renderer/src/components/TeamIcon.js b/electron/renderer/src/components/TeamIcon.js index d8a8b8630bb..3e4d8eff625 100644 --- a/electron/renderer/src/components/TeamIcon.js +++ b/electron/renderer/src/components/TeamIcon.js @@ -24,7 +24,7 @@ import { colorFromId } from '../lib/accentColor'; import './TeamIcon.css'; -const TeamIcon = ({ account, accentID }) => +const TeamIcon = ({account, accentID}) =>
{account.visible && @@ -34,7 +34,7 @@ const TeamIcon = ({ account, accentID }) => - { [...account.name][0] } + {[...account.name][0]}
; TeamIcon.propTypes = { diff --git a/electron/renderer/src/components/Webview.js b/electron/renderer/src/components/Webview.js index ba4e742d98b..261f07b4642 100644 --- a/electron/renderer/src/components/Webview.js +++ b/electron/renderer/src/components/Webview.js @@ -58,15 +58,15 @@ class Webview extends Component { } _focusWebview() { - const is_visible = this.props.visible === true; - if (is_visible) { + const isVisible = this.props.visible === true; + if (isVisible) { this.webview.focus(); } } render() { const {visible, partition, src, onPageTitleUpdated, onIpcMessage, ...validProps} = this.props; // eslint-disable-line no-unused-vars - return this.webview = webview } />; + return this.webview = webview} />; } } diff --git a/electron/renderer/src/components/Webviews.js b/electron/renderer/src/components/Webviews.js index c659e8ac48c..99e1cc500d4 100644 --- a/electron/renderer/src/components/Webviews.js +++ b/electron/renderer/src/components/Webviews.js @@ -17,7 +17,7 @@ * */ -import React, { Component } from 'react'; +import React, {Component} from 'react'; import Webview from './Webview'; @@ -82,7 +82,7 @@ class Webviews extends Component { window.sendBadgeCount(accumulatedCount); } - _onIpcMessage(account, { channel, args }) { + _onIpcMessage(account, {channel, args}) { switch (channel) { case 'notification-click': { this.props.switchAccount(account.id); @@ -111,7 +111,7 @@ class Webviews extends Component { } } - this.setState({ canDelete: { ...this.state.canDelete, [account.id]: this._canDeleteWebview(account) } }); + this.setState({canDelete: {...this.state.canDelete, [account.id]: this._canDeleteWebview(account)}}); } _onWebviewClose(account) { diff --git a/electron/renderer/src/components/context/AddAccountMenu.js b/electron/renderer/src/components/context/AddAccountMenu.js index 54f37982acb..a13223b9124 100644 --- a/electron/renderer/src/components/context/AddAccountMenu.js +++ b/electron/renderer/src/components/context/AddAccountMenu.js @@ -24,7 +24,7 @@ import ContextMenu from './ContextMenu'; import ContextMenuItem from './ContextMenuItem'; import { addAccountWithSession } from '../../actions/'; -function AddAccountMenu({ ...connected }) { +function AddAccountMenu({...connected}) { return ( window.open('https://wire.com/create-team/?pk_campaign=client&pk_kwd=desktop')}> @@ -35,4 +35,4 @@ function AddAccountMenu({ ...connected }) { ); } -export default connect(null, { addAccountWithSession })(AddAccountMenu); +export default connect(null, {addAccountWithSession})(AddAccountMenu); diff --git a/electron/renderer/src/components/context/AddAccountMenuTrigger.js b/electron/renderer/src/components/context/AddAccountMenuTrigger.js index 6273d745657..b6b55635124 100644 --- a/electron/renderer/src/components/context/AddAccountMenuTrigger.js +++ b/electron/renderer/src/components/context/AddAccountMenuTrigger.js @@ -19,7 +19,7 @@ import React from 'react'; -const AddAccountMenuTrigger = ({ onClick, forceVisible }) => ( +const AddAccountMenuTrigger = ({onClick, forceVisible}) => (
({ position: state.contextMenuState.position, }), - { setAccountContextHidden } + {setAccountContextHidden} )(ContextMenu); diff --git a/electron/renderer/src/components/context/EditAccountMenu.js b/electron/renderer/src/components/context/EditAccountMenu.js index c5621670ddf..aecf05dc410 100644 --- a/electron/renderer/src/components/context/EditAccountMenu.js +++ b/electron/renderer/src/components/context/EditAccountMenu.js @@ -63,7 +63,7 @@ function EditAccountMenu({ } export default connect( - ({ contextMenuState }) => ({ + ({contextMenuState}) => ({ accountId: contextMenuState.accountId, isAtLeastAdmin: contextMenuState.isAtLeastAdmin, lifecycle: contextMenuState.lifecycle, diff --git a/electron/renderer/src/containers/WebviewsContainer.js b/electron/renderer/src/containers/WebviewsContainer.js index ee1341d3002..7933d4f83fb 100644 --- a/electron/renderer/src/containers/WebviewsContainer.js +++ b/electron/renderer/src/containers/WebviewsContainer.js @@ -28,7 +28,7 @@ import { } from '../actions'; import Webviews from '../components/Webviews'; -const WebviewsContainer = connect(state => ({ accounts: state.accounts }), { +const WebviewsContainer = connect(state => ({accounts: state.accounts}), { abortAccountCreation, switchAccount, updateAccountBadgeCount, diff --git a/electron/renderer/src/lib/accentColor.js b/electron/renderer/src/lib/accentColor.js index c09fe50c326..bfa0866de9b 100644 --- a/electron/renderer/src/lib/accentColor.js +++ b/electron/renderer/src/lib/accentColor.js @@ -62,7 +62,7 @@ export const ACCENT_COLORS = [ VIOLET, ]; -export function colorFromId(id) { +export const colorFromId = id => { const accentColor = ACCENT_COLORS.find((color) => color.id === id); return accentColor && accentColor.color; -} \ No newline at end of file +}; diff --git a/electron/renderer/src/lib/localStorage.js b/electron/renderer/src/lib/localStorage.js index d3276c5756f..595d909acb7 100644 --- a/electron/renderer/src/lib/localStorage.js +++ b/electron/renderer/src/lib/localStorage.js @@ -17,13 +17,12 @@ * */ +const STATE_NAME = 'state'; + export const loadState = () => { try { - const serializedState = localStorage.getItem('state'); - if (serializedState === null) { - return undefined; - } - return JSON.parse(serializedState); + const serializedState = localStorage.getItem(STATE_NAME); + return !!serializedState ? JSON.parse(serializedState) : undefined; } catch (error) { console.error('ERROR: Failed to load state ', error.message); return undefined; @@ -33,7 +32,7 @@ export const loadState = () => { export const saveState = (state) => { try { const serializedState = JSON.stringify(state); - localStorage.setItem('state', serializedState); + localStorage.setItem(STATE_NAME, serializedState); } catch (error) { console.error('ERROR: Failed to save state ', error.message); } diff --git a/electron/renderer/src/lib/locale.js b/electron/renderer/src/lib/locale.js index 4ba1d3db039..faf11fe1d43 100644 --- a/electron/renderer/src/lib/locale.js +++ b/electron/renderer/src/lib/locale.js @@ -1,6 +1,4 @@ window.locStrings = window.locStrings || {}; window.locStringsDefault = window.locStringsDefault || {}; -export function getText(id) { - return locStrings[id] || locStringsDefault[id] || id; -} +export const getText = id => locStrings[id] || locStringsDefault[id] || id; diff --git a/electron/renderer/src/lib/util.js b/electron/renderer/src/lib/util.js index 765bb375568..783fa508ce1 100644 --- a/electron/renderer/src/lib/util.js +++ b/electron/renderer/src/lib/util.js @@ -19,10 +19,10 @@ export const noop = () => {}; -export function preventFocus(func = noop) { - return function(event) { +export const preventFocus = (fn = noop) => { + return event => { event.stopPropagation(); event.preventDefault(); - func(event); + fn(event); }; -} +}; diff --git a/electron/renderer/src/lib/verifyObjectProperties.js b/electron/renderer/src/lib/verifyObjectProperties.js index 5ed7d2901e8..c1186257b42 100644 --- a/electron/renderer/src/lib/verifyObjectProperties.js +++ b/electron/renderer/src/lib/verifyObjectProperties.js @@ -17,10 +17,10 @@ * */ -function is(type, obj) { - const getType = Object.prototype.toString.call(obj).slice(8, -1); - return obj && getType === type; -} +const isType = (type, object) => { + const getType = Object.prototype.toString.call(object).slice(8, -1); + return object && getType === type; +}; export default function(data, config) { const validatedData = {}; @@ -31,7 +31,7 @@ export default function(data, config) { return true; } - const isValid = is(config[key], data[key]); + const isValid = isType(config[key], data[key]); if (isValid) { validatedData[key] = data[key]; } diff --git a/electron/renderer/src/reducers/__tests__/accounts.spec.js b/electron/renderer/src/reducers/__tests__/accountReducer.spec.js similarity index 69% rename from electron/renderer/src/reducers/__tests__/accounts.spec.js rename to electron/renderer/src/reducers/__tests__/accountReducer.spec.js index c583f774b6d..76a3f82305b 100644 --- a/electron/renderer/src/reducers/__tests__/accounts.spec.js +++ b/electron/renderer/src/reducers/__tests__/accountReducer.spec.js @@ -17,7 +17,7 @@ * */ -import reducer from '../accounts'; +import accountReducer from '../accountReducer'; import { updateAccount, addAccount, @@ -28,11 +28,11 @@ import { describe('accounts reducer', () => { it('should return the initial state with one account', () => { - expect(reducer(undefined, {}).length).toEqual(1); + expect(accountReducer(undefined, {}).length).toEqual(1); }); it('should return a state with a new account', () => { - const state = [{ + const initialState = [{ accentID: undefined, badgeCount: 0, id: '046da4f1-39be-4b8b-823b-e71f12811454', @@ -43,16 +43,17 @@ describe('accounts reducer', () => { userID: undefined, visible: true, }]; - const newState = reducer(state, addAccount()); + const newState = accountReducer(initialState, addAccount()); + const [firstAccount, secondAccount] = newState; expect(newState.length).toEqual(2); - expect(newState[0].visible).toBeFalsy(); - expect(newState[1].visible).toBeTruthy(); - expect(newState[1].sessionID).toBeDefined(); + expect(firstAccount.visible).toBeFalsy(); + expect(secondAccount.visible).toBeTruthy(); + expect(secondAccount.sessionID).toBeDefined(); }); it('should return a state with a new account without a session', () => { - const state = [{ + const initialState = [{ accentID: undefined, badgeCount: 0, id: '046da4f1-39be-4b8b-823b-e71f12811454', @@ -63,16 +64,17 @@ describe('accounts reducer', () => { userID: undefined, visible: true, }]; - const newState = reducer(state, addAccount(false)); + const newState = accountReducer(initialState, addAccount(false)); + const [firstAccount, secondAccount] = newState; expect(newState.length).toEqual(2); - expect(newState[0].visible).toBeFalsy(); - expect(newState[1].visible).toBeTruthy(); - expect(newState[1].sessionID).not.toBeDefined(); + expect(firstAccount.visible).toBeFalsy(); + expect(secondAccount.visible).toBeTruthy(); + expect(secondAccount.sessionID).not.toBeDefined(); }); it('should return a state with only the specified account visible', () => { - const state = [{ + const initialState = [{ accentID: undefined, badgeCount: 0, id: '046da4f1-39be-4b8b-823b-e71f12811454', @@ -93,14 +95,14 @@ describe('accounts reducer', () => { userID: undefined, visible: false, }]; - const newState = reducer(state, switchAccount(state[1].id)); + const [firstAccount, secondAccount] = accountReducer(initialState, switchAccount(initialState[1].id)); - expect(newState[0].visible).toBeFalsy(); - expect(newState[1].visible).toBeTruthy(); + expect(firstAccount.visible).toBeFalsy(); + expect(secondAccount.visible).toBeTruthy(); }); it('should return a state with an updated account', () => { - const state = [{ + const initialState = [{ accentID: undefined, badgeCount: 0, id: '046da4f1-39be-4b8b-823b-e71f12811454', @@ -121,14 +123,15 @@ describe('accounts reducer', () => { userID: undefined, visible: false, }]; - const newState = reducer(state, updateAccount(state[0].id, { userID: 'f4b9a5d0-3e36-4e6f-a404-ba22d23e3730'})); + const accountData = {userID: 'f4b9a5d0-3e36-4e6f-a404-ba22d23e3730'}; + const [firstAccount, secondAccount] = accountReducer(initialState, updateAccount(initialState[0].id, accountData)); - expect(newState[0].userID).toEqual('f4b9a5d0-3e36-4e6f-a404-ba22d23e3730'); - expect(newState[1].userID).toBeUndefined(); + expect(firstAccount.userID).toEqual('f4b9a5d0-3e36-4e6f-a404-ba22d23e3730'); + expect(secondAccount.userID).toBeUndefined(); }); it('should return a state with an updated badge count', () => { - const state = [{ + const initialState = [{ accentID: undefined, badgeCount: 0, id: '046da4f1-39be-4b8b-823b-e71f12811454', @@ -149,14 +152,14 @@ describe('accounts reducer', () => { userID: undefined, visible: false, }]; - const newState = reducer(state, updateAccountBadge(state[1].id, 12)); + const [firstAccount, secondAccount] = accountReducer(initialState, updateAccountBadge(initialState[1].id, 12)); - expect(newState[0].badgeCount).toEqual(0); - expect(newState[1].badgeCount).toEqual(12); + expect(firstAccount.badgeCount).toEqual(0); + expect(secondAccount.badgeCount).toEqual(12); }); it('should return a state without the deleted account', () => { - const state = [{ + const initialState = [{ accentID: undefined, badgeCount: 0, id: '046da4f1-39be-4b8b-823b-e71f12811454', @@ -177,10 +180,10 @@ describe('accounts reducer', () => { userID: undefined, visible: false, }]; - const newState = reducer(state, deleteAccount(state[0].id)); + const newState = accountReducer(initialState, deleteAccount(initialState[0].id)); + const [firstAccount] = newState; expect(newState.length).toEqual(1); - expect(newState[0].id).toEqual('d01eb964-bf56-4668-8883-dc248b58b1ca'); + expect(firstAccount.id).toEqual('d01eb964-bf56-4668-8883-dc248b58b1ca'); }); - }); diff --git a/electron/renderer/src/reducers/accounts.js b/electron/renderer/src/reducers/accountReducer.js similarity index 52% rename from electron/renderer/src/reducers/accounts.js rename to electron/renderer/src/reducers/accountReducer.js index 76c4512be7d..c54c9f36515 100644 --- a/electron/renderer/src/reducers/accounts.js +++ b/electron/renderer/src/reducers/accountReducer.js @@ -18,8 +18,9 @@ */ import uuid from 'uuid/v4'; +import * as ActionCreator from '../actions'; -function createAccount(sessionID) { +const createAccount = sessionId => { return { accentID: undefined, badgeCount: 0, @@ -27,50 +28,54 @@ function createAccount(sessionID) { lifecycle: undefined, name: undefined, picture: undefined, - sessionID: sessionID, + sessionID: sessionId, teamID: undefined, userID: undefined, visible: true, }; -} +}; -const accounts = (state = [createAccount()], action) => { +const accountReducer = (state = [createAccount()], action) => { switch (action.type) { - case 'ADD_ACCOUNT': - return [ - ...state.map(account => ({ ...account, visible: false })), - createAccount(action.sessionID), - ]; - case 'UPDATE_ACCOUNT': + case ActionCreator.ADD_ACCOUNT: { + return state.map(account => ({...account, visible: false})).push(createAccount(action.sessionID)); + } + + case ActionCreator.DELETE_ACCOUNT: { + return state.filter(account => account.id !== action.id); + } + + case ActionCreator.SWITCH_ACCOUNT: { return state.map(account => { - return account.id === action.id - ? { ...account, ...action.data } - : account; + const isMatchingAccount = account.id === action.id; + return {...account, visible: isMatchingAccount}; }); - case 'UPDATE_ACCOUNT_BADGE': + } + + case ActionCreator.UPDATE_ACCOUNT: { return state.map(account => { - return account.id === action.id - ? { ...account, badgeCount: action.count } - : account; + const isMatchingAccount = account.id === action.id; + return isMatchingAccount ? {...account, ...action.data} : account; }); - case 'UPDATE_ACCOUNT_LIFECYCLE': + } + + case ActionCreator.UPDATE_ACCOUNT_BADGE: { return state.map(account => { - return account.id === action.id - ? { ...account, lifecycle: action.data } - : account; + const isMatchingAccount = account.id === action.id; + return isMatchingAccount ? {...account, badgeCount: action.count} : account; }); - case 'SWITCH_ACCOUNT': + } + + case ActionCreator.UPDATE_ACCOUNT_LIFECYCLE: { return state.map(account => { - return { - ...account, - visible: account.id === action.id, - }; + const isMatchingAccount = account.id === action.id; + return isMatchingAccount ? {...account, lifecycle: action.data} : account; }); - case 'DELETE_ACCOUNT': - return state.filter(account => account.id !== action.id); + } + default: return state; } }; -export default accounts; +export default accountReducer; diff --git a/electron/renderer/src/reducers/contextMenu.js b/electron/renderer/src/reducers/contextMenuReducer.js similarity index 79% rename from electron/renderer/src/reducers/contextMenu.js rename to electron/renderer/src/reducers/contextMenuReducer.js index 2e049298816..c2f4340510e 100644 --- a/electron/renderer/src/reducers/contextMenu.js +++ b/electron/renderer/src/reducers/contextMenuReducer.js @@ -17,7 +17,9 @@ * */ -const defaultState = { +import * as ActionCreator from '../actions'; + +const DEFAULT_STATE = { accountId: '', isAddAccountMenuVisible: false, isAtLeastAdmin: false, @@ -27,20 +29,22 @@ const defaultState = { sessionId: '', }; -const accounts = (state = defaultState, action) => { +const contextMenuReducer = (state = DEFAULT_STATE, action) => { switch (action.type) { - case 'HIDE_CONTEXT_MENUS': - return { - ...defaultState, - }; - case 'TOGGLE_ADD_ACCOUNT_VISIBILITY': + case ActionCreator.HIDE_CONTEXT_MENUS: { + return {...DEFAULT_STATE}; + } + + case ActionCreator.TOGGLE_ADD_ACCOUNT_VISIBILITY: { return { ...state, isAddAccountMenuVisible: !state.isAddAccountMenuVisible, isEditAccountMenuVisible: false, position: action.payload.position, }; - case 'TOGGLE_EDIT_ACCOUNT_VISIBILITY': + } + + case ActionCreator.TOGGLE_EDIT_ACCOUNT_VISIBILITY: { return { ...state, accountId: action.payload.accountId, @@ -51,9 +55,11 @@ const accounts = (state = defaultState, action) => { position: action.payload.position, sessionId: action.payload.sessionId, }; + } + default: return state; } }; -export default accounts; +export default contextMenuReducer; diff --git a/electron/renderer/src/reducers/index.js b/electron/renderer/src/reducers/index.js index 51d776a8f15..2ce7d286fb5 100644 --- a/electron/renderer/src/reducers/index.js +++ b/electron/renderer/src/reducers/index.js @@ -17,13 +17,13 @@ * */ -import { combineReducers } from 'redux'; -import accounts from './accounts'; -import contextMenuState from './contextMenu'; +import {combineReducers} from 'redux'; +import accountsReducer from './accountReducer'; +import contextMenuReducer from './contextMenuReducer'; const store = combineReducers({ - accounts, - contextMenuState, + accountsReducer, + contextMenuReducer, }); export default store;