Skip to content

Commit

Permalink
Added basic redux store.
Browse files Browse the repository at this point in the history
  • Loading branch information
workhorsy committed Jul 21, 2017
1 parent 50e4700 commit 4996e94
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export function addComic(text) {
return {
type: 'ADD_COMIC',
text
};
}

export function removeComic(comic) {
return {
type: 'REMOVE_COMIC',
comic
};
}
13 changes: 12 additions & 1 deletion src/components/comic-menu/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { h, Component } from 'preact';
//import { Link } from 'preact-router/match';
import { connect } from 'preact-redux';
import { bindActions } from '../../util';
import reduce from '../../reducers';
import style from './style';
import * as actions from '../../actions';
import { toggleFullScreen } from '../../lib/utility';

import ComicSettings from '../comic-settings';
Expand All @@ -18,6 +22,7 @@ const TestData = {
]
}

@connect(reduce, bindActions(actions))
export default class ComicMenu extends Component {
state = {
is_showing_settings: false,
Expand All @@ -28,6 +33,12 @@ export default class ComicMenu extends Component {
toggleFullScreen();
}

onbtnOpenFile = () => {
alert("FIXME: onbtnOpenFile");
this.setState({ text: 'ADD_COMIC' });
console.log(this.state.text);
}

onBtnSettings = () => {
this.setState(prevState => ({ is_showing_settings: !prevState.is_showing_settings }));
this.setState({ is_showing_library: false });
Expand All @@ -44,7 +55,7 @@ export default class ComicMenu extends Component {
<div class={style.comic_menu}>
<div id={style.topMenuPanel}>
<div id={style.topMenuButtons}>
<ComicButton id="btnFileLoad" translatable="true">Open comic file</ComicButton>
<ComicButton id="btnFileLoad" translatable="true" onClick={this.onbtnOpenFile}>Open comic file</ComicButton>
<ComicButton id="btnLibrary" translatable="true" onClick={this.onBtnLibrary}>Library</ComicButton>
<ComicButton id="btnFullScreen" translatable="true" onClick={this.onBtnFullScreen}>Full Screen</ComicButton>
<ComicButton id="btnSettings" translatable="true" onClick={this.onBtnSettings}>Settings</ComicButton>
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import './style';
import { h, render } from 'preact';
import { Provider } from 'preact-redux';
import store from './store';
import App from './components/app';
import './style';

render((
<div id="outer">
<Provider store={store}>
<App />
</Provider>
</div>
), document.body);

export default App;
6 changes: 6 additions & 0 deletions src/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

const EMPTY = {};

export default store => {
return store || EMPTY;
};
24 changes: 24 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createStore } from 'redux';

let ACTIONS = {
ADD_COMIC: ({ comics, ...state }, { text }) => ({
comics: [...comics, {
id: Math.random().toString(36).substring(2),
text
}],
...state
}),

REMOVE_COMIC: ({ comics, ...state }, { comic }) => ({
comics: comics.filter( i => i!==comic ),
...state
})
};

const INITIAL = {
comics: []
};

export default createStore( (state, action) => (
action && ACTIONS[action.type] ? ACTIONS[action.type](state, action) : state
), INITIAL, window.devToolsExtension && window.devToolsExtension());
7 changes: 7 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bindActionCreators } from 'redux';

export function bindActions(actions) {
return dispatch => ({
...bindActionCreators(actions, dispatch)
});
}

0 comments on commit 4996e94

Please sign in to comment.