AdminCategoriesPageContainer.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { connect } from "react-redux";
  2. import { useEffect } from "react";
  3. import { useDispatch } from "react-redux";
  4. import { useSearchParams } from "react-router-dom";
  5. import { actionCategoriesPage } from "../../../../actions/actionCategoriesPage";
  6. import { actionFeedAdd, actionFeedCats } from "../../../../reducers";
  7. import { AdminCategoriesPage } from "../../AdminCategoriesPage";
  8. const AdminCategoriesPageContainer = ({ feed, cats, promiseStatus, onLoad, onUnmount, onScroll }) => {
  9. const dispatch = useDispatch();
  10. const [searchParams] = useSearchParams();
  11. const orderBy = searchParams.get("orderBy") || "_id";
  12. useEffect(() => {
  13. onLoad({ orderBy });
  14. return () => {
  15. onUnmount();
  16. };
  17. }, [orderBy]);
  18. useEffect(() => {
  19. window.onscroll = (e) => {
  20. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 200) {
  21. if (promiseStatus !== "PENDING") {
  22. onScroll({ feed, orderBy });
  23. }
  24. }
  25. };
  26. return () => {
  27. window.onscroll = null;
  28. };
  29. }, [feed, promiseStatus]);
  30. useEffect(() => {
  31. if (cats.length) dispatch(actionFeedAdd(cats));
  32. }, [cats]);
  33. return <AdminCategoriesPage orderBy={orderBy} />;
  34. };
  35. export const CAdminCategoriesPageContainer = connect(
  36. (state) => ({
  37. cats: state.promise?.feedCatAll?.payload || [],
  38. feed: state.feed?.payload || [],
  39. promiseStatus: state.promise?.feedCatAll?.status || null,
  40. }),
  41. {
  42. onUnmount: () => ({ type: "CATEGORIES_PAGE_CLEAR" }),
  43. onLoad: ({ orderBy }) => actionCategoriesPage({ orderBy }),
  44. onScroll: ({ feed, orderBy }) => actionFeedCats({ skip: feed?.length || 0, orderBy }),
  45. }
  46. )(AdminCategoriesPageContainer);