Skip to content

Commit

Permalink
refactor main.js
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewglover committed Oct 9, 2016
1 parent f32704c commit b0e9b35
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 21 deletions.
14 changes: 13 additions & 1 deletion app/action_creators/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
// @flow
/* eslint-disable import/prefer-default-export */
import { UPDATE_AUTH_STATUS } from '../action_types';
import {
UPDATE_AUTH_STATUS,
SET_JWT } from '../action_types';

type updateAuthAction = {
type: string,
isAuthenticated: boolean
}

type setJwtAction = {
type: string,
jwt: string
}

export const updateAuthStatus = (isAuthenticated: boolean): updateAuthAction =>
({
type: UPDATE_AUTH_STATUS,
isAuthenticated,
});

export const setJwt = (jwt: string): setJwtAction =>
({
type: SET_JWT,
jwt,
});
2 changes: 2 additions & 0 deletions app/action_types/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/* eslint-disable import/prefer-default-export */
export const UPDATE_AUTH_STATUS = 'UPDATE_AUTH_STATUS';

export const SET_JWT = 'SET_JWT';
7 changes: 5 additions & 2 deletions app/components/requires_authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import React, { Component, PropTypes } from 'react';
import { compose } from 'ramda';
import { withRouter } from 'react-router';
import { connect } from 'react-redux';
import { getIsAuthenticated } from '../reducers';

// mapStateToProps :: State -> { isAuthenticated: boolean }
const mapStateToProps =
({ auth: { isAuthenticated } }) => ({ isAuthenticated });
const mapStateToProps = (state) =>
({
isAuthenticated: getIsAuthenticated(state),
});


// connector :: React.Component -> React.Component
Expand Down
23 changes: 15 additions & 8 deletions app/configure_store/configure_store.dev.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { createStore } from 'redux';
import rootReducer from '../reducers/root_reducer';
import { createStore, compose } from 'redux';
import rootReducer from '../reducers';

const enhancer =
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(); // eslint-disable-line
/* eslint-disable no-underscore-dangle */
const devToolsEnhancer = window.__REDUX_DEVTOOLS_EXTENSION__
? window.__REDUX_DEVTOOLS_EXTENSION__()
: undefined;
/* eslint-enable */

const configureStore = initialState => {
const store = createStore(rootReducer, initialState, enhancer);
const configureStore = (initialState, customEnhancer) => {
const enhancers = customEnhancer
? compose(devToolsEnhancer, customEnhancer)
: devToolsEnhancer;

const store = createStore(rootReducer, initialState, enhancers);

if (module.hot) {
module.hot.accept(
'../reducers/root_reducer',
() => store.replaceReducer(require('../reducers/root_reducer').default)); // eslint-disable-line global-require, max-len
'../reducers',
() => store.replaceReducer(require('../reducers').default)); // eslint-disable-line global-require, max-len
}

return store;
Expand Down
6 changes: 3 additions & 3 deletions app/configure_store/configure_store.production.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createStore } from 'redux';
import rootReducer from '../reducers/root_reducer';
import rootReducer from '../reducers';


const configureStore = initialState =>
createStore(rootReducer, initialState);
const configureStore = (initialState, enhancer) =>
createStore(rootReducer, initialState, enhancer);

export default configureStore;
12 changes: 12 additions & 0 deletions app/main_helpers/query_params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @flow

const queryPairs: string[] =
window.location.search.match(/[^?&]+/g) || [];


const queryParams: Object =
queryPairs
.map(s => s.split('='))
.reduce((acc, [k, v]) => Object.assign(acc, { [k]: v }), {});

export default queryParams;
19 changes: 16 additions & 3 deletions app/main_helpers/store.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import thunk from 'redux-thunk';
import throttle from 'lodash/throttle';
import { compose, pick } from 'ramda';
// import { compose, pick } from 'ramda';
import configureStore from '../configure_store';
import { loadState, saveState } from '../local_storage';
import { setJwt } from '../action_creators';
import queryParams from './query_params';

const saveStateToLocalStorage =
throttle(compose(saveState, pick(['auth'])), 1000);
throttle(saveState, 1000);

const persistedState = loadState();

const store = configureStore(persistedState);
const logger = process.env.NODE_ENV !== 'production'
? createLogger()
: undefined;

const middlewares = [thunk, logger].filter(v => !!v);

const store = configureStore(persistedState, applyMiddleware(...middlewares));

if (queryParams.jwt) store.dispatch(setJwt(queryParams.jwt));

store.subscribe(() => saveStateToLocalStorage(store.getState()));

Expand Down
9 changes: 7 additions & 2 deletions app/reducers/auth_reducer/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { UPDATE_AUTH_STATUS } from '../../action_types';
import {
UPDATE_AUTH_STATUS } from '../../action_types';

// DEFAULT_AUTH :: AuthState
const DEFAULT_AUTH = {
isAuthenticated: false,
};

// updateAuthStatus :: (AuthState, UpdateAuthAction) => AuthState
// updateAuthStatus :: (AuthState, UpdateAuthAction) -> AuthState
const updateAuthStatus = (state, { isAuthenticated }) =>
Object.assign({}, state, { isAuthenticated });


// authReducer :: (AuthState, Action) -> AuthState
const authReducer = (state = DEFAULT_AUTH, action) => {
switch (action.type) {
Expand All @@ -20,3 +22,6 @@ const authReducer = (state = DEFAULT_AUTH, action) => {
};

export default authReducer;

export const getIsAuthenticated = (state) =>
state.isAuthenticated;
5 changes: 4 additions & 1 deletion app/reducers/root_reducer.js → app/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { combineReducers } from 'redux';
import auth from './auth_reducer';
import auth, * as fromAuth from './auth_reducer';

// rootReducer :: State -> State
const rootReducer = combineReducers({
auth,
});

export default rootReducer;

export const getIsAuthenticated = (state) =>
fromAuth.getIsAuthenticated(state.auth);
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"react-router-redux": "^4.0.6",
"react-tap-event-plugin": "^1.0.0",
"redux": "^3.5.2",
"redux-logger": "^2.7.0",
"redux-thunk": "^2.1.0",
"uuid": "^2.0.2"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion test/reducers/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import rootReducer from '../../app/reducers/root_reducer';
import rootReducer from '../../app/reducers';

const initialState = {
auth: {
Expand Down

0 comments on commit b0e9b35

Please sign in to comment.