Last active
September 1, 2019 11:24
-
-
Save FrancoSirena/94f956b9c4a2e1c380d2919b3c01c622 to your computer and use it in GitHub Desktop.
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
import { createSelector } from 'reselect'; | |
export default createSelector( | |
state => state.module.pieceOne, | |
state => state.module.pieceTwo, | |
state => state.moduleA.pieceOne, | |
function api(pieceOne, pieceTwo, ApieceOne) { | |
return ({ | |
fetchData: function() { | |
return axios.get({ | |
url, | |
payload: { pieceOne, pieceTwo } | |
}); | |
}, | |
fetchHistoricalData: function(startDate) { | |
return axios.get({ | |
url, | |
payload: { pieceOne, ApieceOne, startDate } | |
}); | |
} | |
} | |
); |
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
import { select, put } from 'redux-sagas/effects'; | |
import { requestType, success } from './actions'; | |
import apiGenerator from './apiGenerator'; | |
function* fetchGlobalData({ | |
payload: { startDate } }) { | |
const api = yield select(apiGenerator); | |
const response = yield api.fetchHistoricalData(startDate); | |
yield put success(response); | |
} | |
return yield all([ | |
takeLatest(requestType, fetchGlobalData); | |
]) |
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
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import apiGenerator from './apiGenerator'; | |
function ComponentAPI({ | |
api | |
}) { | |
const [data, setData] = useState(null); | |
const [loading, setLoading] = useState(true); | |
useEffect(() => { | |
api.fetchData().then(setData); | |
}, []); | |
return (loading ? 'Loading data...' : <DataComponent data={data} />); | |
} | |
export default connect(state => ({ api: apiGenerator(state) }))(ComponentAPI); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment