Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. export const setEntities = (entityType, entities) => ({
  2. type: 'SET_ENTITIES',
  3. payload: { entities },
  4. meta: { entityType }
  5. })
  6. export const removeEntities = (entityType, ids) => ({
  7. type: 'REMOVE_ENTITIES',
  8. meta: { ids, entityType }
  9. })
  10. export const setEntity = (entityType, entity) => ({
  11. type: 'SET_ENTITY',
  12. payload: { entity },
  13. meta: { entityType }
  14. })
  15. export const removeEntity = (entityType, id) => ({
  16. type: 'REMOVE_ENTITY',
  17. meta: { id, entityType }
  18. })
  19. /////////////////////////////////////////////////////////////////////////////////////
  20. export const createEntity = (entityType, data) => ({
  21. type: 'CREATE_ENTITY',
  22. payload: data,
  23. meta: { entityType }
  24. })
  25. export const updateEntity = (entityType, id, data) => ({
  26. type: 'UPDATE_ENTITY',
  27. payload: data,
  28. meta: { entityType, id }
  29. })
  30. export const deleteEntity = (entityType, id) => ({
  31. type: 'DELETE_ENTITY',
  32. meta: { entityType, id }
  33. })
  34. ////////////////////////////////////////////////////////////////////////////////////
  35. const entitiesMiddleware = store => next => action => {
  36. next(action)
  37. switch (action.type) {
  38. case 'CREATE_ENTITY':
  39. return axios
  40. .post('/' + action.entityType, { form: action.payload })
  41. .then(res => next(setEntity(action.meta.entityType, res.data)))
  42.  
  43. case 'UPDATE_ENTITY':
  44. return axios
  45. .put('/' + action.meta.entityType + '/' + action.meta.id, { form: action.payload })
  46. .then(res => next(setEntity(action.meta.entityType, res.data)))
  47.  
  48. case 'DELETE_ENTITY':
  49. return axios
  50. .delete('/' + action.meta.entityType + '/' + action.meta.id)
  51. .then(res => next(removeEntity(action.meta.entityType, action.meta.id)))
  52. default:
  53. return false
  54. }
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////////
  57. setEntities('articles', [
  58. {id: 1, name: 'article 1'},
  59. {id: 2, name: 'article 2'},
  60. {id: 3, name: 'article 3'}
  61. ])
  62. removeEntities('articles', [1, 2, 3])
  63.  
  64. setEntity('articles', {id: 1, name: 'article 1'})
  65. removeEntity('articles', 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement