Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Redux and Redux-Saga #170

Merged
merged 6 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"plugins": [
["transform-class-properties",
{ "spec": true }
]
]
],
["@babel/plugin-transform-runtime", {
"regenerator": true
}]
]
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"homepage": "https://github.com/AndelaOSP/andela-societies-frontend#readme",
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "7.0.0-bridge.0",
Expand Down Expand Up @@ -57,8 +58,13 @@
"webpack-merge": "^4.1.5"
},
"dependencies": {
"prop-types": "^15.6.2",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-router-dom": "^4.3.1"
"react-redux": "^6.0.0",
"react-router-dom": "^4.3.1",
"redux": "^4.0.1",
"redux-devtools-extension": "^2.13.7",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be in devDependencies

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above libraries when listed on 'devDependencies' are causing bundling errors that they should be listed on 'dependencies'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not all just "redux-devtools-extension"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and 'babel stuff'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kachulio1 The babel stuff is already in devDependencies. Installing redux-devtools-extension in devDependencies is causing the following compilation error. It should be in dependencies.
screenshot 2019-02-06 at 12 26 25

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok maybe keep 'the dev tool then

"redux-saga": "^0.16.2"
}
}
53 changes: 48 additions & 5 deletions src/app/Home/components/HomeComponent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
import React from 'react';
/* eslint-disable react/destructuring-assignment */
import React, { Component } from 'react';
import PropTypes from 'prop-types';

const HomeComponent = () => (
<h1 className='home__title'>Welcome to Andela Societies </h1>
);
import { connect } from 'react-redux';
import TestSaga from './testSaga';

export default HomeComponent;
export class HomeComponent extends Component {
onIncrement = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should move the actions to the ducks folder

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kachulio1 Could you please expound more on what you mean by saying I should move the actions to the ducks folder.

this.props.dispatch({
type: 'INCREMENT',
});
}

onDecrement = () => {
this.props.dispatch({
type: 'DECREMENT',
});
}

onIncrementAsync = () => {
this.props.dispatch({
type: 'INCREMENT_ASYNC',
});
}

render() {
return (
<TestSaga
increment={this.onIncrement}
decrement={this.onDecrement}
incrementAsync={this.onIncrementAsync}
value={this.props.value}
/>
);
}
}

HomeComponent.propTypes = {
value: PropTypes.number.isRequired,
dispatch: PropTypes.func.isRequired,
};

function mapStateToProps(state) {
return {
value: state.home,
};
}

export default connect(mapStateToProps)(HomeComponent);
1 change: 0 additions & 1 deletion src/app/Home/components/index.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/app/Home/components/testSaga.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import PropTypes from 'prop-types';

const TestSaga = ({
increment, decrement, incrementAsync, value,
}) => (
<div>
<h1 className='home__title'>Welcome to Andela Societies</h1>
<div className='home__container'>
<button type='button' onClick={increment}>
Increment
</button>
{' '}
<button type='button' onClick={decrement}>
Decrement
</button>
{' '}
<button type='button' onClick={incrementAsync}>
Increment after 1 second
</button>
<hr />
<div>
Clicked:
{' '}
{value}
{' '}
times
</div>
</div>
</div>
);

TestSaga.propTypes = {
value: PropTypes.number.isRequired,
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
incrementAsync: PropTypes.func.isRequired,
};

export default TestSaga;
6 changes: 3 additions & 3 deletions src/app/Home/components/tests/HomeComponent.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import { shallow } from 'enzyme';
import HomeComponent from '../HomeComponent';
import TestSaga from '../testSaga';

describe('<HomeComponent />', () => {
const shallowWrapper = shallow(<HomeComponent />);
describe('<TestSaga />', () => {
const shallowWrapper = shallow(<TestSaga />);

it('should contain text in h1 tag', () => {
expect(shallowWrapper.find('.home__title').html()).toContain('Welcome to Andela Societies');
Expand Down
3 changes: 3 additions & 0 deletions src/app/Home/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import reducer from './operations/reducers';

export default reducer;
11 changes: 11 additions & 0 deletions src/app/Home/operations/home.data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { delay } from 'redux-saga';
import { call, put, takeEvery } from 'redux-saga/effects';

export function* incrementAsync() {
yield call(delay, 1000);
yield put({ type: 'INCREMENT' });
}

export function* watchIncrementAsync() {
yield takeEvery('INCREMENT_ASYNC', incrementAsync);
}
14 changes: 14 additions & 0 deletions src/app/Home/operations/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const initialState = 0;

export default function reducer(state = initialState, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'INCREMENT_IF_ODD':
return (state % 2 !== 0) ? state + 1 : state;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
2 changes: 2 additions & 0 deletions src/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export { default as home } from './Home';
20 changes: 17 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { render } from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';

import HomeComponent from './app/Home/components';

import { Provider } from 'react-redux';
import configureStore from './store';
// eslint-disable-next-line import/no-named-as-default
import HomeComponent from './app/Home/components/HomeComponent';

import './styles/main.scss';

ReactDOM.render(<HomeComponent />, document.getElementById('root'));
const store = configureStore();
alexmochu marked this conversation as resolved.
Show resolved Hide resolved

render(
<Provider store={store}>
<Router>
<HomeComponent />
</Router>
</Provider>,
document.getElementById('root'),
);
6 changes: 6 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { combineReducers } from 'redux';
import * as reducers from '../app';

const rootReducer = combineReducers(reducers);

export default rootReducer;
9 changes: 9 additions & 0 deletions src/sagas/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { all } from 'redux-saga/effects';
import { watchIncrementAsync } from '../app/Home/operations/home.data';

// single entry point to start all Sagas at once
export default function* rootSaga() {
yield all([
watchIncrementAsync(),
]);
}
21 changes: 21 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in jest, config ignore store and other .config files
which are bringing the coverage down

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kachulio1 I have ignored the files that are bringing the coverage down.

import { applyMiddleware, createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';
import { composeWithDevTools } from 'redux-devtools-extension';

import rootReducer from './reducers';
import rootSaga from './sagas';

export default function configureStore(initialState) {
const sagaMiddleware = createSagaMiddleware();
const middlewares = [sagaMiddleware];
const middlewareEnhancer = applyMiddleware(...middlewares);

const enhancers = [middlewareEnhancer];
const composedEnhancers = composeWithDevTools(...enhancers);

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

sagaMiddleware.run(rootSaga);
return store;
}
4 changes: 4 additions & 0 deletions src/styles/Home/home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
text-align: center;
color: '#FD5523'
}

.home__container {
text-align: center;
}
73 changes: 71 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@
dependencies:
regenerator-transform "^0.13.3"

"@babel/plugin-transform-runtime@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.2.0.tgz#566bc43f7d0aedc880eaddbd29168d0f248966ea"
integrity sha512-jIgkljDdq4RYDnJyQsiWbdvGeei/0MOTtSHKO/rfbd/mXBxNpdlulMx49L0HQ4pug1fXannxoqCI+fYSle9eSw==
dependencies:
"@babel/helper-module-imports" "^7.0.0"
"@babel/helper-plugin-utils" "^7.0.0"
resolve "^1.8.1"
semver "^5.5.1"

"@babel/plugin-transform-shorthand-properties@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.2.0.tgz#6333aee2f8d6ee7e28615457298934a3b46198f0"
Expand Down Expand Up @@ -608,6 +618,13 @@
"@babel/plugin-transform-react-jsx-self" "^7.0.0"
"@babel/plugin-transform-react-jsx-source" "^7.0.0"

"@babel/runtime@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.2.0.tgz#b03e42eeddf5898e00646e4c840fa07ba8dcad7f"
integrity sha512-oouEibCbHMVdZSDlJBO6bZmID/zA/G/Qx3H1d3rSNPTD+L8UNKvCat7aKWSJ74zYbm5zWGh0GQN0hKj8zYFTCg==
dependencies:
regenerator-runtime "^0.12.0"

"@babel/template@^7.1.0", "@babel/template@^7.1.2", "@babel/template@^7.2.2":
version "7.2.2"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.2.2.tgz#005b3fdf0ed96e88041330379e0da9a708eb2907"
Expand Down Expand Up @@ -3724,6 +3741,13 @@ hoist-non-react-statics@^2.5.0:
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.5.tgz#c5903cf409c0dfd908f388e619d86b9c1174cb47"
integrity sha512-rqcy4pJo55FTTLWt+bU8ukscqHeE/e9KWvsOW2b/a3afxQZhwkQdT1rPPCJ0rYXdj4vNcasY8zHTH+jF/qStxw==

hoist-non-react-statics@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.2.1.tgz#c09c0555c84b38a7ede6912b61efddafd6e75e1e"
integrity sha512-TFsu3TV3YLY+zFTZDrN8L2DTFanObwmBLpWvJs1qfUuEQ5bTAdFcwfx2T/bsCXfM9QHSLvjfP+nihEl0yvozxw==
dependencies:
react-is "^16.3.2"

home-or-tmp@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
Expand Down Expand Up @@ -5081,7 +5105,7 @@ loglevel@^1.4.1:
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.1.tgz#e0fc95133b6ef276cdc8887cdaf24aa6f156f8fa"
integrity sha1-4PyVEztu8nbNyIh82vJKpvFW+Po=

loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1:
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
Expand Down Expand Up @@ -6493,11 +6517,28 @@ react-dom@^16.6.3:
prop-types "^15.6.2"
scheduler "^0.11.2"

react-is@^16.3.2:
version "16.7.0"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.7.0.tgz#c1bd21c64f1f1364c6f70695ec02d69392f41bfa"
integrity sha512-Z0VRQdF4NPDoI0tsXVMLkJLiwEBa+RP66g0xDHxgxysxSoCUccSten4RTF/UFvZF1dZvZ9Zu1sx+MDXwcOR34g==

react-is@^16.6.1, react-is@^16.6.3:
version "16.6.3"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.6.3.tgz#d2d7462fcfcbe6ec0da56ad69047e47e56e7eac0"
integrity sha512-u7FDWtthB4rWibG/+mFbVd5FvdI20yde86qKGx4lVUTWmPlSWQ4QxbBIrrs+HnXGbxOUlUzTAP/VDmvCwaP2yA==

react-redux@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-6.0.0.tgz#09e86eeed5febb98e9442458ad2970c8f1a173ef"
integrity sha512-EmbC3uLl60pw2VqSSkj6HpZ6jTk12RMrwXMBdYtM6niq0MdEaRq9KYCwpJflkOZj349BLGQm1MI/JO1W96kLWQ==
dependencies:
"@babel/runtime" "^7.2.0"
hoist-non-react-statics "^3.2.1"
invariant "^2.2.4"
loose-envify "^1.4.0"
prop-types "^15.6.2"
react-is "^16.6.3"

react-router-dom@^4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-4.3.1.tgz#4c2619fc24c4fa87c9fd18f4fb4a43fe63fbd5c6"
Expand Down Expand Up @@ -6633,6 +6674,24 @@ redent@^1.0.0:
indent-string "^2.1.0"
strip-indent "^1.0.1"

redux-devtools-extension@^2.13.7:
version "2.13.7"
resolved "https://registry.yarnpkg.com/redux-devtools-extension/-/redux-devtools-extension-2.13.7.tgz#14bd7a1a7c8bee7f397beb1116fd16fc9633b752"
integrity sha512-F2GlWMWxCTJGRjJ+GSZcGDcVAj6Pbf77FKb4C9S8eni5Eah6UBGNwxNj8K1MTtmItdZH1Wx+EvIifHN2KKcQrw==

redux-saga@^0.16.2:
version "0.16.2"
resolved "https://registry.yarnpkg.com/redux-saga/-/redux-saga-0.16.2.tgz#993662e86bc945d8509ac2b8daba3a8c615cc971"
integrity sha512-iIjKnRThI5sKPEASpUvySemjzwqwI13e3qP7oLub+FycCRDysLSAOwt958niZW6LhxfmS6Qm1BzbU70w/Koc4w==

redux@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.1.tgz#436cae6cc40fbe4727689d7c8fae44808f1bfef5"
integrity sha512-R7bAtSkk7nY6O/OYMVR9RiBI+XghjF9rlbl5806HJbQph0LJVHZrU5oaO4q70eUKiqMRqm4y07KLTlMZ2BlVmg==
dependencies:
loose-envify "^1.4.0"
symbol-observable "^1.2.0"

regenerate-unicode-properties@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-7.0.0.tgz#107405afcc4a190ec5ed450ecaa00ed0cafa7a4c"
Expand All @@ -6650,6 +6709,11 @@ regenerator-runtime@^0.11.0:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==

regenerator-runtime@^0.12.0:
version "0.12.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.12.1.tgz#fa1a71544764c036f8c49b13a08b2594c9f8a0de"
integrity sha512-odxIc1/vDlo4iZcfXqRYFj0vpXFNoGdKMAUieAlFYO6m/nl5e9KR/beGf41z4a1FI+aQgtjhuaSlDxQ0hmkrHg==

regenerator-transform@^0.13.3:
version "0.13.3"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.13.3.tgz#264bd9ff38a8ce24b06e0636496b2c856b57bcbb"
Expand Down Expand Up @@ -6857,7 +6921,7 @@ resolve@1.1.7:
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=

resolve@^1.3.2, resolve@^1.5.0, resolve@^1.6.0:
resolve@^1.3.2, resolve@^1.5.0, resolve@^1.6.0, resolve@^1.8.1:
version "1.9.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.9.0.tgz#a14c6fdfa8f92a7df1d996cb7105fa744658ea06"
integrity sha512-TZNye00tI67lwYvzxCxHGjwTNlUV70io54/Ed4j6PscB8xVfuBJpRenI/o6dVk0cY0PYTY27AgCoGGxRnYuItQ==
Expand Down Expand Up @@ -7558,6 +7622,11 @@ supports-color@^5.1.0, supports-color@^5.3.0, supports-color@^5.5.0:
dependencies:
has-flag "^3.0.0"

symbol-observable@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==

symbol-tree@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6"
Expand Down