From d29d8c5877c91162f72d12ab9e3f90bd5f95a458 Mon Sep 17 00:00:00 2001 From: Daniel Baranowski Date: Wed, 15 Aug 2018 09:41:26 +0100 Subject: [PATCH] SAVE & LOAD --- app/Root.js | 5 ++ app/home-page/menu-drawer/MenuDrawer.css | 1 + app/index.js | 9 ++- app/main.dev.js | 2 + app/menu.js | 97 ++++++++---------------- app/state/RootReducer.js | 11 ++- app/state/store/store.js | 3 + 7 files changed, 57 insertions(+), 71 deletions(-) create mode 100644 app/state/store/store.js diff --git a/app/Root.js b/app/Root.js index 1ea1d01..b1b5cb5 100644 --- a/app/Root.js +++ b/app/Root.js @@ -10,6 +10,11 @@ type Props = { }; export default class Root extends Component { + componentDidMount() { + window.store = this.props.store; + document.store = this.props.store; + } + render() { return ( diff --git a/app/home-page/menu-drawer/MenuDrawer.css b/app/home-page/menu-drawer/MenuDrawer.css index c8e07d7..add2522 100644 --- a/app/home-page/menu-drawer/MenuDrawer.css +++ b/app/home-page/menu-drawer/MenuDrawer.css @@ -1,3 +1,4 @@ + .closeButton { width: 15px; height: 15px; diff --git a/app/index.js b/app/index.js index 5b01712..8dfaad6 100644 --- a/app/index.js +++ b/app/index.js @@ -2,14 +2,15 @@ import React from 'react'; import { render } from 'react-dom'; import { AppContainer } from 'react-hot-loader'; import Root from './Root'; -import { configureStore, history } from './state/store/configureStore'; +import { history } from './state/store/configureStore'; +import store from "./state/store/store"; import './app.global.scss'; -const store = configureStore(); +const globalStore = store; render( - + , document.getElementById('root') ); @@ -19,7 +20,7 @@ if (module.hot) { const NextRoot = require('./Root'); // eslint-disable-line global-require render( - + , document.getElementById('root') ); diff --git a/app/main.dev.js b/app/main.dev.js index fd031e5..b017062 100644 --- a/app/main.dev.js +++ b/app/main.dev.js @@ -78,6 +78,8 @@ app.on('ready', async () => { mainWindow.focus(); }); + + mainWindow.on('closed', () => { mainWindow = null; }); diff --git a/app/menu.js b/app/menu.js index e4f6b98..df7ed6b 100644 --- a/app/menu.js +++ b/app/menu.js @@ -1,5 +1,6 @@ // @flow -import { app, Menu, shell, BrowserWindow } from 'electron'; +import fs from 'fs'; +import { app, Menu, shell, BrowserWindow, dialog } from 'electron'; export default class MenuBuilder { mainWindow: BrowserWindow; @@ -17,7 +18,7 @@ export default class MenuBuilder { } const template = process.platform === 'darwin' - ? this.buildDarwinTemplate() + ? this.buildDefaultTemplate() : this.buildDefaultTemplate(); const menu = Menu.buildFromTemplate(template); @@ -141,42 +142,11 @@ export default class MenuBuilder { { label: 'Bring All to Front', selector: 'arrangeInFront:' } ] }; - const subMenuHelp = { - label: 'Help', - submenu: [ - { - label: 'Learn More', - click() { - shell.openExternal('http://electron.atom.io'); - } - }, - { - label: 'Documentation', - click() { - shell.openExternal( - 'https://github.com/atom/electron/tree/master/docs#readme' - ); - } - }, - { - label: 'Community Discussions', - click() { - shell.openExternal('https://discuss.atom.io/c/electron'); - } - }, - { - label: 'Search Issues', - click() { - shell.openExternal('https://github.com/atom/electron/issues'); - } - } - ] - }; const subMenuView = process.env.NODE_ENV === 'development' ? subMenuViewDev : subMenuViewProd; - return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow, subMenuHelp]; + return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow]; } buildDefaultTemplate() { @@ -186,7 +156,33 @@ export default class MenuBuilder { submenu: [ { label: '&Open', - accelerator: 'Ctrl+O' + accelerator: 'Ctrl+O', + click: () => { + const path = dialog.showOpenDialog({properties: ['openFile'], filters: [{name: "Character", extensions: ["dnd"]}]}); + + fs.readFile(path[0], 'utf8', (err, data) => { + if (err) throw err; + + this.mainWindow.webContents.executeJavaScript(`document.store.dispatch({type: "LOAD_CHARACTER", payload: '${data}'})`); + }); + } + }, + { + label: '&Save', + accelerator: 'Ctrl+S', + click: () => { + this.mainWindow.webContents.executeJavaScript(`JSON.stringify(document.store.getState())`, function (state) { + dialog.showSaveDialog((fileName) => { + if (fileName === undefined) return; + fs.writeFile(fileName, state, (err) => { + // ToDo handle error + }); + }); + }); + + + + } }, { label: '&Close', @@ -238,37 +234,6 @@ export default class MenuBuilder { } ] }, - { - label: 'Help', - submenu: [ - { - label: 'Learn More', - click() { - shell.openExternal('http://electron.atom.io'); - } - }, - { - label: 'Documentation', - click() { - shell.openExternal( - 'https://github.com/atom/electron/tree/master/docs#readme' - ); - } - }, - { - label: 'Community Discussions', - click() { - shell.openExternal('https://discuss.atom.io/c/electron'); - } - }, - { - label: 'Search Issues', - click() { - shell.openExternal('https://github.com/atom/electron/issues'); - } - } - ] - } ]; return templateDefault; diff --git a/app/state/RootReducer.js b/app/state/RootReducer.js index 886a74b..52f1ae6 100644 --- a/app/state/RootReducer.js +++ b/app/state/RootReducer.js @@ -7,10 +7,19 @@ import attributesMutator from '../home-page/character-sheet/page-one/elements/at import savingThrowsMutator from '../home-page/character-sheet/page-one/elements/saving-throws/state/savingThrowsMutator'; -const rootReducer = combineReducers({ +const combinedReducers = combineReducers({ router, form: attributesMutator(formReducer), homePageReducer, }); +const rootReducer = (state, action) => { + + if (action.type === "LOAD_CHARACTER") { + return JSON.parse(action.payload); + } + + return combinedReducers(state, action) +}; + export default savingThrowsMutator(rootReducer); diff --git a/app/state/store/store.js b/app/state/store/store.js new file mode 100644 index 0000000..0b77dec --- /dev/null +++ b/app/state/store/store.js @@ -0,0 +1,3 @@ +import {configureStore} from './configureStore'; + +export default configureStore();