From 5573ab7e17400229dd5d79fa50808b38293872fc Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Thu, 23 Nov 2017 11:30:19 +0100 Subject: Add SettingsModel and fix issue with improper mobx data handling --- src/api/server/LocalApi.js | 4 +++- src/components/layout/Sidebar.js | 2 ++ src/containers/layout/AppLayoutContainer.js | 9 ++++----- src/models/Settings.js | 19 +++++++++++++++++++ src/stores/AppStore.js | 4 ++-- src/stores/SettingsStore.js | 13 ++++--------- 6 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 src/models/Settings.js (limited to 'src') diff --git a/src/api/server/LocalApi.js b/src/api/server/LocalApi.js index 79ac6e12f..eba236f16 100644 --- a/src/api/server/LocalApi.js +++ b/src/api/server/LocalApi.js @@ -1,3 +1,5 @@ +import SettingsModel from '../../models/Settings'; + export default class LocalApi { // App async updateAppSettings(data) { @@ -13,7 +15,7 @@ export default class LocalApi { async getAppSettings() { const settingsString = localStorage.getItem('app'); try { - const settings = JSON.parse(settingsString) || {}; + const settings = new SettingsModel(JSON.parse(settingsString) || {}); console.debug('LocalApi::getAppSettings resolves', settings); return settings; diff --git a/src/components/layout/Sidebar.js b/src/components/layout/Sidebar.js index ea34e8702..cb2ecc8ce 100644 --- a/src/components/layout/Sidebar.js +++ b/src/components/layout/Sidebar.js @@ -2,6 +2,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ReactTooltip from 'react-tooltip'; import { defineMessages, intlShape } from 'react-intl'; +import { observer } from 'mobx-react'; import Tabbar from '../services/tabs/Tabbar'; import { ctrlKey } from '../../environment'; @@ -25,6 +26,7 @@ const messages = defineMessages({ }, }); +@observer export default class Sidebar extends Component { static propTypes = { openSettings: PropTypes.func.isRequired, diff --git a/src/containers/layout/AppLayoutContainer.js b/src/containers/layout/AppLayoutContainer.js index 8e5b3d2ed..7c6ceccd6 100644 --- a/src/containers/layout/AppLayoutContainer.js +++ b/src/containers/layout/AppLayoutContainer.js @@ -67,20 +67,19 @@ export default class AppLayoutContainer extends Component { const isLoadingServices = services.allServicesRequest.isExecuting && services.allServicesRequest.isExecutingFirstTime; - // const isLoadingRecipes = recipes.allRecipesRequest.isExecuting - // && recipes.allRecipesRequest.isExecutingFirstTime; - if (isLoadingServices) { return ( ); } + const isMuted = settings.all.isAppMuted || app.isSystemMuted; + const sidebar = ( ); diff --git a/src/models/Settings.js b/src/models/Settings.js new file mode 100644 index 000000000..3b352f9aa --- /dev/null +++ b/src/models/Settings.js @@ -0,0 +1,19 @@ +import { observable } from 'mobx'; +import { DEFAULT_APP_SETTINGS } from '../config'; + +export default class Settings { + @observable autoLaunchOnStart = DEFAULT_APP_SETTINGS.autoLaunchOnStart; + @observable autoLaunchInBackground = DEFAULT_APP_SETTINGS.autoLaunchInBackground; + @observable runInBackground = DEFAULT_APP_SETTINGS.runInBackground; + @observable enableSystemTray = DEFAULT_APP_SETTINGS.enableSystemTray; + @observable minimizeToSystemTray = DEFAULT_APP_SETTINGS.minimizeToSystemTray; + @observable showDisabledServices = DEFAULT_APP_SETTINGS.showDisabledServices; + @observable enableSpellchecking = DEFAULT_APP_SETTINGS.enableSpellchecking; + @observable locale = DEFAULT_APP_SETTINGS.locale; + @observable beta = DEFAULT_APP_SETTINGS.beta; + @observable isAppMuted = DEFAULT_APP_SETTINGS.isAppMuted; + + constructor(data) { + Object.assign(this, data); + } +} diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index 3eb2c38d2..0b7c60bce 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js @@ -220,13 +220,13 @@ export default class AppStore extends Store { @action _muteApp({ isMuted }) { this.actions.settings.update({ settings: { - isMuted, + isAppMuted: isMuted, }, }); } @action _toggleMuteApp() { - this._muteApp({ isMuted: !this.stores.settings.all.isMuted }); + this._muteApp({ isMuted: !this.stores.settings.all.isAppMuted }); } // Reactions diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index ad3c53ccf..30058f41d 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js @@ -1,11 +1,10 @@ import { ipcRenderer } from 'electron'; -import { action, computed, observable } from 'mobx'; +import { action, computed, observable, extendObservable } from 'mobx'; import Store from './lib/Store'; import Request from './lib/Request'; import CachedRequest from './lib/CachedRequest'; import { gaEvent } from '../lib/analytics'; -import { DEFAULT_APP_SETTINGS } from '../config'; export default class SettingsStore extends Store { @observable allSettingsRequest = new CachedRequest(this.api.local, 'getSettings'); @@ -18,10 +17,6 @@ export default class SettingsStore extends Store { // Register action handlers this.actions.settings.update.listen(this._update.bind(this)); this.actions.settings.remove.listen(this._remove.bind(this)); - - // this.registerReactions([ - // this._shareSettingsWithMainProcess.bind(this), - // ]); } setup() { @@ -30,14 +25,14 @@ export default class SettingsStore extends Store { } @computed get all() { - return observable(Object.assign(DEFAULT_APP_SETTINGS, this.allSettingsRequest.result)); + return this.allSettingsRequest.result || {}; } @action async _update({ settings }) { await this.updateSettingsRequest.execute(settings)._promise; - this.allSettingsRequest.patch((result) => { + await this.allSettingsRequest.patch((result) => { if (!result) return; - Object.assign(result, settings); + extendObservable(result, settings); }); // We need a little hack to wait until everything is patched -- cgit v1.2.3-54-g00ecf From 27894ebbbf38193a6f77dc9837731bca081afd32 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Thu, 23 Nov 2017 11:30:35 +0100 Subject: Remove flow types --- src/models/News.js | 2 +- src/models/Plan.js | 2 +- src/models/RecipePreview.js | 2 +- src/models/User.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/models/News.js b/src/models/News.js index e8953ff8c..a96e6550f 100644 --- a/src/models/News.js +++ b/src/models/News.js @@ -6,7 +6,7 @@ export default class News { type: string = 'primary'; sticky: bool = false; - constructor(data: Object) { + constructor(data) { if (!data.id) { throw Error('News requires Id'); } diff --git a/src/models/Plan.js b/src/models/Plan.js index 1f2a44902..e77353824 100644 --- a/src/models/Plan.js +++ b/src/models/Plan.js @@ -10,7 +10,7 @@ export default class Plan { price: 0, } - constructor(data: Object) { + constructor(data) { Object.assign(this, data); } } diff --git a/src/models/RecipePreview.js b/src/models/RecipePreview.js index 7b497edf3..525a5c4b5 100644 --- a/src/models/RecipePreview.js +++ b/src/models/RecipePreview.js @@ -6,7 +6,7 @@ export default class RecipePreview { icon: string = ''; // TODO: check if this isn't replaced by `icons` featured: bool = false; - constructor(data: Object) { + constructor(data) { if (!data.id) { throw Error('RecipePreview requires Id'); } diff --git a/src/models/User.js b/src/models/User.js index 94b579928..e2d2fc0c8 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -16,7 +16,7 @@ export default class User { @observable isDonor = false; @observable isMiner = false; - constructor(data: Object) { + constructor(data) { if (!data.id) { throw Error('User requires Id'); } -- cgit v1.2.3-54-g00ecf