-
-
Save Mukundhan-I2I/4329430eb8baf0a438b4ac4fd660d0f2 to your computer and use it in GitHub Desktop.
redux-sagas utils
This file contains 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 { createConstAndAction, createFetchTypesAndFuncs } from './utils'; | |
export const { PRODUCTS, products } = createFetchTypesAndFuncs('products'); | |
export const { LOAD_PRODUCTS, loadProducts } = createConstAndAction('LOAD_PRODUCTS'); | |
// these actions is for pull-to-load-more | |
export const { LOAD_MORE_PRODUCTS, loadMoreProducts } = createConstAndAction('LOAD_MORE_PRODUCTS'); |
This file contains 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 { createAction } from 'redux-actions'; | |
import camelCase from 'lodash-es/camelCase'; | |
import toUpper from 'lodash-es/toUpper'; | |
function createRequestTypes(base) { | |
const res = {}; | |
['REQUEST', 'SUCCESS', 'FAILURE'].forEach((type) => { | |
res[type] = `${base}_${type}`; | |
}); | |
return res; | |
} | |
// base is something like my_career; | |
// camelCase: myCareer | |
// capitalize: MY_CAREER | |
export function createFetchTypesAndFuncs(base) { | |
const camelCaseBase = camelCase(base); | |
const toUpperBase = toUpper(base); | |
const TYPES = createRequestTypes(toUpperBase); | |
return { | |
[toUpperBase]: TYPES, | |
[camelCaseBase]: { | |
// request(data) | |
request: createAction(TYPES.REQUEST), | |
// params here is request parameters | |
success: createAction(TYPES.SUCCESS, ({ response }) => response, ({ params }) => params), | |
failure: createAction(TYPES.FAILURE, ({ error }) => error, ({ params }) => params), | |
}, | |
}; | |
} | |
// input: LOAD_SOMETHING | |
// output: LOAD_SOMETHING, loadSomething | |
export function createConstAndAction(base) { | |
const camelCaseBase = camelCase(base); | |
return { | |
[base]: base, | |
[camelCaseBase]: createAction(base), | |
}; | |
} |
This file contains 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 { put, call } from 'redux-saga/effects'; | |
// resuable fetch Subroutine | |
// entity : user | repo | starred | stargazers | |
// apiFn : api.fetchUser | api.fetchRepo | ... | |
// id : login | fullName | |
// url : next page url. If not provided will use pass it to apiFn | |
export function* fetchEntity(entity, apiFn, params) { | |
yield put(entity.request(params)); | |
const { response, error } = yield call(apiFn, params); | |
if (response) { | |
yield put(entity.success({ response, params })); | |
} else { | |
yield put(entity.failure({ error, params })); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment