Skip to content

Commit

Permalink
Add custom redux store factory
Browse files Browse the repository at this point in the history
  • Loading branch information
arman37 committed May 3, 2018
1 parent 4f54412 commit 2f93d4e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
10 changes: 3 additions & 7 deletions app/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@

import React from 'react';
import ReactDOM from 'react-dom';
import Reducer from '../reducers';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunkMiddleware from 'redux-thunk';
import {initializeApp} from '../actions';
import storeFactory from '../store/index';
import {Containers} from './smart/containers';
import injectTapEventPlugin from 'react-tap-event-plugin';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';

const store = createStore(Reducer, applyMiddleware(
thunkMiddleware, // lets us dispatch() functions
));
const store = storeFactory();

injectTapEventPlugin();
class App extends React.Component {
render () {
render() {
return (
<Provider store={store}>
<div className="electron__player body">
Expand Down
16 changes: 2 additions & 14 deletions app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,10 @@
import base64 from 'base64-js';
import { List, Map } from 'immutable';
import * as Types from '../constants';
import initialState from '../store/initial-state';
const config = new (nodeRequire('electron-config'))({name: 'electron-player-user-preferences'});

const init = Map({
audioView: true,
tableView: false,
dialogOpen: false,
drawerOpen: false,
trackMetadata: null,
backgroundImage: '',
audioTrackList: List.of(),
tbodyHeight: '680px',
scanningForTracks: false,
searchPathList: List.of()
});

export default function(state = init, action) {
export default function(state = initialState, action) {
switch(action.type) {
case Types.ADD_NEW_TRACK:
return state.set('audioTrackList', state.get('audioTrackList').push(action.payload));
Expand Down
28 changes: 28 additions & 0 deletions app/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @author arman
* @since 5/3/18
*
*/


import { createStore, applyMiddleware } from 'redux';
import initialState from '../store/initial-state';
import thunkMiddleware from 'redux-thunk';
import Reducer from '../reducers';

let logger = store => next => action => {
let result;
console.log('Dispatching: ', action.type);
console.log('Prev state: ', store.getState());
console.log('Action: ', action);

result = next(action);

console.log('Next state: ', store.getState());

return result;
};

const storeFactory = (state = initialState) => applyMiddleware(logger, thunkMiddleware)(createStore)(Reducer, state);

export default storeFactory;
22 changes: 22 additions & 0 deletions app/store/initial-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @author arman
* @since 5/3/18
*
*/

import { List, Map } from 'immutable';

const initialState = Map({
audioView: true,
tableView: false,
dialogOpen: false,
drawerOpen: false,
trackMetadata: null,
backgroundImage: '',
audioTrackList: List.of(),
tbodyHeight: '680px',
scanningForTracks: false,
searchPathList: List.of()
});

export default initialState;

0 comments on commit 2f93d4e

Please sign in to comment.