This file contains hidden or 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
const createReducer = (initialState, reducerMap) => ( | |
state = initialState, | |
action, | |
) => { | |
// Pick a reducer from the object by key | |
const reducer = state[action.type] | |
if (!reducer) { | |
return state | |
} | |
// Run the reducer if present |
This file contains hidden or 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
const createReducer = (initialState, reducerMap) => ( | |
state = initialState, | |
action, | |
) => { | |
// Pick a reducer from the object by key | |
const reducer = reducerMap[action.type] | |
if (!reducer) { | |
return state | |
} | |
// Run the reducer if present |
This file contains hidden or 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
const makeSelectorCatNames = () => | |
createSelector( | |
(state) => state.cats.data, | |
(cats) => cats.data.reduce((accum, { name }) => `${accum} ${name}`, ''), | |
) |
This file contains hidden or 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
const selectorCatsData = (state) => state.cats.data | |
const selectorCatsLoading = (state) => state.cats.loading |
This file contains hidden or 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
const makeSelectorCatsData = () => | |
createSelector( | |
(state) => state.cats.data, | |
(cats) => cats, | |
) | |
const makeSelectorCatsLoading = () => | |
createSelector( | |
(state) => state.cats.loading, | |
(loading) => loading, | |
) |
This file contains hidden or 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' | |
const actionTypeCatsGetInit = 'CATS_GET_INIT' | |
const actionTypeCatsGetSuccess = 'CATS_GET_SUCCESS' | |
const actionTypeCatsGetError = 'CATS_GET_ERROR' | |
const actionCatsGetInit = () => ({ type: actionTypeCatsGetInit }) | |
const actionCatsGetSuccess = (payload) => ({ type: actionTypeCatsGetSuccess, payload }) | |
const actionCatsGetError = (error) => ({ type: actionTypeCatsGetError, payload: error }) |
This file contains hidden or 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
class CatsGetInit extends ActionStandard {} | |
class CatsGetSuccess extends ActionStandard {} | |
class CatsGetError extends ActionStandard {} | |
const reducerCatsLoading = createReducer( | |
false, | |
reducerLoadingMap(CatsGetInit, CatsGetSuccess, CatsGetError), | |
) | |
const reducerCatsData = createReducer(undefined, { | |
[CatsGetSuccess.type]: () => action.payload, |
This file contains hidden or 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
const catsGetAsync = async (dispatch) => { | |
dispatch(new CatsGetInit()) | |
try { | |
const res = await fetch('https://cats.com/api/v1/cats') | |
const body = await res.json() | |
dispatch(new CatsGetSuccess(body)) | |
} catch (error) { | |
dispatch(new CatsGetError(error)) | |
dispatch(new GlobalErrorInit(error)) | |
} |
This file contains hidden or 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
class GlobalErrorInit extends ActionStandard {} | |
class GlobalErrorClear extends ActionStandard {} | |
const reducerError = createReducer(undefined, { | |
[GlobalErrorInit.type]: (state, action) => action.payload, | |
[GlobalErrorClear.type]: (state, action) => undefined, | |
}) |
This file contains hidden or 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
class ActionStandard { | |
get static type () { | |
return `prefix/${this.name}` | |
} | |
constructor(payload) { | |
this.type = this.constructor.type | |
this.payload = payload | |
this.error = payload instanceof Error | |
} | |
} |