Browse Source

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..
master
Gaëtan Renaudeau 7 years ago
parent
commit
df72d04638
  1. 7
      src/middlewares/db.js
  2. 33
      src/reducers/settings.js

7
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
}
}

33
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,

Loading…
Cancel
Save