index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { Box, Container } from '@mui/material';
  2. import { useEffect } from 'react';
  3. import { connect, useDispatch, useSelector } from 'react-redux';
  4. import { Navigate, Route, Routes, useParams, useSearchParams } from 'react-router-dom';
  5. import { actionGoodById } from '../../../actions/actionGoodById';
  6. import { actionCatById } from '../../../actions/actionCatById';
  7. import { actionPromiseClear, store, actionFeedCats } from '../../../reducers';
  8. import { actionFeedAdd, actionFeedClear, actionFeedGoods, actionFeedOrders } from '../../../reducers/feedReducer';
  9. import { CProtectedRoute } from '../../common/ProtectedRoute';
  10. import { CAdminGoodPage } from '../AdminGoodPage';
  11. import { AdminGoodsPage } from '../AdminGoodsPage';
  12. import { AdminCategoriesPage } from '../AdminCategoriesPage';
  13. import { CAdminCategoryPage } from '../AdminCategoryPage';
  14. import { AdminOrdersPage } from '../AdminOrdersPage';
  15. import { CAdminOrderPage } from '../AdminOrderPage';
  16. import { actionOrderById } from '../../../actions/actionOrderById';
  17. import { actionCatAll } from '../../../actions/actionCatAll';
  18. import { actionGoodsAll } from '../../../actions/actionGoodsAll';
  19. const AdminCategoryPageContainer = ({}) => {
  20. const dispatch = useDispatch();
  21. const params = useParams();
  22. dispatch(actionGoodsAll());
  23. useEffect(() => {
  24. if (params._id) {
  25. dispatch(actionCatById({ _id: params._id, promiseName: 'adminCatById' }));
  26. } else {
  27. dispatch(actionPromiseClear('adminCatById'));
  28. }
  29. }, [params._id]);
  30. return <CAdminCategoryPage />;
  31. };
  32. const AdminCategoriesPageContainer = ({ cats }) => {
  33. const dispatch = useDispatch();
  34. const [searchParams] = useSearchParams();
  35. const orderBy = searchParams.get('orderBy') || '_id';
  36. useEffect(() => {
  37. dispatch(actionFeedClear());
  38. dispatch(actionPromiseClear('feedCatAll'));
  39. dispatch(actionPromiseClear('categoryUpsert'));
  40. dispatch(actionFeedCats({ skip: 0, orderBy }));
  41. }, [orderBy]);
  42. useEffect(() => {
  43. dispatch(actionFeedCats({ skip: cats?.length || 0, orderBy }));
  44. window.onscroll = (e) => {
  45. if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
  46. const {
  47. feed,
  48. promise: { feedCatAll },
  49. } = store.getState();
  50. if (feedCatAll.status !== 'PENDING') {
  51. dispatch(actionFeedCats(feed.payload?.length || 0, orderBy));
  52. }
  53. }
  54. };
  55. return () => {
  56. dispatch(actionFeedClear());
  57. dispatch(actionPromiseClear('feedCatAll'));
  58. dispatch(actionPromiseClear('categoryUpsert'));
  59. window.onscroll = null;
  60. };
  61. }, []);
  62. useEffect(() => {
  63. if (cats.length) dispatch(actionFeedAdd(cats));
  64. }, [cats]);
  65. return <AdminCategoriesPage orderBy={orderBy} />;
  66. };
  67. const AdminGoodPageContainer = () => {
  68. const params = useParams();
  69. const dispatch = useDispatch();
  70. dispatch(actionCatAll());
  71. useEffect(() => {
  72. if (params._id) {
  73. dispatch(actionGoodById({ _id: params._id, promiseName: 'adminGoodById' }));
  74. } else {
  75. dispatch(actionPromiseClear('adminGoodById'));
  76. }
  77. }, [params._id]);
  78. return <CAdminGoodPage />;
  79. };
  80. const AdminGoodsPageContainer = ({ goods }) => {
  81. const dispatch = useDispatch();
  82. const [searchParams] = useSearchParams();
  83. const orderBy = searchParams.get('orderBy') || '_id';
  84. useEffect(() => {
  85. dispatch(actionFeedClear());
  86. dispatch(actionPromiseClear('feedGoodsAll'));
  87. dispatch(actionPromiseClear('goodUpsert'));
  88. dispatch(actionFeedGoods({ skip: 0, orderBy }));
  89. }, [orderBy]);
  90. useEffect(() => {
  91. dispatch(actionFeedGoods({ skip: goods?.length || 0, orderBy }));
  92. window.onscroll = (e) => {
  93. if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
  94. const {
  95. feed,
  96. promise: { feedGoodsAll },
  97. } = store.getState();
  98. if (feedGoodsAll.status !== 'PENDING') {
  99. dispatch(actionFeedGoods({ skip: feed.payload?.length || 0, orderBy }));
  100. }
  101. }
  102. };
  103. return () => {
  104. dispatch(actionFeedClear());
  105. dispatch(actionPromiseClear('feedGoodsAll'));
  106. dispatch(actionPromiseClear('goodUpsert'));
  107. window.onscroll = null;
  108. };
  109. }, []);
  110. useEffect(() => {
  111. if (goods?.length) store.dispatch(actionFeedAdd(goods));
  112. }, [goods]);
  113. return <AdminGoodsPage orderBy={orderBy} />;
  114. };
  115. const AdminOrdersPageContainer = ({ orders }) => {
  116. const dispatch = useDispatch();
  117. const [searchParams] = useSearchParams();
  118. const orderBy = searchParams.get('orderBy') || '_id';
  119. const status = searchParams.get('status') || 0;
  120. useEffect(() => {
  121. dispatch(actionFeedClear());
  122. dispatch(actionPromiseClear('feedOrdersAll'));
  123. dispatch(actionPromiseClear('orderUpsert'));
  124. dispatch(actionFeedOrders({ skip: 0, orderBy, status }));
  125. }, [orderBy, status]);
  126. useEffect(() => {
  127. dispatch(actionFeedOrders({ skip: orders?.length || 0, orderBy, status }));
  128. window.onscroll = (e) => {
  129. if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
  130. const {
  131. feed,
  132. promise: { feedOrdersAll },
  133. } = store.getState();
  134. if (feedOrdersAll.status !== 'PENDING') {
  135. dispatch(actionFeedOrders({ skip: feed.payload?.length || 0, orderBy, status }));
  136. }
  137. }
  138. };
  139. return () => {
  140. dispatch(actionFeedClear());
  141. dispatch(actionPromiseClear('feedOrdersAll'));
  142. dispatch(actionPromiseClear('orderUpsert'));
  143. window.onscroll = null;
  144. };
  145. }, []);
  146. useEffect(() => {
  147. if (orders?.length) store.dispatch(actionFeedAdd(orders));
  148. }, [orders]);
  149. return <AdminOrdersPage orderBy={orderBy} />;
  150. };
  151. const AdminOrderPageContainer = () => {
  152. const params = useParams();
  153. const dispatch = useDispatch();
  154. dispatch(actionPromiseClear('adminOrderById'));
  155. dispatch(actionGoodsAll());
  156. useEffect(() => {
  157. if (params._id) {
  158. dispatch(actionOrderById({ _id: params._id, promiseName: 'adminOrderById' }));
  159. } else {
  160. dispatch(actionOrderById('adminOrderById'));
  161. }
  162. }, [params._id]);
  163. return <CAdminOrderPage />;
  164. };
  165. const CAdminGoodsPageContainer = connect((state) => ({ goods: state.promise?.feedGoodsAll?.payload || [] }))(
  166. AdminGoodsPageContainer
  167. );
  168. const CAdminOrdersPageContainer = connect((state) => ({ orders: state.promise?.feedOrdersAll?.payload || [] }))(
  169. AdminOrdersPageContainer
  170. );
  171. const CAdminCategoriesPageContainer = connect((state) => ({ cats: state.promise?.feedCatAll?.payload || [] }))(
  172. AdminCategoriesPageContainer
  173. );
  174. const AdminLayoutPage = () => {
  175. return (
  176. <Box className="AdminLayoutPage">
  177. <Routes>
  178. <Route path="/" element={<Navigate to={'/admin/goods/'} />} exact />
  179. <Route path="/goods/" element={<CAdminGoodsPageContainer />} />
  180. <Route path="/good/" element={<AdminGoodPageContainer />} />
  181. <Route path="/good/:_id" element={<AdminGoodPageContainer />} />
  182. <Route path="/categories/" element={<CAdminCategoriesPageContainer />} />
  183. <Route path="/category/" element={<AdminCategoryPageContainer />} />
  184. <Route path="/category/:_id" element={<AdminCategoryPageContainer />} />
  185. <Route path="/orders/" element={<CAdminOrdersPageContainer />} />
  186. <Route path="/order/" element={<AdminOrderPageContainer />} />
  187. <Route path="/order/:_id" element={<AdminOrderPageContainer />} />
  188. </Routes>
  189. </Box>
  190. );
  191. };
  192. export { AdminLayoutPage };