CategoryReducer.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export const CategoryReducer = (state={}, { type, value={} }) => {
  2. if (type === 'CATEGORY_CREATE') {
  3. if (Object.entries(value).length !== 0) {
  4. return {
  5. ...state,
  6. ...value
  7. }
  8. }
  9. else
  10. return state
  11. }
  12. if (type === 'CATEGORY_CHANGE') {
  13. if (Object.entries(value).length !== 0) {
  14. for (const item of Object.entries(state)) {
  15. if(item[1]['_id'] === value['_id']){
  16. item[1]['goods'] = value['goods']
  17. }
  18. else if(item[1]['subCategories'] !== null){
  19. for (const itemSub of Object.entries(item[1]['subCategories'])) {
  20. if(itemSub[1]['_id'] === value['_id']){
  21. itemSub[1]['goods'] = value['goods']
  22. }
  23. else if(itemSub[1]['subCategories'] !== null){
  24. for (const itemSubSub of Object.entries(itemSub[1]['subCategories'])) {
  25. if(itemSubSub[1]['_id'] === value['_id']){
  26. itemSubSub[1]['goods'] = value['goods']
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. return {
  34. ...state
  35. }
  36. }
  37. else
  38. return {...state}
  39. }
  40. if (type === 'CATEGORY_REMOVE') {
  41. return {}
  42. }
  43. return state
  44. }
  45. export const actionCategoryCreate = value => ({ type: 'CATEGORY_CREATE', value })
  46. export const actionCategoryChange = value => ({ type: 'CATEGORY_CHANGE', value })
  47. export const actionCategoryRemove = () => ({ type: 'CATEGORY_REMOVE' })