From df72d04638ac63cd7e0a6ff7c73e9fd3c69cc259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Sat, 19 May 2018 10:11:08 +0200 Subject: [PATCH] Bugfix settings to always be saved even without DB: there were a bug were the currency exchange settings was never saved DB: is a too tricky pattern that we'll drop in the future. the problem with this is that we actually miss cases where a DB: should be triggered ultimately an action should be atomic and each changes it affect should be saved without extra magic for performance purpose we can just === check if things have actually changed and we can do this at many levels in the future, we will try to reorganize things to allow this better, for instance splitting latest countervalues vs historical countervalues so you only save the latest more frequently.. --- src/middlewares/db.js | 7 +++++-- src/reducers/settings.js | 33 +++++++++++++++++---------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/middlewares/db.js b/src/middlewares/db.js index 3ac08297..6cc5e566 100644 --- a/src/middlewares/db.js +++ b/src/middlewares/db.js @@ -3,7 +3,7 @@ import db from 'helpers/db' import { getAccounts } from 'reducers/accounts' -import { settingsExportSelector } from 'reducers/settings' +import { settingsExportSelector, areSettingsLoaded } from 'reducers/settings' import CounterValues from 'helpers/countervalues' export default store => next => action => { @@ -11,8 +11,8 @@ export default store => next => action => { const [, type] = action.type.split(':') store.dispatch({ type, payload: action.payload }) const state = store.getState() - db.set('settings', settingsExportSelector(state)) db.set('accounts', getAccounts(state)) + // ^ TODO ultimately we'll do same for accounts to drop DB: pattern } else { const oldState = store.getState() const res = next(action) @@ -20,6 +20,9 @@ export default store => next => action => { if (oldState.countervalues !== newState.countervalues) { db.set('countervalues', CounterValues.exportSelector(newState)) } + if (areSettingsLoaded(newState) && oldState.settings !== newState.settings) { + db.set('settings', settingsExportSelector(newState)) + } return res } } diff --git a/src/reducers/settings.js b/src/reducers/settings.js index 377cfd36..5ba2d60e 100644 --- a/src/reducers/settings.js +++ b/src/reducers/settings.js @@ -14,6 +14,7 @@ import type { Settings, CurrencySettings } from 'types/common' import type { State } from 'reducers' export type SettingsState = { + loaded: boolean, // is the settings loaded from db (it not we don't save them) hasCompletedOnboarding: boolean, counterValue: string, language: string, @@ -35,21 +36,6 @@ const localeSplit = window.navigator.language.split('-') const language = (localeSplit[0] || 'en').toLowerCase() const region = (localeSplit[1] || 'US').toUpperCase() -const defaultState: SettingsState = { - hasCompletedOnboarding: false, - counterValue: 'USD', - language, - orderAccounts: 'balance|asc', - password: { - isEnabled: false, - value: '', - }, - marketIndicator: 'western', - currenciesSettings: {}, - region, - developerMode: false, -} - const CURRENCY_DEFAULTS_SETTINGS: CurrencySettings = { confirmationsToSpend: 10, minConfirmationsToSpend: 10, // FIXME DROP @@ -65,7 +51,19 @@ const CURRENCY_DEFAULTS_SETTINGS: CurrencySettings = { } const state: SettingsState = { - ...defaultState, + hasCompletedOnboarding: false, + counterValue: 'USD', + language, + orderAccounts: 'balance|asc', + password: { + isEnabled: false, + value: '', + }, + marketIndicator: 'western', + currenciesSettings: {}, + region, + developerMode: false, + loaded: false, } function asCryptoCurrency(c: Currency): ?CryptoCurrency { @@ -107,6 +105,7 @@ const handlers: Object = { FETCH_SETTINGS: (state: SettingsState, { payload: settings }: { payload: Settings }) => ({ ...state, ...settings, + loaded: true, }), } @@ -144,6 +143,8 @@ export const localeSelector = (state: State) => { export const getOrderAccounts = (state: State) => state.settings.orderAccounts +export const areSettingsLoaded = (state: State) => state.settings.loaded + export const currencySettingsSelector = ( state: State, currency: CryptoCurrency,