Skip to content

Instantly share code, notes, and snippets.

@kitze
Created January 10, 2016 01:18
Show Gist options
  • Save kitze/578e813a1e20839409d8 to your computer and use it in GitHub Desktop.
Save kitze/578e813a1e20839409d8 to your computer and use it in GitHub Desktop.
export const deleteFromArray = (itemKey, loadingKey, stateProperty) => (state, action) => {
return {
...state,
[loadingKey]: false,
[stateProperty]: state[stateProperty].filter(item => item[itemKey] !== action.value)
}
}
export const addToArray = (collection, loadingKey) => (state, action) => {
return {
...state,
[loadingKey]: false,
items: [action.value, ...state[collection]]
}
}
export const loading = loadingKey => state => {
return {
...state,
[loadingKey]: true
}
}
export const async = (type, cb) => params => {
return {
type,
promise: new Promise(cb)
}
}
export const addWithLoading = (key, property, loadingKey) => {
return {
[`${key}_INIT`]: loading(loadingKey),
[key]: addToArray(property, loadingKey)
}
}
export const deleteWithLoading = (key, property, loadingKey, deleteBy) => {
return {
[`${key}_INIT`]: loading(loadingKey),
[key]: deleteFromArray(deleteBy, loadingKey, property),
}
}
const replace = (propety, loadingKey) => (state, action) => {
return {
...state,
[propety]: action.value,
[loadingKey]: false
}
}
export const getWithLoading = (key, property, loadingKey) => {
return {
[`${key}_INIT`]: loading(loadingKey),
[key]: replace(property, loadingKey)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment