Skip to content

Commit

Permalink
refactor: improve HWRedux store
Browse files Browse the repository at this point in the history
  • Loading branch information
soroushm committed Feb 16, 2023
1 parent f06c40e commit c7618dd
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 108 deletions.
10 changes: 5 additions & 5 deletions src/modules/hardwareWallet/hooks/useHwListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { useDispatch } from 'react-redux';
import { useEffect } from 'react';
import { IPC_MESSAGES } from '@libs/hwServer/constants';
import {
setDeviceListChanged,
setDeviceUpdated,
} from 'src/modules/hardwareWallet/store/hardwareWalletActions';
setHardwareWalletDevices,
setCurrentDevice,
} from 'src/modules/hardwareWallet/store/actions';

const { DEVICE_LIST_CHANGED, DEVICE_UPDATE } = IPC_MESSAGES;

Expand All @@ -17,10 +17,10 @@ function useHwListener() {

useEffect(() => {
ipc.on(DEVICE_LIST_CHANGED, (action, data) => {
dispatch(setDeviceListChanged(data));
dispatch(setHardwareWalletDevices(data));
});
ipc.on(DEVICE_UPDATE, (action, data) => {
dispatch(setDeviceUpdated(data));
dispatch(setCurrentDevice(data));
});
}, []);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import actionTypes from './actionTypes';

export const storeHWAccounts = (accounts) => ({
export const setHWAccounts = (accounts) => ({
type: actionTypes.storeHWAccounts,
accounts,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { hwAccounts } from '@hardwareWallet/__fixtures__/hwAccounts';
import { setHWAccounts, removeHWAccounts } from './accountsActions';
import actionTypes from './actionTypes';
import { storeHWAccounts, removeHWAccounts } from './actions';
import { hwAccounts } from '../../__fixtures__/hwAccounts';

describe('actions: hardware wallet', () => {
it('stores the list of accounts', () => {
const expectedAction = {
type: actionTypes.storeHWAccounts,
accounts: hwAccounts,
};
expect(storeHWAccounts(hwAccounts)).toEqual(expectedAction);
expect(setHWAccounts(hwAccounts)).toEqual(expectedAction);
});
it('removes the list of accounts', () => {
const expectedAction = {
Expand Down
9 changes: 7 additions & 2 deletions src/modules/hardwareWallet/store/actions/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { IPC_MESSAGES } from '@libs/hwServer/constants';

const {DEVICE_LIST_CHANGED, DEVICE_UPDATE} = IPC_MESSAGES
const actionTypes = {
storeHWAccounts: 'STORE_HW_ACCOUNTS',
removeHWAccounts: 'REMOVE_HW_ACCOUNTS',
storeHWAccounts: 'HW_ACCOUNTS_ADD',
removeHWAccounts: 'HW_ACCOUNTS_REMOVE',
changeDevices: `HW_${DEVICE_LIST_CHANGED}`,
deviceUpdate: `HW_${DEVICE_UPDATE}`,
};

export default actionTypes;
13 changes: 13 additions & 0 deletions src/modules/hardwareWallet/store/actions/devicesActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IPC_MESSAGES } from '@libs/hwServer/constants';

const { DEVICE_LIST_CHANGED, DEVICE_UPDATE } = IPC_MESSAGES;

export const setHardwareWalletDevices = (devices) => ({
type: `HW_${DEVICE_LIST_CHANGED}`,
devices,
});

export const setCurrentDevice = ({device}) => ({
type: `HW_${DEVICE_UPDATE}`,
device,
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IPC_MESSAGES } from '@libs/hwServer/constants';
import {
setDeviceListChanged,
setDeviceUpdated,
} from './hardwareWalletActions';
setHardwareWalletDevices,
setCurrentDevice,
} from './devicesActions';

const { DEVICE_LIST_CHANGED, DEVICE_UPDATE } = IPC_MESSAGES;

Expand All @@ -19,7 +19,7 @@ describe('hardwareWalletActions', () => {
payload: devices,
};

expect(setDeviceListChanged(devices)).toEqual(expectedAction);
expect(setHardwareWalletDevices(devices)).toEqual(expectedAction);
});

it('should create an action to update activeHardwareDeviceId', () => {
Expand All @@ -28,6 +28,6 @@ describe('hardwareWalletActions', () => {
payload: '1',
};

expect(setDeviceUpdated('1')).toEqual(expectedAction);
expect(setCurrentDevice('1')).toEqual(expectedAction);
});
});
2 changes: 2 additions & 0 deletions src/modules/hardwareWallet/store/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './accountsActions'
export * from './devicesActions'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
selectActiveHardwareDevice,
selectActiveHardwareDeviceId,
selectHardwareDevices,
} from './hardwareWalletSelectors';
} from '../selectors';

describe('HardwareWallet selectors', () => {
const mockState = {
Expand Down
13 changes: 0 additions & 13 deletions src/modules/hardwareWallet/store/hardwareWalletActions.js

This file was deleted.

28 changes: 0 additions & 28 deletions src/modules/hardwareWallet/store/hardwareWalletReducer.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/modules/hardwareWallet/store/hardwareWalletSelectors.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/modules/hardwareWallet/store/reducers/acountsReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import actionTypes from '../actions/actionTypes';

const initAccounts = [];

export const accounts = (state = initAccounts, {type, accounts: hwAccount= initAccounts}) => {
switch (type) {
case actionTypes.storeHWAccounts:
return hwAccount;
case actionTypes.removeHWAccounts:
return initAccounts;
default:
return state;
}
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { hwAccounts } from '../../__fixtures__/hwAccounts';
import actionTypes from '../actions/actionTypes';
import hardwareWallet from './reducers';
import { accounts } from './acountsReducers';

describe('reducer: hardware wallet', () => {
const testHWWallet = {
const testHWWalletAccount = {
hw: {
deviceId: '20231',
model: 'Nano S',
Expand All @@ -20,31 +20,31 @@ describe('reducer: hardware wallet', () => {
},
version: 1,
};
const state = { deviceId: 0, status: 'disconnected', accounts: [] };
const state = [];

it('stores the list of accounts', () => {
const action = {
type: actionTypes.storeHWAccounts,
accounts: [...hwAccounts, testHWWallet],
accounts: [...hwAccounts, testHWWalletAccount],
};
const expectedData = { ...state, accounts: [...state.accounts, ...action.accounts] };
const updatedState = hardwareWallet(state, action);
const updatedState = accounts(state, action);
expect(updatedState).toEqual(expectedData);
});

it('removes the list of accounts', () => {
const action = {
type: actionTypes.removeHWAccounts,
};
const updatedState = hardwareWallet(state, action);
const updatedState = accounts(state, action);
expect(updatedState).toEqual(state);
});

it('returns the default list of accounts if invalid action is called', () => {
const action = {
type: 'INVALID_ACTION',
};
const updatedState = hardwareWallet(state, action);
const updatedState = accounts(state, action);
expect(updatedState).toEqual(state);
});
});
21 changes: 21 additions & 0 deletions src/modules/hardwareWallet/store/reducers/currentDeviceReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import actionTypes from '@hardwareWallet/store/actions/actionTypes';

export const initialState = {
status: 'disconnected',
};

/**
*
* @param {Object} state
* @param {Object} action
*/
export const currentDevice = (state = initialState, action) => {
const { type, device } = action;
switch (type) {
case actionTypes.deviceUpdate: {
return device;
}
default:
return state;
}
};
19 changes: 19 additions & 0 deletions src/modules/hardwareWallet/store/reducers/devicesReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import actionTypes from '@hardwareWallet/store/actions/actionTypes';

export const initialState = [];

/**
*
* @param {Object} state
* @param {Object} action
*/
export const devices = (state = initialState, action) => {
const { type, devices: newDevices } = action;
switch (type) {
case actionTypes.changeDevices: {
return newDevices;
}
default:
return state;
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IPC_MESSAGES } from '@libs/hwServer/constants';
import { hardwareWallet, initialState } from './hardwareWalletReducer';
import { devices, initialState } from './devicesReducer';

const { DEVICE_LIST_CHANGED, DEVICE_UPDATE } = IPC_MESSAGES;

Expand All @@ -18,7 +18,7 @@ describe('HardwareWallet reducer', () => {
hardwareDevices: updatedDevices,
activeHardwareDeviceId: initialState.activeHardwareDeviceId,
};
expect(hardwareWallet(initialState, actionData)).toEqual(expectedState);
expect(devices(initialState, actionData)).toEqual(expectedState);
});

it('Should update activeHardwareDeviceId when dispatching DEVICE_UPDATE', async () => {
Expand All @@ -31,7 +31,7 @@ describe('HardwareWallet reducer', () => {
hardwareDevices: initialState.hardwareDevices,
activeHardwareDeviceId: selectedDeviceId,
};
expect(hardwareWallet(initialState, actionData)).toEqual(expectedState);
expect(devices(initialState, actionData)).toEqual(expectedState);
});

it('Should update activeHardwareDeviceId when dispatching DEVICE_UPDATE', async () => {
Expand All @@ -44,10 +44,10 @@ describe('HardwareWallet reducer', () => {
hardwareDevices: initialState.hardwareDevices,
activeHardwareDeviceId: selectedDeviceId,
};
expect(hardwareWallet(initialState, actionData)).toEqual(expectedState);
expect(devices(initialState, actionData)).toEqual(expectedState);
});

it('Should return default state', async () => {
expect(hardwareWallet(initialState, {})).toEqual(initialState);
expect(devices(initialState, {})).toEqual(initialState);
});
});
10 changes: 10 additions & 0 deletions src/modules/hardwareWallet/store/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { combineReducers } from 'redux';
import { accounts } from './acountsReducers';
import { currentDevice } from './currentDeviceReducer';
import { devices } from './devicesReducer';

export const hardwareWallet = combineReducers({
devices,
currentDevice,
accounts,
});
28 changes: 0 additions & 28 deletions src/modules/hardwareWallet/store/reducers/reducers.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/modules/hardwareWallet/store/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const selectHardwareDevices = (state) => state.hardwareWallet.devices;
export const selectActiveHardwareDeviceId = (state) => state.hardwareWallet.currentDevice.deviceId;
export const selectActiveHardwareDevice = (state) => state.hardwareWallet.currentDevice;
2 changes: 1 addition & 1 deletion src/redux/rootReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { default as token } from '@token/fungible/store/reducer';
export { default as transactions } from '@transaction/store/reducer';
export { default as appUpdates } from '@update/store/reducers/appUpdates';
export { default as staking } from '@pos/validator/store/reducers/staking';
export { hardwareWallet } from 'src/modules/hardwareWallet/store/hardwareWalletReducer';
export { hardwareWallet } from '@hardwareWallet/store/reducers';
export { default as watchList } from '@pos/validator/store/reducers/watchList';
export { account } from '@account/store/reducer';
export { blockChainApplications } from '@blockchainApplication/manage/store/reducer';
Expand Down
2 changes: 1 addition & 1 deletion src/redux/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const selectNetworkIdentifier = (state) => state.network.networks?.LSK?.networkI
const selectNetworkName = (state) => state.network.name;
const selectActiveTokenNetwork = (state) => state.network.networks[state.token.active];
const selectStaking = (state) => state.staking;
const selectHW = (state) => state.hardwareWallet;
const selectHW = (state) => state.hardwareWallet.currentDevice;
const selectHWAccounts = (state) => state.hardwareWallet?.accounts || [];
const selectHWStatus = (state) => state.hardwareWallet.status;

Expand Down

0 comments on commit c7618dd

Please sign in to comment.