-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Marcello
committed
Sep 24, 2015
1 parent
babd2ef
commit 8a81346
Showing
11 changed files
with
194 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
import "babel/polyfill"; | ||
import React from "react"; | ||
import BrowserHistory from "react-router/lib/BrowserHistory"; | ||
import Location from "react-router/lib/Location"; | ||
import queryString from "query-string"; | ||
import createStore from "./redux/store"; | ||
import ApiClient from "./helpers/ApiClient"; | ||
import universalRouter from "./helpers/universalRouter"; | ||
|
||
const history = new BrowserHistory(); | ||
const client = new ApiClient(); | ||
|
||
const dest = document.getElementById('content'); | ||
const store = createStore(client, window.__data); | ||
const search = document.location.search; | ||
const query = search && queryString.parse(search); // TODO: research the specifics of this | ||
const location = new Location(document.location.pathname, query); | ||
|
||
universalRouter(location, history, store) | ||
.then(({ component }) => { | ||
React.render(component, dest); | ||
if (__DEVTOOLS__) { | ||
console.log("devtois"); | ||
const { DevTools, DebugPanel, LogMonitor } = require('redux-devtools/lib/react'); | ||
React.render(<div> | ||
{ component } | ||
<DebugPanel top right bottom key="debugPanel"> | ||
<DevTools store={ store } monitor={ LogMonitor } /> | ||
</DebugPanel> | ||
</div>, dest); | ||
|
||
} | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import superagent from 'superagent'; | ||
|
||
class ApiClient_ { | ||
constructor(req) { | ||
['get', 'post', 'put', 'patch', 'del']. | ||
forEach((method) => { | ||
this[method] = (path, options) => { | ||
return new Promise((resolve, reject) => { | ||
const request = superagent[method](this.formatUrl(path)); | ||
if (options && options.params) { | ||
request.query(options.params); | ||
} | ||
if (options && options.data) { | ||
request.send(options.data); | ||
} | ||
request.end((err, res) => { | ||
if (err) { | ||
reject((res && res.body) || err); | ||
} else { | ||
resolve(res.body); | ||
} | ||
}); | ||
}); | ||
}; | ||
}); | ||
} | ||
|
||
/* This was originally a standalone function outside of this class, but babel kept breaking, and this fixes it */ | ||
formatUrl(path) { | ||
const adjustedPath = path[0] !== '/' ? '/' + path : path; | ||
//TODO this will need to be adjusted | ||
return '/api' + adjustedPath; | ||
} | ||
} | ||
const ApiClient = ApiClient_; | ||
|
||
export default ApiClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react'; | ||
import Router from 'react-router'; | ||
import createRoutes from '../routes'; | ||
import { Provider } from 'react-redux'; | ||
|
||
const getFetchData = (component = {}) => { | ||
//TODO: research this WrappedComponent attribue | ||
return component.WrappedComponent ? | ||
getFetchData(component.WrappedComponent) : | ||
component.fetchData; | ||
}; | ||
|
||
export function createTransitionHook(store) { | ||
return (nextState, transition, callback) => { | ||
const { params, location: { query } } = nextState; | ||
const promises = nextState.branch | ||
.map(route => route.component) // pull out individual route components | ||
.filter((component) => getFetchData(component)) // only look at ones with a static fetchData() | ||
.map(getFetchData) // pull out fetch data methods | ||
.map(fetchData => fetchData(store, params, query || {})); // call fetch data methods and save promises | ||
Promise.all(promises) | ||
.then(() => { | ||
callback(); // can't just pass callback to then() because callback assumes first param is error | ||
}, (error) => { | ||
callback(error); | ||
}); | ||
}; | ||
} | ||
|
||
export default function universalRouter(location, history, store) { | ||
const routes = createRoutes(store); | ||
return new Promise((resolve, reject) => { | ||
Router.run(routes, location, [createTransitionHook(store)], (error, initialState, transition) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
|
||
if (transition && transition.redirectInfo) { | ||
return resolve({ | ||
transition, | ||
isRedirect: true | ||
}); | ||
} | ||
|
||
if (history) { // only on client side | ||
initialState.history = history; | ||
} | ||
|
||
const component = ( | ||
<Provider store={store} key="provider"> | ||
{() => <Router {...initialState} children={routes}/>} | ||
</Provider> | ||
); | ||
|
||
return resolve({ | ||
component, | ||
isRedirect: false | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export default function clientMiddleware(client) { | ||
return ({dispatch, getState}) => { | ||
return next => action => { | ||
if (typeof action === 'function') { | ||
return action(dispatch, getState); | ||
} | ||
|
||
const { promise, types, ...rest } = action; | ||
if (!promise) { | ||
return next(action); | ||
} | ||
|
||
const [REQUEST, SUCCESS, FAILURE] = types; | ||
next({...rest, type: REQUEST}); | ||
return promise(client).then( | ||
(result) => next({...rest, result, type: SUCCESS}), | ||
(error) => next({...rest, error, type: FAILURE}) | ||
).catch((error)=> { | ||
console.error('MIDDLEWARE ERROR:', error); | ||
next({...rest, error, type: FAILURE}); | ||
}); | ||
}; | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,30 @@ | ||
import { createStore, applyMiddleware, compose } from 'redux'; | ||
|
||
//TODO: implement middleware for async actions | ||
// import createMiddleware from './middleware/clientMiddleware'; | ||
import createMiddleware from './middleware/clientMiddleware'; | ||
|
||
export default function createApiClientStore(client, data) { | ||
// const middleware = createMiddleware(client); | ||
const middleware = createMiddleware(client); | ||
let finalCreateStore; | ||
if ( __DEVTOOLS__) { | ||
const { devTools, persistState } = require('redux-devtools'); | ||
finalCreateStore = compose( | ||
// applyMiddleware(middleware), | ||
devTools(), | ||
persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)) | ||
)(createStore); | ||
} | ||
else { | ||
// finalCreateStore = applyMiddleware(middleware)(createStore); | ||
finalCreateStore = creteStore | ||
if (__DEVTOOLS__) { | ||
const { devTools, persistState } = require('redux-devtools'); | ||
finalCreateStore = compose( | ||
applyMiddleware(middleware), | ||
devTools(), | ||
persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)) | ||
)(createStore); | ||
} else { | ||
finalCreateStore = applyMiddleware(middleware)(createStore); | ||
} | ||
|
||
const reducer = require('./modules/reducer'); | ||
const store = finalCreateStore(reducer, data); | ||
store.client = client; | ||
|
||
|
||
//TODO: set up hotmodule replacement | ||
if (__DEVELOPMENT__ && module.hot) { | ||
module.hot.accept('./modules/reducer', () => { | ||
store.replaceReducer(require('./modules/reducer')); | ||
}); | ||
} | ||
module.hot.accept('./modules/reducer', () => { | ||
store.replaceReducer(require('./modules/reducer')); | ||
}); | ||
} | ||
|
||
return store; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters