AdminCategoriesSearchPageContainer.js 2.0 KB

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