cartReducer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { createSlice, current } from "@reduxjs/toolkit"
  2. import { v4 } from "uuid";
  3. import { history } from "../App";
  4. import { findObjectIndexById } from "../utills";
  5. import { ordersApi } from "./ordersReducer";
  6. //import { clearCartData, getCartData } from "./cartGoodsReducer";
  7. const cartSlice = createSlice({ //promiseReducer
  8. name: 'cart', //префикс типа наподобие AUTH_
  9. initialState: {
  10. goods: []
  11. },
  12. reducers: {
  13. restoreCart(state, action) {
  14. let goods = localStorage.cart?.goods ?? [];
  15. if (!goods) {
  16. goods = [];
  17. localStorage.cart = { goods: goods };
  18. }
  19. setStateData(state, goods, v4());
  20. return state;
  21. },
  22. cleanCart(state, action) {
  23. return cleanCartInt(state);
  24. },
  25. refreshCart(state, action) {
  26. state.uniqueId = v4();
  27. return state;
  28. },
  29. addGood(state, action) {
  30. let { _id, count = 1 } = action.payload.good;
  31. let goods = state.goods;
  32. let goodIdx = findObjectIndexById(goods, _id);
  33. let good;
  34. if (goodIdx < 0) {
  35. goodIdx = goods.length;
  36. good = { _id: _id, count: 0 }
  37. }
  38. else {
  39. good = goods[goodIdx];
  40. }
  41. count = good.count + count;
  42. if (count > 0) {
  43. good.count = count;
  44. state.goods[goodIdx] = good;
  45. state.uniqueId = v4()
  46. }
  47. return state;
  48. },
  49. deleteGood(state, action) {
  50. let { _id } = action.payload.good;
  51. let goods = state.goods;
  52. let goodIdx = findObjectIndexById(goods, _id);
  53. if (goodIdx >= 0) {
  54. goods.splice(goodIdx, 1);
  55. state.goods = goods;
  56. state.uniqueId = v4()
  57. }
  58. return state;
  59. }
  60. },
  61. extraReducers: builder => {
  62. builder.addMatcher(ordersApi.endpoints.addOrder.matchFulfilled,
  63. (state, { payload }) => {
  64. cleanCartInt(state);
  65. let orderId = payload.OrderUpsert._id;
  66. history.push(`/order/${orderId}`);
  67. });
  68. }
  69. })
  70. const getCartItemsCount = state => {
  71. return state.cart?.goods?.reduce((sum, g) => sum + g.count, 0);
  72. }
  73. function cleanCartInt(state) {
  74. localStorage.cart = { goods: [] };
  75. setStateData(state, [], v4());
  76. return state;
  77. }
  78. let actionAddGoodToCart = (good, count = 1) =>
  79. async (dispatch, state) => {
  80. dispatch(cartSlice.actions.addGood({ good: { ...good, count } }))
  81. }
  82. let actionDeleteGoodFromCart = good =>
  83. async dispatch => {
  84. dispatch(cartSlice.actions.deleteGood({ good }))
  85. }
  86. let actionRestoreCart = () =>
  87. async dispatch => {
  88. dispatch(cartSlice.actions.restoreCart({}))
  89. }
  90. let actionClearCart = () =>
  91. async dispatch => {
  92. dispatch(cartSlice.actions.cleanCart({}))
  93. }
  94. const setStateData = (state, goods, uniqueId = undefined) => {
  95. if (goods !== undefined)
  96. state.goods = goods;
  97. if (uniqueId !== undefined)
  98. state.uniqueId = uniqueId;
  99. }
  100. export {
  101. cartSlice, /*getCart,*/
  102. actionAddGoodToCart, actionDeleteGoodFromCart, actionRestoreCart,
  103. actionClearCart/*, actionClearCartData*/,
  104. getCartItemsCount
  105. };