Skip to content

Instantly share code, notes, and snippets.

@gilankpam
Last active May 17, 2022 14:04
Show Gist options
  • Save gilankpam/a93a532a80cee2d016b38613173f4e50 to your computer and use it in GitHub Desktop.
Save gilankpam/a93a532a80cee2d016b38613173f4e50 to your computer and use it in GitHub Desktop.
redux-request-success-failure.js
function reduxHelper (actionName, fn) {
if (typeof actionName !== 'string') {
throw new Error('actionName must be a string')
}
if (typeof fn !== 'function') {
throw new Error('fn must be a function')
}
const actionNameUpper = actionName.toUpperCase()
const actionRequest = actionNameUpper + '_REQUEST'
const actionSuccess = actionNameUpper + '_SUCCESS'
const actionFailure = actionNameUpper + '_FAILURE'
const initialState = {
data: null,
loading: false,
error: null
}
const reducer = (state = initialState, action) => {
switch (action.type) {
case actionRequest:
return {
...state,
loading: true
}
case actionSuccess:
return {
...state,
loading: false,
data: action.data !== undefined ? action.data : null
}
case actionFailure:
return {
...state,
loading: false,
error: action.error
}
default:
return state
}
}
// we are not using arrow function, because there no arguments binding
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
const action = function () {
const args = arguments
return dispatch => {
dispatch({
type: actionRequest
})
try {
const result = fn.apply(null, args)
// It's a promise
if (typeof result.then === 'function') {
result.then(data => dispatch({
type: actionSuccess,
data
}))
.catch(error => dispatch({
type: actionFailure,
error
}))
} else {
dispatch({
type: actionSuccess,
data: result
})
}
} catch (error) {
dispatch({
type: actionFailure,
error
})
}
}
}
return {
action,
actionTypes: {
request: actionRequest,
success: actionSuccess,
failure: actionFailure
},
reducer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment