-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from 1 commit
9c391cf
088ffdc
a43b790
9e8d02d
cd11e5f
0586265
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should move the actions to the ducks folder There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
This file was deleted.
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import reducer from './operations/reducers'; | ||
|
||
export default reducer; |
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); | ||
} |
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; | ||
} | ||
} |
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'; |
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'), | ||
); |
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; |
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(), | ||
]); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in jest, config ignore store and other .config files There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,7 @@ | |
text-align: center; | ||
color: '#FD5523' | ||
} | ||
|
||
.home__container { | ||
text-align: center; | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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'.
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and 'babel stuff'
There was a problem hiding this comment.
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](https://user-images.githubusercontent.com/18735075/52332173-ffbb1100-2a0a-11e9-882d-24593f8b13e4.png)
There was a problem hiding this comment.
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