-
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
Showing
12 changed files
with
259 additions
and
183 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
presets: ["es2015", "stage-0", "react"] | ||
} | ||
"presets": ["es2015", "stage-0", "react"] | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
# electrode-apollo-redux | ||
# electrode-apollo-redux-engine |
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,3 @@ | ||
"use strict"; | ||
|
||
module.exports.combineReducerWithApollo = require("./lib/combineReducerWithApollo"); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict"; | ||
|
||
const combineReducerWithApollo = (reducer, client, key = "apollo") => { | ||
const apolloReducer = client.reducer(); | ||
|
||
return function combination(state = {}, action) { | ||
// Remove appolo key to prevent unexpected key warning. | ||
const apolloState = state[key]; | ||
delete state[key]; | ||
|
||
const nextState = reducer(state, action); | ||
|
||
const previousStateForApollo = apolloState; | ||
const nextStateForApollo = apolloReducer(previousStateForApollo, action); | ||
|
||
if (previousStateForApollo !== nextStateForApollo) { | ||
return Object.assign({}, nextState, {[key]: nextStateForApollo}); | ||
} else { | ||
nextState[key] = previousStateForApollo; | ||
return nextState; | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = combineReducerWithApollo; |
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,39 @@ | ||
"use strict"; | ||
|
||
const assert = require("assert"); | ||
const optionalRequire = require("optional-require")(require); | ||
const React = optionalRequire("react"); | ||
const ReactDomServer = optionalRequire("react-dom/server"); | ||
const ReactApollo = optionalRequire("react-apollo"); | ||
const ReactRouter = require("react-router"); | ||
const Provider = require("react-redux").Provider; | ||
|
||
const renderToString = (req, store, match, withIds) => { // eslint-disable-line | ||
if (req.app && req.app.disableSSR) { | ||
return ""; | ||
} else { | ||
assert(React, "Can't do SSR because React module is not available"); | ||
assert(ReactDomServer, "Can't do SSR because ReactDomServer module is not available"); | ||
|
||
const app = req.server && req.server.app || req.app; | ||
if (app.apollo) { | ||
assert(ReactApollo, "Can't use Apollo because ReactApollo module is not available"); | ||
|
||
return (withIds ? ReactDomServer.renderToString : ReactDomServer.renderToStaticMarkup)( | ||
React.createElement( | ||
ReactApollo.ApolloProvider, { store, client: app.apollo }, | ||
React.createElement(ReactRouter.RouterContext, match.renderProps) | ||
) | ||
); | ||
} else { | ||
return (withIds ? ReactDomServer.renderToString : ReactDomServer.renderToStaticMarkup)( | ||
React.createElement( | ||
Provider, { store }, | ||
React.createElement(ReactRouter.RouterContext, match.renderProps) | ||
) | ||
); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = renderToString; |
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,16 +1,16 @@ | ||
{ | ||
"name": "electrode-apollo-redux-engine", | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"description": "Integrating Electrode with Apollo and Redux", | ||
"main": "lib/apollo-redux-engine.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "clap test", | ||
"lint": "clap lint", | ||
"coverage": "clap check" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/khayong/electrode-apollo-redux.git" | ||
"url": "git+https://github.com/khayong/electrode-apollo-redux-engine.git" | ||
}, | ||
"keywords": [ | ||
"Electrode", | ||
|
@@ -20,27 +20,29 @@ | |
"author": "Tan Khay Ong <[email protected]> (https://github.com/khayong)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/khayong/electrode-apollo-redux/issues" | ||
"url": "https://github.com/khayong/electrode-apollo-redux-engine/issues" | ||
}, | ||
"homepage": "https://github.com/khayong/electrode-apollo-redux#readme", | ||
"homepage": "https://github.com/khayong/electrode-apollo-redux-engine#readme", | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-stage-0": "^6.24.1", | ||
"babel-register": "^6.24.1", | ||
"babel-preset-es2015": "^6.0.15", | ||
"babel-preset-react": "^6.0.15", | ||
"babel-preset-stage-0": "^6.0.15", | ||
"babel-register": "^6.5.2", | ||
"electrode-archetype-njs-module-dev": "^2.2.0", | ||
"electrode-redux-router-engine": "^1.4.5" | ||
"electrode-redux-router-engine": "^1.4.5", | ||
"react": "^15.6.1", | ||
"react-apollo": "^1.4.3", | ||
"redux": "^3.7.2" | ||
}, | ||
"dependencies": { | ||
"optional-require": "^1.0.0", | ||
"react-apollo": "^1.4.3", | ||
"react-dom": "^15.6.1", | ||
"react-redux": "^5.0.5", | ||
"react-router": "^3.0.5" | ||
}, | ||
"peerDependencies": { | ||
"react": "^15.3.1 || ^0.14.8", | ||
"react-dom": "^15.3.1 || ^0.14.8" | ||
"react": "^15.6.1", | ||
"react-apollo": "^1.4.3", | ||
"react-dom": "^15.6.1" | ||
}, | ||
"nyc": { | ||
"all": true, | ||
|
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,3 @@ | ||
"use strict"; | ||
|
||
module.exports.renderToString = require("./lib/renderToString"); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
|
||
const createStore = require("redux").createStore; | ||
const combineReducers = require("redux").combineReducers; | ||
const ApolloClient = require("react-apollo").ApolloClient; | ||
|
||
const {combineReducerWithApollo} = require("../.."); | ||
|
||
const checkBox = (store, action) => { | ||
if (action.type === "TOGGLE_CHECK") { | ||
return { | ||
checked: !store.checked | ||
}; | ||
} | ||
|
||
return store || {checked: false}; | ||
}; | ||
|
||
const number = (store, action) => { | ||
if (action.type === "INC_NUMBER") { | ||
return { | ||
value: store.value + 1 | ||
}; | ||
} else if (action.type === "DEC_NUMBER") { | ||
return { | ||
value: store.value - 1 | ||
}; | ||
} | ||
|
||
return store || {value: 0}; | ||
}; | ||
|
||
describe("Combine reducer with Apollo", function () { | ||
|
||
it("should combine existing root reducer", () => { | ||
const client = new ApolloClient(); | ||
|
||
const rootReducer = combineReducers({ | ||
checkBox, | ||
number | ||
}); | ||
|
||
const initialState = { | ||
checkBox: {checked: false}, | ||
number: {value: 999} | ||
}; | ||
const store = createStore(combineReducerWithApollo(rootReducer, client), initialState); | ||
expect(store.getState()).to.have.all.keys("checkBox", "number", "apollo"); | ||
expect(store.getState().checkBox).to.deep.equal({checked: false}); | ||
expect(store.getState().number).to.deep.equal({value: 999}); | ||
|
||
store.dispatch({type: "TOGGLE_CHECK"}); | ||
expect(store.getState().checkBox).to.deep.equal({checked: true}); | ||
store.dispatch({type: "TOGGLE_CHECK"}); | ||
expect(store.getState().checkBox).to.deep.equal({checked: false}); | ||
|
||
store.dispatch({type: "INC_NUMBER"}); | ||
expect(store.getState().number).to.deep.equal({value: 1000}); | ||
store.dispatch({type: "DEC_NUMBER"}); | ||
expect(store.getState().number).to.deep.equal({value: 999}); | ||
}); | ||
}); |
Oops, something went wrong.