Skip to content

Instantly share code, notes, and snippets.

View mvbattan's full-sized avatar

Batu mvbattan

  • Buenos Aires
View GitHub Profile
@mvbattan
mvbattan / reducer.js
Created December 11, 2017 21:43
Complete Reducer - before
const reducerDescription: {
[actions.GET_MATCHES]: onLoading(),
[actions.GET_MATCHES_SUCCESS]: onSuccess(),
[actions.GET_MATCHES_FAILURE]: onFailure(),
[actions.GET_PITCHES]: onLoading(),
[actions.GET_PITCHES_SUCCESS]: onSuccess(),
[actions.GET_PITCHES_FAILURE]: onFailure(),
[actions.INCREMENT_COUNTER]: onAdd()
};
@mvbattan
mvbattan / actions.js
Last active December 19, 2017 23:59
Compose Injections
const actionCreators = {
fetchSomething: () => composeInjections(
baseThunkAction(actions.FETCH, 'fetchTarget', Service.fetch),
withPostSuccess(dispatch => dispatch(navigationActions.push('/successRoute'))),
withStatusHandling({ 404: dispatch => dispatch(navigationActions.push('/failureRoute')) })
)
}
@mvbattan
mvbattan / actions.js
Last active December 14, 2017 13:07
Async with behavior
const actionCreators = {
fetchSomething: () => async dispatch => {
dispatch({ type: actions.FETCH });
const response = Service.fetch();
if (response.ok) {
dispatch({ type: actions.FETCH_SUCCESS, payload: response.data });
dispatch(navigationActions.push('/successRoute');
} else {
dispatch({ type: actions.FETCH_ERROR, payload: response.error });
if (response.status === 404) {
@mvbattan
mvbattan / actions.js
Created December 11, 2017 18:02
Create Thunk Action
import SoccerService from '../services/SoccerService';
export const actions = createTypes(completeTypes['GET_MATCHES','GET_PITCHES'], '@SOCCER');
const actionCreators = {
getMatches: () =>
createThunkAction(actions.GET_MATCHES, 'matches', SoccerService.getMatches),
getPitches: clubId =>
createThunkAction(actions.GET_PITCHES, 'pitches', SoccerService.getPitches, () => clubId)
};
@mvbattan
mvbattan / reducer.js
Created December 11, 2017 17:53
Selectors
// if action.payload is like: { matches: [] };
const reducerDescription = {
// This will store the array of matches instead of the whole object comming from payload
[actions.GET_MATCHES_SUCCESS]: onSuccess(action => action.payload.matches)
};
@mvbattan
mvbattan / actions.js
Created December 11, 2017 17:44
complete types
export const actions = createTypes(
completeTypes(['GET_MATCHES', 'GET_PITCHES'], ['INCREMENT_COUNTER']),
'@@SOCCER'
);
@mvbattan
mvbattan / reducer.js
Last active December 14, 2017 13:06
Complete state
const stateDescription = {
matches: [],
pitches: [],
counter: 0
};
const initialState = completeState(stateDescription, ['counter']);
@mvbattan
mvbattan / reducer.js
Created December 11, 2017 17:39
Complete reducer
const reducerDescription: {
primaryActions: [actions.GET_MATCHES, actions.GET_PITCHES],
override: {
[actions.INCREMENT_COUNTER]: onAdd()
}
}
export default createReducer(initialState, completeReducer(reducerDescription))
@mvbattan
mvbattan / utils.js
Last active December 11, 2017 17:49
Effects
export function onLoading(selector = (action, state) => true) {
return (state, action) => ({ ...state, [`${action.target}Loading`]: selector(action, state) });
}
export function onSuccess(selector = (action, state) => action.payload) {
return (state, action) => ({
...state,
[`${action.target}Loading`]: false,
[action.target]: selector(action, state),
[`${action.target}Error`]: null
@mvbattan
mvbattan / reducer.js
Created December 11, 2017 17:31
Handlers with effects
const reducerDescription = {
[actions.MATCHES]: onLoading(),
[actions.MATCHES_SUCCESS]: onSuccess(),
[actions.MATCHES_FAILURE]: onFailure(),
[actions.PITCHES]: onLoading(),
[actions.PITCHES_SUCCESS]: onSuccess(),
[actions.PITCHES_FAILURE]: onFailure()
};
export default createReducer(initialState, reducerDescription);