index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import { Box } from "@mui/material";
  2. import { useEffect } from "react";
  3. import { connect, useDispatch } from "react-redux";
  4. import { Navigate, Route, Routes, useParams, useSearchParams } from "react-router-dom";
  5. import { actionPromiseClear, actionFeedCats, actionFeedGoodsFind, actionFeedOrdersFind, actionFeedCatsFind } from "../../../reducers";
  6. import { actionFeedAdd, actionFeedGoods, actionFeedOrders, actionFeedUsers, actionFeedUsersFind } from "../../../reducers/feedReducer";
  7. import { CAdminGoodPage } from "../AdminGoodPage";
  8. import { AdminGoodsPage } from "../AdminGoodsPage";
  9. import { AdminCategoriesPage } from "../AdminCategoriesPage";
  10. import { CAdminCategoryPage } from "../AdminCategoryPage";
  11. import { AdminOrdersPage } from "../AdminOrdersPage";
  12. import { CAdminOrderPage } from "../AdminOrderPage";
  13. import { actionCatAll } from "../../../actions/actionCatAll";
  14. import { CAdminCategoryTree } from "../AdminCategoryTree";
  15. import { AdminUsersPage } from "../AdminUsersPage";
  16. import { CAdminUserPage } from "../AdminUserPage.js";
  17. import { actionAdminCategoryPage } from "../../../actions/actionAdminCategoryPage";
  18. import { actionAdminCategoryPageClear } from "../../../actions/actionAdminCategoryPageClear";
  19. import { actionAdminCategoriesPage } from "../../../actions/actionAdminCategoriesPage";
  20. import { actionAdminCategoriesPageClear } from "../../../actions/actionAdminCategoriesPageClear";
  21. import { actionAdminCategoriesSearchPageClear } from "../../../actions/actionAdminCategoriesSearchPageClear";
  22. import { actionAdminCategoriesSearchPage } from "../../../actions/actionAdminCategoriesSearchPage";
  23. import { actionAdminGoodsPageClear } from "../../../actions/actionAdminGoodsPageClear";
  24. import { actionAdminGoodsPage } from "../../../actions/actionAdminGoodsPage";
  25. import { actionAdminGoodPage } from "../../../actions/actionAdminGoodPage";
  26. import { actionAdminGoodPageClear } from "../../../actions/actionAdminGoodPageClear";
  27. import { actionAdminGoodsSearchPageClear } from "../../../actions/actionAdminGoodsSearchPageClear";
  28. import { actionAdminGoodsSearchPage } from "../../../actions/actionAdminGoodsSearchPage";
  29. import { actionAdminOrdersPageClear } from "../../../actions/actionAdminOrdersPageClear";
  30. import { actionAdminOrdersPage } from "../../../actions/actionAdminOrdersPage";
  31. import { actionAdminOrdersSearchPageClear } from "../../../actions/actionAdminOrdersSearchPageClear";
  32. import { actionAdminOrderPage } from "../../../actions/actionAdminOrderPage";
  33. import { actionAdminOrderPageClear } from "../../../actions/actionAdminOrderPageClear";
  34. import { actionAdminUsersSearchPageClear } from "../../../actions/actionAdminUsersSearchPageClear";
  35. import { actionAdminUsersSearchPage } from "../../../actions/actionAdminUsersSearchPage";
  36. import { actionAdminUsersPageClear } from "../../../actions/actionAdminUsersPageClear";
  37. import { actionAdminUsersPage } from "../../../actions/actionAdminUsersPage";
  38. import { actionAdminUserPageClear } from "../../../actions/actionAdminUserPageClear";
  39. import { actionAdminUserPage } from "../../../actions/actionAdminUserPage";
  40. import { InfScroll } from "../../common/InfScroll";
  41. const AdminCategoryTreePageContainer = ({ onLoad, onUnmount }) => {
  42. useEffect(() => {
  43. onLoad();
  44. return () => {
  45. onUnmount();
  46. };
  47. }, []);
  48. return <CAdminCategoryTree />;
  49. };
  50. const CAdminCategoryTreePageContainer = connect(null, {
  51. onUnmount: () => actionCatAll(),
  52. onLoad: () => actionPromiseClear("catAll"),
  53. })(AdminCategoryTreePageContainer);
  54. const AdminCategoryPageContainer = ({ onUnmount, onLoad }) => {
  55. const params = useParams();
  56. useEffect(() => {
  57. return () => {
  58. onUnmount();
  59. };
  60. }, []);
  61. useEffect(() => {
  62. onLoad(params._id);
  63. }, [params._id]);
  64. return <CAdminCategoryPage />;
  65. };
  66. const CAdminCategoryPageContainer = connect(null, {
  67. onUnmount: () => actionAdminCategoryPageClear(),
  68. onLoad: (_id) => actionAdminCategoryPage({ _id }),
  69. })(AdminCategoryPageContainer);
  70. const AdminCategoriesPageContainer = ({ feed, cats, promiseStatus, onLoad, onUnmount, onScroll }) => {
  71. const dispatch = useDispatch();
  72. const [searchParams] = useSearchParams();
  73. const orderBy = searchParams.get("orderBy") || "_id";
  74. useEffect(() => {
  75. onLoad({ orderBy });
  76. return () => {
  77. onUnmount();
  78. };
  79. }, [orderBy]);
  80. useEffect(() => {
  81. window.onscroll = (e) => {
  82. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  83. if (promiseStatus !== "PENDING") {
  84. onScroll({ feed, orderBy });
  85. }
  86. }
  87. };
  88. return () => {
  89. window.onscroll = null;
  90. };
  91. }, [feed, promiseStatus]);
  92. useEffect(() => {
  93. if (cats.length) dispatch(actionFeedAdd(cats));
  94. }, [cats]);
  95. return <AdminCategoriesPage orderBy={orderBy} />;
  96. };
  97. const CAdminCategoriesPageContainer = connect(
  98. (state) => ({
  99. cats: state.promise?.feedCatAll?.payload || [],
  100. feed: state.feed?.payload || [],
  101. promiseStatus: state.promise?.feedCatAll?.status || null,
  102. }),
  103. {
  104. onUnmount: () => actionAdminCategoriesPageClear(),
  105. onLoad: ({ orderBy }) => actionAdminCategoriesPage({ orderBy }),
  106. onScroll: ({ feed, orderBy }) => actionFeedCats({ skip: feed?.length || 0, orderBy }),
  107. }
  108. )(AdminCategoriesPageContainer);
  109. const AdminCategoriesSearchPageContainer = ({ feed, cats, promiseStatus, onLoad, onUnmount, onScroll }) => {
  110. const dispatch = useDispatch();
  111. const [searchParams] = useSearchParams();
  112. const orderBy = searchParams.get("orderBy") || "_id";
  113. const text = searchParams.get("text") || "";
  114. useEffect(() => {
  115. onLoad({ orderBy, text });
  116. return () => {
  117. onUnmount();
  118. };
  119. }, [orderBy, text]);
  120. useEffect(() => {
  121. window.onscroll = (e) => {
  122. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  123. if (promiseStatus !== "PENDING") {
  124. onScroll({ feed, orderBy, text });
  125. }
  126. }
  127. };
  128. return () => {
  129. window.onscroll = null;
  130. };
  131. }, [promiseStatus, feed, text]);
  132. useEffect(() => {
  133. if (cats?.length) dispatch(actionFeedAdd(cats));
  134. }, [cats]);
  135. return <AdminCategoriesPage orderBy={orderBy} />;
  136. };
  137. const CAdminCategoriesSearchPageContainer = connect(
  138. (state) => ({
  139. cats: state.promise?.feedCatsFind?.payload || [],
  140. feed: state.feed?.payload || [],
  141. promiseStatus: state.promise?.feedCatsFind?.status || null,
  142. }),
  143. {
  144. onUnmount: () => actionAdminCategoriesSearchPageClear(),
  145. onLoad: ({ orderBy, text }) => actionAdminCategoriesSearchPage({ orderBy, text }),
  146. onScroll: ({ feed, orderBy, text }) => actionFeedCatsFind({ text, skip: feed?.length || 0, orderBy }),
  147. }
  148. )(AdminCategoriesSearchPageContainer);
  149. const AdminGoodPageContainer = ({ onUnmount, onLoad }) => {
  150. const params = useParams();
  151. useEffect(() => {
  152. onLoad(params._id);
  153. return () => {
  154. onUnmount();
  155. };
  156. }, []);
  157. return <CAdminGoodPage />;
  158. };
  159. const CAdminGoodPageContainer = connect(null, {
  160. onUnmount: () => actionAdminGoodPageClear(),
  161. onLoad: (_id) => actionAdminGoodPage({ _id }),
  162. })(AdminGoodPageContainer);
  163. const AdminGoodsPageContainer = ({ feed, goods, promiseStatus, onLoad, onUnmount, onScroll }) => {
  164. const [searchParams] = useSearchParams();
  165. const orderBy = searchParams.get("orderBy") || "_id";
  166. useEffect(() => {
  167. onLoad({ orderBy });
  168. return () => {
  169. onUnmount();
  170. };
  171. }, [orderBy]);
  172. return (
  173. <InfScroll
  174. items={goods}
  175. component={AdminGoodsPage}
  176. promiseStatus={promiseStatus}
  177. onScroll={() => onScroll({ feed, orderBy })}
  178. orderBy={orderBy}
  179. />
  180. );
  181. };
  182. const CAdminGoodsPageContainer = connect(
  183. (state) => ({
  184. goods: state.promise?.feedGoodsAll?.payload || [],
  185. feed: state.feed?.payload || [],
  186. promiseStatus: state.promise?.feedGoodsAll?.status || null,
  187. }),
  188. {
  189. onUnmount: () => actionAdminGoodsPageClear(),
  190. onLoad: ({ orderBy }) => actionAdminGoodsPage({ orderBy }),
  191. onScroll: ({ feed, orderBy }) => actionFeedGoods({ skip: feed?.length || 0, orderBy }),
  192. }
  193. )(AdminGoodsPageContainer);
  194. const AdminGoodsSearchPageContainer = ({ feed, goods, promiseStatus, onLoad, onUnmount, onScroll }) => {
  195. const dispatch = useDispatch();
  196. const [searchParams] = useSearchParams();
  197. const orderBy = searchParams.get("orderBy") || "_id";
  198. const text = searchParams.get("text") || "";
  199. useEffect(() => {
  200. onLoad({ orderBy, text });
  201. return () => {
  202. onUnmount();
  203. };
  204. }, [orderBy, text]);
  205. useEffect(() => {
  206. window.onscroll = (e) => {
  207. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  208. if (promiseStatus !== "PENDING") {
  209. onScroll({ feed, orderBy, text });
  210. }
  211. }
  212. };
  213. return () => {
  214. window.onscroll = null;
  215. };
  216. }, [promiseStatus, feed, text]);
  217. useEffect(() => {
  218. if (goods?.length) dispatch(actionFeedAdd(goods));
  219. }, [goods]);
  220. return <AdminGoodsPage orderBy={orderBy} />;
  221. };
  222. const CAdminGoodsSearchPageContainer = connect(
  223. (state) => ({
  224. goods: state.promise?.feedGoodsFind?.payload || [],
  225. feed: state.feed?.payload || [],
  226. promiseStatus: state.promise?.feedGoodsFind?.status || null,
  227. }),
  228. {
  229. onUnmount: () => actionAdminGoodsSearchPageClear(),
  230. onLoad: ({ orderBy, text }) => actionAdminGoodsSearchPage({ orderBy, text }),
  231. onScroll: ({ feed, orderBy, text }) => actionFeedGoodsFind({ text, skip: feed?.length || 0, orderBy }),
  232. }
  233. )(AdminGoodsSearchPageContainer);
  234. const AdminOrdersPageContainer = ({ feed, orders, promiseStatus, onLoad, onUnmount, onScroll }) => {
  235. const dispatch = useDispatch();
  236. const [searchParams] = useSearchParams();
  237. const orderBy = searchParams.get("orderBy") || "_id";
  238. const status = searchParams.get("status") || 0;
  239. useEffect(() => {
  240. onLoad({ orderBy, status });
  241. return () => {
  242. onUnmount();
  243. };
  244. }, [orderBy, status]);
  245. useEffect(() => {
  246. window.onscroll = (e) => {
  247. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  248. if (promiseStatus !== "PENDING") {
  249. onScroll({ feed, orderBy, status });
  250. }
  251. }
  252. };
  253. return () => {
  254. window.onscroll = null;
  255. };
  256. }, [promiseStatus, feed, status]);
  257. useEffect(() => {
  258. if (orders?.length) dispatch(actionFeedAdd(orders));
  259. }, [orders]);
  260. return <AdminOrdersPage orderBy={orderBy} />;
  261. };
  262. const CAdminOrdersPageContainer = connect(
  263. (state) => ({
  264. orders: state.promise?.feedOrdersAll?.payload || [],
  265. feed: state.feed?.payload || [],
  266. promiseStatus: state.promise?.feedOrdersAll?.status || null,
  267. }),
  268. {
  269. onUnmount: () => actionAdminOrdersPageClear(),
  270. onLoad: ({ orderBy, status }) => actionAdminOrdersPage({ orderBy, status }),
  271. onScroll: ({ feed, orderBy, status }) => actionFeedOrders({ skip: feed?.length || 0, orderBy, status }),
  272. }
  273. )(AdminOrdersPageContainer);
  274. const AdminOrdersSearchPageContainer = ({ feed, orders, promiseStatus, onLoad, onUnmount, onScroll }) => {
  275. const dispatch = useDispatch();
  276. const [searchParams] = useSearchParams();
  277. const orderBy = searchParams.get("orderBy") || "_id";
  278. const text = searchParams.get("text") || "";
  279. const status = searchParams.get("status") || 0;
  280. useEffect(() => {
  281. onLoad({ orderBy, text });
  282. return () => {
  283. onUnmount();
  284. };
  285. }, [orderBy, text, status]);
  286. useEffect(() => {
  287. window.onscroll = (e) => {
  288. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  289. if (promiseStatus !== "PENDING") {
  290. onScroll({ feed, orderBy, text, status });
  291. }
  292. }
  293. };
  294. return () => {
  295. window.onscroll = null;
  296. };
  297. }, [promiseStatus, feed, text, status]);
  298. useEffect(() => {
  299. if (orders?.length) dispatch(actionFeedAdd(orders));
  300. }, [orders]);
  301. return <AdminOrdersPage orderBy={orderBy} />;
  302. };
  303. const CAdminOrdersSearchPageContainer = connect(
  304. (state) => ({
  305. orders: state.promise?.feedOrdersFind?.payload || [],
  306. feed: state.feed?.payload || [],
  307. promiseStatus: state.promise?.feedOrdersFind?.status || null,
  308. }),
  309. {
  310. onUnmount: () => actionAdminOrdersSearchPageClear(),
  311. onLoad: ({ orderBy, text, status }) => actionAdminOrdersSearchPageClear({ orderBy, text, status }),
  312. onScroll: ({ feed, orderBy, text, status }) => actionFeedOrdersFind({ text, skip: feed?.length || 0, orderBy, status }),
  313. }
  314. )(AdminOrdersSearchPageContainer);
  315. const AdminOrderPageContainer = ({ onLoad, onUnmount }) => {
  316. const params = useParams();
  317. useEffect(() => {
  318. return () => {
  319. onUnmount();
  320. };
  321. }, []);
  322. useEffect(() => {
  323. onLoad(params._id);
  324. }, [params._id]);
  325. return <CAdminOrderPage />;
  326. };
  327. const CAdminOrderPageContainer = connect(null, {
  328. onUnmount: () => actionAdminOrderPageClear(),
  329. onLoad: (_id) => actionAdminOrderPage({ _id }),
  330. })(AdminOrderPageContainer);
  331. const AdminUsersSearchPageContainer = ({ feed, users, promiseStatus, onLoad, onUnmount, onScroll }) => {
  332. const dispatch = useDispatch();
  333. const [searchParams] = useSearchParams();
  334. const orderBy = searchParams.get("orderBy") || "_id";
  335. const text = searchParams.get("text") || "";
  336. useEffect(() => {
  337. onLoad({ orderBy, text });
  338. return () => {
  339. onUnmount();
  340. };
  341. }, [orderBy, text]);
  342. useEffect(() => {
  343. window.onscroll = (e) => {
  344. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  345. if (promiseStatus !== "PENDING") {
  346. onScroll({ feed, orderBy, text });
  347. }
  348. }
  349. };
  350. return () => {
  351. window.onscroll = null;
  352. };
  353. }, [promiseStatus, feed, text]);
  354. useEffect(() => {
  355. if (users?.length) dispatch(actionFeedAdd(users));
  356. }, [users]);
  357. return <AdminUsersPage orderBy={orderBy} />;
  358. };
  359. const CAdminUsersSearchPageContainer = connect(
  360. (state) => ({
  361. users: state.promise?.feedUsersFind?.payload || [],
  362. feed: state.feed?.payload || [],
  363. promiseStatus: state.promise?.feedUsersFind?.status || null,
  364. }),
  365. {
  366. onUnmount: () => actionAdminUsersSearchPageClear(),
  367. onLoad: ({ orderBy, text }) => actionAdminUsersSearchPage({ orderBy, text }),
  368. onScroll: ({ feed, orderBy, text }) => actionFeedUsersFind({ text, skip: feed?.length || 0, orderBy }),
  369. }
  370. )(AdminUsersSearchPageContainer);
  371. const AdminUsersPageContainer = ({ feed, users, promiseStatus, onLoad, onUnmount, onScroll }) => {
  372. const dispatch = useDispatch();
  373. const [searchParams] = useSearchParams();
  374. const orderBy = searchParams.get("orderBy") || "_id";
  375. useEffect(() => {
  376. onLoad({ orderBy });
  377. return () => {
  378. onUnmount();
  379. };
  380. }, [orderBy]);
  381. useEffect(() => {
  382. window.onscroll = (e) => {
  383. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  384. if (promiseStatus !== "PENDING") {
  385. onScroll({ feed, orderBy });
  386. }
  387. }
  388. };
  389. return () => {
  390. window.onscroll = null;
  391. };
  392. }, [promiseStatus, feed]);
  393. useEffect(() => {
  394. if (users?.length) dispatch(actionFeedAdd(users));
  395. }, [users]);
  396. return <AdminUsersPage orderBy={orderBy} />;
  397. };
  398. const CAdminUsersPageContainer = connect(
  399. (state) => ({
  400. users: state.promise?.feedUsersAll?.payload || [],
  401. feed: state.feed?.payload || [],
  402. promiseStatus: state.promise?.feedUsersAll?.status || null,
  403. }),
  404. {
  405. onUnmount: () => actionAdminUsersPageClear(),
  406. onLoad: ({ orderBy }) => actionAdminUsersPage({ orderBy }),
  407. onScroll: ({ feed, orderBy }) => actionFeedUsers({ skip: feed?.length || 0, orderBy }),
  408. }
  409. )(AdminUsersPageContainer);
  410. const AdminUserPageContainer = ({ onLoad, onUnmount }) => {
  411. const params = useParams();
  412. useEffect(() => {
  413. return () => {
  414. onUnmount();
  415. };
  416. }, []);
  417. useEffect(() => {
  418. onLoad(params._id);
  419. }, [params._id]);
  420. return <CAdminUserPage />;
  421. };
  422. const CAdminUserPageContainer = connect(null, {
  423. onUnmount: () => actionAdminUserPageClear(),
  424. onLoad: (_id) => actionAdminUserPage({ _id }),
  425. })(AdminUserPageContainer);
  426. const AdminLayoutPage = () => {
  427. return (
  428. <Box className="AdminLayoutPage">
  429. <Routes>
  430. <Route path="/" element={<Navigate to={"/admin/goods/"} />} />
  431. <Route path="/tree/" element={<CAdminCategoryTreePageContainer />} />
  432. <Route path="/goods/" element={<CAdminGoodsPageContainer />} />
  433. <Route path="/goods/search" element={<CAdminGoodsSearchPageContainer />} />
  434. <Route path="/good/" element={<CAdminGoodPageContainer />} />
  435. <Route path="/good/:_id" element={<CAdminGoodPageContainer />} />
  436. <Route path="/categories/" element={<CAdminCategoriesPageContainer />} />
  437. <Route path="/categories/search" element={<CAdminCategoriesSearchPageContainer />} />
  438. <Route path="/category/" element={<CAdminCategoryPageContainer />} />
  439. <Route path="/category/:_id" element={<CAdminCategoryPageContainer />} />
  440. <Route path="/orders/" element={<CAdminOrdersPageContainer />} />
  441. <Route path="/orders/search" element={<CAdminOrdersSearchPageContainer />} />
  442. <Route path="/order/" element={<CAdminOrderPageContainer />} />
  443. <Route path="/order/:_id" element={<CAdminOrderPageContainer />} />
  444. <Route path="/users/search" element={<CAdminUsersSearchPageContainer />} />
  445. <Route path="/users/" element={<CAdminUsersPageContainer />} />
  446. <Route path="/user/" element={<CAdminUserPageContainer />} />
  447. <Route path="/user/:_id" element={<CAdminUserPageContainer />} />
  448. <Route path="*" element={<Navigate to="/404" />} />
  449. </Routes>
  450. </Box>
  451. );
  452. };
  453. export { AdminLayoutPage };