From ba3cfd6739d48e07a8eebcc401a1f30058838338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Wed, 6 Jun 2018 13:25:11 +0200 Subject: [PATCH] Polish code of reducers/accounts --- src/helpers/db.js | 23 +++++++++++++---------- src/reducers/accounts.js | 39 ++++++--------------------------------- src/reducers/settings.js | 1 - 3 files changed, 19 insertions(+), 44 deletions(-) diff --git a/src/helpers/db.js b/src/helpers/db.js index d5198ce4..19968185 100644 --- a/src/helpers/db.js +++ b/src/helpers/db.js @@ -4,7 +4,7 @@ import Store from 'electron-store' import set from 'lodash/set' import get from 'lodash/get' -import { serializeAccounts, deserializeAccounts } from 'reducers/accounts' +import { decodeAccountsModel, encodeAccountsModel } from 'reducers/accounts' type DBKey = 'settings' | 'accounts' | 'countervalues' @@ -23,17 +23,20 @@ export function setEncryptionKey(key: DBKey, value?: string) { encryptionKey[key] = value } -function middleware(type, key, data: any) { - if (key === 'accounts') { - if (type === 'get') { - data = serializeAccounts(data) - } +const transforms = { + get: { + accounts: decodeAccountsModel, + }, + set: { + accounts: encodeAccountsModel, + }, +} - if (type === 'set') { - data = deserializeAccounts(data) - } +function middleware(type: 'get' | 'set', key: string, data: any) { + const t = transforms[type][key] + if (t) { + data = t(data) } - return data } diff --git a/src/reducers/accounts.js b/src/reducers/accounts.js index b89cc8d5..b3a9ac8e 100644 --- a/src/reducers/accounts.js +++ b/src/reducers/accounts.js @@ -59,9 +59,7 @@ const handlers: Object = { // Selectors -export function accountsSelector(state: { accounts: AccountsState }): Account[] { - return state.accounts -} +export const accountsSelector = (state: { accounts: AccountsState }): Account[] => state.accounts export const currenciesSelector = createSelector(accountsSelector, accounts => [...new Set(accounts.map(a => a.currency))].sort((a, b) => a.name.localeCompare(b.name)), @@ -73,41 +71,16 @@ export const accountSelector = createSelector( (accounts, accountId) => accounts.find(a => a.id === accountId), ) -export function decodeAccount(account: AccountRaw): Account { - return accountModel.decode({ +export const decodeAccount = (account: AccountRaw): Account => + accountModel.decode({ data: account, version: accountModel.version, }) -} - -export function encodeAccount(account: Account): AccountRaw { - return accountModel.encode(account).data -} -// Yeah. `any` should be `AccountRaw[]` but it can also be a map -// of wrapped accounts. And as flow is apparently incapable of doing -// such a simple thing, let's put any, right? I don't care. -// FIXME: no it shouldn't. the purpose of DataModel is to contain the version, it's the only way we can run the migration functions -export function serializeAccounts(accounts: any): Account[] { - // ensure that accounts are always wrapped in data key - if (accounts && accounts.length && !accounts[0].data) { - accounts = accounts.map(account => ({ data: account })) - } - return accounts ? accounts.map(accountModel.decode) : [] -} +export const encodeAccount = (account: Account): AccountRaw => accountModel.encode(account).data -export function deserializeAccounts(accounts: Account[]) { - return accounts.map(account => { - // as account can be passed by main process, the Date types - // can be converted to string. we ensure here that we have real - // date - // FIXME Account shouldn't be passed by main process. only AccountRaw should be! - if (typeof account.lastSyncDate === 'string') { - account.lastSyncDate = new Date(account.lastSyncDate) - } +export const decodeAccountsModel = (raws: *) => (raws || []).map(accountModel.decode) - return accountModel.encode(account) - }) -} +export const encodeAccountsModel = (accounts: *) => (accounts || []).map(accountModel.encode) export default handleActions(handlers, state) diff --git a/src/reducers/settings.js b/src/reducers/settings.js index 2a42f861..7c8d4112 100644 --- a/src/reducers/settings.js +++ b/src/reducers/settings.js @@ -76,7 +76,6 @@ const INITIAL_STATE: SettingsState = { } function asCryptoCurrency(c: Currency): ?CryptoCurrency { - // $FlowFixMe return 'id' in c ? c : null }