|
@@ -1,150 +1,76 @@
|
|
|
import { Box, Grid } from "@mui/material";
|
|
|
import { useEffect } from "react";
|
|
|
-import { connect, useDispatch, useSelector } from "react-redux";
|
|
|
-import { Navigate, Route, Routes, useLocation, useParams, useSearchParams } from "react-router-dom";
|
|
|
-import { actionCatById } from "../../actions/actionCatById";
|
|
|
+import { connect } from "react-redux";
|
|
|
+import { Navigate, Route, Routes, useLocation, useParams } from "react-router-dom";
|
|
|
import { actionGoodById } from "../../actions/actionGoodById";
|
|
|
-import { actionGoodsFind } from "../../actions/actionGoodsFind";
|
|
|
+import { actionGoodsPopular } from "../../actions/actionGoodsPopular";
|
|
|
import { actionOrders } from "../../actions/actionOrders";
|
|
|
-import { actionPromiseClear, store } from "../../reducers";
|
|
|
-import { actionFeedAdd, actionFeedCategoryGoods, actionFeedClear, actionFeedGoodsFind } from "../../reducers/feedReducer";
|
|
|
import { AdminLayoutPage } from "../admin/AdminLayoutPage";
|
|
|
import { CCartPage } from "../CartPage";
|
|
|
import { GoodList } from "../common/GoodList";
|
|
|
import { CProtectedRoute } from "../common/ProtectedRoute";
|
|
|
import { CDashboardPage } from "../DashboardPage";
|
|
|
import { GoodPage } from "../GoodPage";
|
|
|
-import { CGoodsPage, GoodsPage } from "../GoodsPage";
|
|
|
import { Aside } from "../layout/Aside";
|
|
|
import Content from "../layout/Content";
|
|
|
import { Footer } from "../layout/Footer";
|
|
|
import { Header } from "../layout/Header";
|
|
|
import { MainPage } from "../MainPage";
|
|
|
+import { CAdminGoodsPageContainer } from "./GoodsPageContainer";
|
|
|
+import { CAdminGoodsSearchPageContainer } from "./GoodsSearchPageContainer";
|
|
|
|
|
|
-const GoodsPageContainer = () => {
|
|
|
+const GoodPageContainer = ({ onLoad }) => {
|
|
|
const params = useParams();
|
|
|
- const category = useSelector((state) => state.promise?.catById?.payload || null);
|
|
|
- const goods = useSelector((state) => state.promise?.feedCategoryGoods?.payload || []);
|
|
|
- const feed = useSelector((state) => state.feed?.payload || []);
|
|
|
- const dispatch = useDispatch();
|
|
|
- const [searchParams] = useSearchParams();
|
|
|
- const orderBy = searchParams.get("orderBy") || "_id";
|
|
|
-
|
|
|
useEffect(() => {
|
|
|
- if (params._id) {
|
|
|
- dispatch(actionCatById({ _id: params._id }));
|
|
|
- }
|
|
|
+ onLoad({ _id: params._id });
|
|
|
+ }, []);
|
|
|
|
|
|
- return () => {
|
|
|
- dispatch(actionPromiseClear("catById"));
|
|
|
- };
|
|
|
- }, [params._id]);
|
|
|
+ return <GoodPage />;
|
|
|
+};
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- dispatch(actionFeedClear());
|
|
|
- dispatch(actionPromiseClear("feedCategoryGoods"));
|
|
|
- dispatch(actionFeedCategoryGoods({ category, orderBy, skip: 0 }));
|
|
|
- }, [orderBy, category]);
|
|
|
+const CGoodPageContainer = connect(null, {
|
|
|
+ onLoad: ({ _id }) => actionGoodById({ _id }),
|
|
|
+})(GoodPageContainer);
|
|
|
|
|
|
+const DashboardPageContainer = ({ onLoad }) => {
|
|
|
useEffect(() => {
|
|
|
- dispatch(actionFeedCategoryGoods({ skip: goods?.length || 0, orderBy }));
|
|
|
- window.onscroll = (e) => {
|
|
|
- if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
|
|
|
- const {
|
|
|
- feed,
|
|
|
- promise: { feedCategoryGoods },
|
|
|
- } = store.getState();
|
|
|
-
|
|
|
- if (feedCategoryGoods.status !== "PENDING") {
|
|
|
- dispatch(actionFeedCategoryGoods({ skip: feed.payload?.length || 0, orderBy }));
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- return () => {
|
|
|
- dispatch(actionFeedClear());
|
|
|
- dispatch(actionPromiseClear("feedCategoryGoods"));
|
|
|
- window.onscroll = null;
|
|
|
- };
|
|
|
+ onLoad();
|
|
|
}, []);
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- if (goods?.length) store.dispatch(actionFeedAdd(goods));
|
|
|
- }, [goods]);
|
|
|
- return <CGoodsPage goods={feed} />;
|
|
|
+ return <CDashboardPage />;
|
|
|
};
|
|
|
|
|
|
-const GoodsSearchPageContainer = () => {
|
|
|
- const goods = useSelector((state) => state.promise?.feedGoodsFind?.payload || []);
|
|
|
- const feed = useSelector((state) => state.feed?.payload || []);
|
|
|
- const dispatch = useDispatch();
|
|
|
- const [searchParams] = useSearchParams();
|
|
|
- const orderBy = searchParams.get("orderBy") || "_id";
|
|
|
- const text = searchParams.get("text") || "";
|
|
|
+const CDashboardPageContainer = connect(null, {
|
|
|
+ onLoad: () => actionOrders(),
|
|
|
+})(DashboardPageContainer);
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- dispatch(actionFeedClear());
|
|
|
- dispatch(actionPromiseClear("feedGoodsFind"));
|
|
|
- dispatch(actionFeedGoodsFind({ text, orderBy, skip: 0 }));
|
|
|
- }, [orderBy, text]);
|
|
|
-
|
|
|
- useEffect(() => {
|
|
|
- dispatch(actionFeedGoodsFind({ text, skip: goods?.length || 0, orderBy }));
|
|
|
- window.onscroll = (e) => {
|
|
|
- if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
|
|
|
- const {
|
|
|
- feed,
|
|
|
- promise: { feedGoodsFind },
|
|
|
- } = store.getState();
|
|
|
+const CGoodsList = connect((state) => ({ goods: state.promise?.pageGoodsFind?.payload || [] }))(GoodList);
|
|
|
|
|
|
- if (feedGoodsFind.status !== "PENDING") {
|
|
|
- dispatch(actionFeedGoodsFind({ text, skip: feed.payload?.length || 0, orderBy }));
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
- return () => {
|
|
|
- dispatch(actionFeedClear());
|
|
|
- dispatch(actionPromiseClear("feedGoodsFind"));
|
|
|
- window.onscroll = null;
|
|
|
- };
|
|
|
- }, []);
|
|
|
+const GoodsListContainer = ({ onLoad }) => {
|
|
|
+ const params = useParams();
|
|
|
|
|
|
useEffect(() => {
|
|
|
- if (goods?.length) store.dispatch(actionFeedAdd(goods));
|
|
|
- }, [goods]);
|
|
|
+ onLoad({ text: params.searchData, promiseName: "pageGoodsFind" });
|
|
|
+ }, [params.searchData]);
|
|
|
|
|
|
- return <GoodsPage goods={feed} />;
|
|
|
+ return <CGoodsList />;
|
|
|
};
|
|
|
|
|
|
-const GoodPageContainer = () => {
|
|
|
- const params = useParams();
|
|
|
- const dispatch = useDispatch();
|
|
|
- useEffect(() => {
|
|
|
- dispatch(actionGoodById({ _id: params._id }));
|
|
|
- }, []);
|
|
|
-
|
|
|
- return <GoodPage />;
|
|
|
-};
|
|
|
+const CGoodsListContainer = connect(null, {
|
|
|
+ onLoad: ({ text, promiseName }) => actionOrders({ text, promiseName }),
|
|
|
+})(GoodsListContainer);
|
|
|
|
|
|
-const DashboardPageContainer = () => {
|
|
|
- const dispatch = useDispatch();
|
|
|
+const MainPageContainer = ({ onLoad, goods }) => {
|
|
|
useEffect(() => {
|
|
|
- dispatch(actionOrders());
|
|
|
+ onLoad();
|
|
|
}, []);
|
|
|
|
|
|
- return <CDashboardPage />;
|
|
|
+ return <MainPage goods={goods || []} />;
|
|
|
};
|
|
|
|
|
|
-const CGoodsList = connect((state) => ({ goods: state.promise?.pageGoodsFind?.payload || [] }))(GoodList);
|
|
|
-
|
|
|
-const GoodsListContainer = () => {
|
|
|
- const params = useParams();
|
|
|
- const dispatch = useDispatch();
|
|
|
- useEffect(() => {
|
|
|
- dispatch(actionGoodsFind({ text: params.searchData, promiseName: "pageGoodsFind" }));
|
|
|
- }, [params.searchData]);
|
|
|
-
|
|
|
- return <CGoodsList />;
|
|
|
-};
|
|
|
+const CMainPageContainer = connect((state) => ({ goods: state.promise?.goodsPopular?.payload || [] }), {
|
|
|
+ onLoad: () => actionGoodsPopular(),
|
|
|
+})(MainPageContainer);
|
|
|
|
|
|
export const LayoutPage = () => {
|
|
|
const location = useLocation();
|
|
@@ -160,7 +86,7 @@ export const LayoutPage = () => {
|
|
|
<Grid xs={location.pathname.match(/(\/categor)|(\/good)|(\/order)|(\/admin)+/) ? 11 : 14} item>
|
|
|
<Content>
|
|
|
<Routes>
|
|
|
- <Route path="/" exact element={<MainPage />} />
|
|
|
+ <Route path="/" exact element={<CMainPageContainer />} />
|
|
|
<Route
|
|
|
path="/cart"
|
|
|
exact
|
|
@@ -170,11 +96,10 @@ export const LayoutPage = () => {
|
|
|
</CProtectedRoute>
|
|
|
}
|
|
|
/>
|
|
|
- <Route path="/search/:searchData/" element={<GoodsListContainer />} exact />
|
|
|
- <Route path="/category/:_id" element={<GoodsPageContainer />} />
|
|
|
- <Route path="/category/" element={<GoodsPageContainer />} />
|
|
|
- <Route path="/good/:_id" element={<GoodPageContainer />} />
|
|
|
- <Route path="/goods/search" element={<GoodsSearchPageContainer />} />
|
|
|
+ {/* <Route path="/search/:searchData/" element={<CGoodsListContainer />} exact /> */}
|
|
|
+ <Route path="/category/:_id" element={<CAdminGoodsPageContainer />} />
|
|
|
+ <Route path="/good/:_id" element={<CGoodPageContainer />} />
|
|
|
+ <Route path="/goods/search" element={<CAdminGoodsSearchPageContainer />} />
|
|
|
<Route
|
|
|
path="/admin/*"
|
|
|
exact
|
|
@@ -189,7 +114,7 @@ export const LayoutPage = () => {
|
|
|
exact
|
|
|
element={
|
|
|
<CProtectedRoute roles={["active"]} fallback="/">
|
|
|
- <DashboardPageContainer />
|
|
|
+ <CDashboardPageContainer />
|
|
|
</CProtectedRoute>
|
|
|
}
|
|
|
/>
|