Skip to content

Commit

Permalink
SAVE & LOAD
Browse files Browse the repository at this point in the history
  • Loading branch information
d-baranowski committed Aug 15, 2018
1 parent 2079995 commit d29d8c5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 71 deletions.
5 changes: 5 additions & 0 deletions app/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ type Props = {
};

export default class Root extends Component<Props> {
componentDidMount() {
window.store = this.props.store;
document.store = this.props.store;
}

render() {
return (
<Provider store={this.props.store}>
Expand Down
1 change: 1 addition & 0 deletions app/home-page/menu-drawer/MenuDrawer.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

.closeButton {
width: 15px;
height: 15px;
Expand Down
9 changes: 5 additions & 4 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<AppContainer>
<Root store={store} history={history} />
<Root store={globalStore} history={history} />
</AppContainer>,
document.getElementById('root')
);
Expand All @@ -19,7 +20,7 @@ if (module.hot) {
const NextRoot = require('./Root'); // eslint-disable-line global-require
render(
<AppContainer>
<NextRoot store={store} history={history} />
<NextRoot store={globalStore} history={history} />
</AppContainer>,
document.getElementById('root')
);
Expand Down
2 changes: 2 additions & 0 deletions app/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ app.on('ready', async () => {
mainWindow.focus();
});



mainWindow.on('closed', () => {
mainWindow = null;
});
Expand Down
97 changes: 31 additions & 66 deletions app/menu.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +18,7 @@ export default class MenuBuilder {
}

const template = process.platform === 'darwin'
? this.buildDarwinTemplate()
? this.buildDefaultTemplate()
: this.buildDefaultTemplate();

const menu = Menu.buildFromTemplate(template);
Expand Down Expand Up @@ -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() {
Expand All @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion app/state/RootReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
3 changes: 3 additions & 0 deletions app/state/store/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {configureStore} from './configureStore';

export default configureStore();

0 comments on commit d29d8c5

Please sign in to comment.