|
@@ -1,6 +1,9 @@
|
|
|
import { createSlice } from "@reduxjs/toolkit"
|
|
|
+import { useDispatch } from "react-redux";
|
|
|
+import { actionClearCart } from "./cartReducer";
|
|
|
import { categoryApi } from "./categoryReducer";
|
|
|
import { goodsApi } from "./goodsReducer";
|
|
|
+import { ordersApi } from "./ordersReducer";
|
|
|
|
|
|
const frontEndReducerSlice = createSlice({ //promiseReducer
|
|
|
name: 'frontend', //префикс типа наподобие AUTH_
|
|
@@ -15,7 +18,10 @@ const frontEndReducerSlice = createSlice({ //promiseReducer
|
|
|
return state;
|
|
|
},
|
|
|
setOrdersPaging(state, action) {
|
|
|
- state.ordersPaging = { fromPage: action.payload.page.fromPage, pageSize: action.payload.page.pageSize };
|
|
|
+ let { fromPage, pageSize } = action.payload.page;
|
|
|
+ fromPage = fromPage ?? state.goodsPaging?.fromPage;
|
|
|
+ pageSize = pageSize ?? state.goodsPaging?.pageSize;
|
|
|
+ state.ordersPaging = { fromPage, pageSize };
|
|
|
return state;
|
|
|
},
|
|
|
setOrdersSearch(state, action) {
|
|
@@ -23,7 +29,7 @@ const frontEndReducerSlice = createSlice({ //promiseReducer
|
|
|
return state;
|
|
|
},
|
|
|
setGoodsPaging(state, action) {
|
|
|
- let {fromPage, pageSize} = action.payload.page;
|
|
|
+ let { fromPage, pageSize } = action.payload.page;
|
|
|
fromPage = fromPage ?? state.goodsPaging?.fromPage;
|
|
|
pageSize = pageSize ?? state.goodsPaging?.pageSize;
|
|
|
state.goodsPaging = { fromPage, pageSize };
|
|
@@ -41,6 +47,10 @@ const frontEndReducerSlice = createSlice({ //promiseReducer
|
|
|
setCurrentGood(state, action.payload._id);
|
|
|
return state;
|
|
|
},
|
|
|
+ setCurrentOrder(state, action) {
|
|
|
+ setCurrentOrder(state, action.payload._id);
|
|
|
+ return state;
|
|
|
+ },
|
|
|
},
|
|
|
extraReducers: builder => {
|
|
|
builder.addMatcher(goodsApi.endpoints.getGoodsCount.matchFulfilled,
|
|
@@ -51,6 +61,15 @@ const frontEndReducerSlice = createSlice({ //promiseReducer
|
|
|
(state, { payload }) => {
|
|
|
state.goodsPaging.fromPage = 0;
|
|
|
});
|
|
|
+ builder.addMatcher(ordersApi.endpoints.getOrdersCount.matchFulfilled,
|
|
|
+ (state, { payload }) => {
|
|
|
+ state.orders = { ordersCount: { payload: payload.OrderCount } }
|
|
|
+ });
|
|
|
+ builder.addMatcher(ordersApi.endpoints.addOrder.matchFulfilled,
|
|
|
+ (state, { payload }) => {
|
|
|
+ const dispatch = useDispatch();
|
|
|
+ dispatch(actionClearCart());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
})
|
|
@@ -76,6 +95,11 @@ let actionSetCurrentCategory = (_id) =>
|
|
|
dispatch(frontEndReducerSlice.actions.setCurrentCategory({ _id }))
|
|
|
}
|
|
|
|
|
|
+let actionSetCurrentOrder = (_id) =>
|
|
|
+ async dispatch => {
|
|
|
+ dispatch(frontEndReducerSlice.actions.setCurrentOrder({ _id }))
|
|
|
+ }
|
|
|
+
|
|
|
let actionSetCurrentGood = (_id) =>
|
|
|
async dispatch => {
|
|
|
dispatch(frontEndReducerSlice.actions.setCurrentGood({ _id }))
|
|
@@ -92,31 +116,42 @@ let actionSetGoodsSearch = (searchStr) =>
|
|
|
}
|
|
|
|
|
|
const currentCategory = 'currentCategory';
|
|
|
-
|
|
|
const getCurrentCategory = state => {
|
|
|
let result = state.frontend[currentCategory]?.payload
|
|
|
return result;
|
|
|
}
|
|
|
-
|
|
|
const setCurrentCategory = (state, id) => {
|
|
|
return state[currentCategory] = { payload: id };
|
|
|
}
|
|
|
-const currentGood = 'currentGood';
|
|
|
|
|
|
+const currentGood = 'currentGood';
|
|
|
const getCurrentGood = state => {
|
|
|
let result = state.frontend[currentGood]?.payload
|
|
|
return result;
|
|
|
}
|
|
|
-
|
|
|
const setCurrentGood = (state, id) => {
|
|
|
return state[currentGood] = { payload: id };
|
|
|
}
|
|
|
|
|
|
+const currentOrder = 'currentOrder';
|
|
|
+const getCurrentOrder = state => {
|
|
|
+ let result = state.frontend[currentOrder]?.payload
|
|
|
+ return result;
|
|
|
+}
|
|
|
+const setCurrentOrder = (state, id) => {
|
|
|
+ return state[currentOrder] = { payload: id };
|
|
|
+}
|
|
|
+
|
|
|
const getGoodsCount = state => {
|
|
|
let result = state.frontend.goods.goodsCount?.payload ?? 0;
|
|
|
return result;
|
|
|
}
|
|
|
+const getOrdersCount = state => {
|
|
|
+ let result = state.frontend.orders.ordersCount?.payload ?? 0;
|
|
|
+ return result;
|
|
|
+}
|
|
|
|
|
|
|
|
|
export { frontEndReducer, actionSetSidebar, actionSetOrdersPaging, actionSetOrderSearch, actionSetGoodsPaging, actionSetGoodsSearch };
|
|
|
-export { getCurrentCategory, getCurrentGood, actionSetCurrentCategory, actionSetCurrentGood, getGoodsCount }
|
|
|
+export { getCurrentCategory, getCurrentGood, actionSetCurrentCategory, actionSetCurrentGood, getGoodsCount, getCurrentOrder, actionSetCurrentOrder }
|
|
|
+export { getOrdersCount };
|