Skip to content

Instantly share code, notes, and snippets.

@jmas
Last active February 16, 2018 14:17
Show Gist options
  • Save jmas/eb296c31fa95ad1cca6fca17f863c063 to your computer and use it in GitHub Desktop.
Save jmas/eb296c31fa95ad1cca6fca17f863c063 to your computer and use it in GitHub Desktop.
function processLoadingActions(loads, fails, successes, state, action) {
if (loads.indexOf(action.type) !== -1) {
return { ...state, isLoading: true };
}
if (fails.indexOf(action.type) !== -1) {
return { ...state, isLoading: false, error: action.error };
}
if (successes.indexOf(action.type) !== -1) {
return { ...state, isLoading: false, payload: action.payload };
}
return state;
}
// ... REDUCER!
function reducer(state, action) {
state = processLoadingActions(
['action_load'],
['action_fail'],
['action_success'],
state,
action
);
switch (action.type) {
case 'action_something':
return { ...state, data: action.payload };
default: return state;
}
}
const defaultState = {
isLoading: false,
error: null,
payload: null,
};
function loagingReducer(state=defaultState, action) {
if (action.type.lastIndexOf('_REQUEST') !== -1) {
return {
...state,
isLoading: true,
};
}
if (action.type.lastIndexOf('_SUCCESS') !== -1) {
return {
...state,
isLoading: false,
payload: action.payload,
};
}
if (action.type.lastIndexOf('_FAIL') !== -1) {
return {
...state,
isLoading: false,
error: action.error,
};
}
return state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment