AdminCategoriesPageContainer.js 1.9 KB

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