Created
January 10, 2016 01:18
-
-
Save kitze/578e813a1e20839409d8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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