Skip to content

Instantly share code, notes, and snippets.

@mvbattan
Created December 11, 2017 15:15
Show Gist options
  • Save mvbattan/b118cb20145225e59df8ce44ab4134f8 to your computer and use it in GitHub Desktop.
Save mvbattan/b118cb20145225e59df8ce44ab4134f8 to your computer and use it in GitHub Desktop.
privateActionCreators
import SoccerService from '../services/SoccerService';
export const actions = createTypes([
'GET_MATCHES',
'GET_MATCHES_SUCCESS',
'GET_MATCHES_FAILURE',
'GET_PITCHES',
'GET_PITCHES_SUCCESS',
'GET_PITCHES_FAILURE'
], '@SOCCER');
const privateActionCreators = {
getMatchesSuccess: matches => ({
type: actions.GET_MATCHES_SUCCESS,
payload: matches
}),
getMatchesError: error => ({
type: actions.GET_MATCHES_ERROR,
payload: error
}),
getPitchesSuccess: pitches => ({
type: actions.GET_PITCHES_SUCCESS,
payload: pitches
}),
getPitchesFailure: error => ({
type: actions.GET_PITCHES_FAILURE,
payload: error
})
};
const actionCreators = {
getMatches: () => async dispatch => {
// Puts loadingMatches in true
dispatch({ type: actions.GET_MATCHES });
// -> api.get('/matches');
const response = await SoccerService.getMatches();
if (response.ok) {
// Stores matches, put loading in false
dispatch(privateActionCreators.getMatchesSuccess(response.data));
} else {
// Store the error, put loading in false
dispatch(privateActionCreators.getMatchesFailure(response.problem));
}
},
getPitches: clubId => async dispatch => {
dispatch({ type: actions.GET_PITCHES });
const response = await SoccerService.getPitches({ club_id: clubId });
if (response.ok) {
dispatch(privateActionCreators.getPitchesSuccess(response.data));
} else {
dispatch(privateActionCreators.getPitchesFailure(response.problem));
}
}
};
export default actionCreators;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment