Persist user data to local storage

This commit is contained in:
Borys_Levytskyi
2016-11-27 16:34:18 +02:00
parent dbe3401ad5
commit af02afb1d6
4 changed files with 51 additions and 11 deletions

View File

@@ -1,9 +1,9 @@
class AppState {
constructor() {
this.emphasizeBytes = true;
export default class AppState {
constructor(persistData) {
this.emphasizeBytes = persistData.emphasizeBytes || true;
this.commandResults = [];
this.handlers = [];
this.uiTheme = 'dark'
this.uiTheme = persistData.uiTheme || 'dark';
}
addCommandResult(result) {
@@ -35,7 +35,11 @@ class AppState {
this.uiTheme = theme;
this.triggerChanged();
}
};
var appState = new AppState();
export default appState;
getPersistData() {
return {
emphasizeBytes: this.emphasizeBytes,
uiTheme: this.uiTheme
}
}
};

26
src/app/appStateStore.js Normal file
View File

@@ -0,0 +1,26 @@
const storeKey = 'AppState';
export default {
getPersistedData() {
var json = window.localStorage.getItem(storeKey);
if(!json) {
return {};
}
try {
return JSON.parse(json);
}
catch(ex) {
console.error('Failed to parse AppState json. Json Value: \n' + json, ex);
return {};
}
},
watch (appState) {
appState.onChange(() => this.persistData(appState));
},
persistData(appState) {
localStorage.setItem(storeKey, JSON.stringify(appState.getPersistData()));
}
}

View File

@@ -1,4 +1,3 @@
import appState from './appState';
import HelpResult from './models/HelpResult';
import UnknownCommandResult from './models/UnknownCommandResult';
import ExpressionResult from './models/ExpressionResult';
@@ -7,7 +6,7 @@ import * as expression from './expression';
var cmdConfig = {};
export default {
initialize (cmd) {
initialize (cmd, appState) {
cmd.command({
canHandle: (input) => expression.parser.canParse(input),

View File

@@ -1,12 +1,23 @@
import React from 'react';
import ReactDOM from 'react-dom';
import InputBox from './components/InputBox.jsx';
import appState from './appState';
import AppState from './AppState';
import appStateStore from './appStateStore';
import cmd from './cmd';
import commands from './commands';
import AppRoot from './components/AppRoot';
commands.initialize(cmd);
var stateData = appStateStore.getPersistedData();
const appState = new AppState(stateData);
console.log('Loaded stateData', stateData);
appStateStore.watch(appState);
commands.initialize(cmd, appState);
console.log("appState", appState);
// cmd.execute('1');
// cmd.execute('2');