Skip to content

Commit

Permalink
Working version.
Browse files Browse the repository at this point in the history
  • Loading branch information
khayong committed Jul 16, 2017
1 parent 4b31c5c commit 877f434
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 183 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
presets: ["es2015", "stage-0", "react"]
}
"presets": ["es2015", "stage-0", "react"]
}
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Created by https://www.gitignore.io/api/gitbook,osx,webstorm,node

### GitBook ###
Expand Down Expand Up @@ -141,4 +140,4 @@ dist
/.npmignore
/xclap.js
/.babelrc
/test
/test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# electrode-apollo-redux
# electrode-apollo-redux-engine
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports.combineReducerWithApollo = require("./lib/combineReducerWithApollo");
62 changes: 0 additions & 62 deletions lib/apollo-redux-engine.js

This file was deleted.

25 changes: 25 additions & 0 deletions lib/combineReducerWithApollo.js
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;
39 changes: 39 additions & 0 deletions lib/renderToString.js
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;
30 changes: 16 additions & 14 deletions package.json
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",
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports.renderToString = require("./lib/renderToString");
102 changes: 0 additions & 102 deletions test/spec/apollo-redux-engine.spec.js

This file was deleted.

62 changes: 62 additions & 0 deletions test/spec/combine.spec.js
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});
});
});
Loading

0 comments on commit 877f434

Please sign in to comment.