You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdminPaymentPage.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import React, { useEffect, useMemo } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. AdminPaymentPageContainer,
  5. AdminPaymentsHeader,
  6. AdminPaymentsSearchField,
  7. NewPaymentButton,
  8. PaymentsList,
  9. } from "./AdminPaymentPage.styled";
  10. import { useTranslation } from "react-i18next";
  11. import useSorting from "../../../hooks/useOffers/useSorting";
  12. import { sortPaymentsEnum } from "../../../enums/sortEnum";
  13. import CategoryCard from "../../../components/Cards/CategoryCard/CategoryCard";
  14. import selectedTheme from "../../../themes";
  15. import { useDispatch, useSelector } from "react-redux";
  16. import { setManualSearchString } from "../../../store/actions/filters/filtersActions";
  17. import { toggleCreatePaymentModal } from "../../../store/actions/modal/modalActions";
  18. import { selectPayments } from "../../../store/selectors/paymentSelector";
  19. import { selectManualSearchString } from "../../../store/selectors/filtersSelectors";
  20. import { fetchPayments } from "../../../store/actions/payment/paymentActions";
  21. import { adminSortMethod } from "../../../util/helpers/adminSortHelper";
  22. import { selectAllProfiles } from "../../../store/selectors/profileSelectors";
  23. import { fetchAllProfiles } from "../../../store/actions/profile/profileActions";
  24. import useQueryString from "../../../hooks/useOffers/useQueryString";
  25. const AdminPaymentPage = () => {
  26. const { t } = useTranslation();
  27. const dispatch = useDispatch();
  28. const sorting = useSorting(() => {}, sortPaymentsEnum);
  29. const payments = useSelector(selectPayments);
  30. const users = useSelector(selectAllProfiles);
  31. const manualSearchString = useSelector(selectManualSearchString);
  32. const queryObject = useQueryString();
  33. useEffect(() => {
  34. dispatch(fetchPayments());
  35. dispatch(fetchAllProfiles());
  36. return () => {
  37. dispatch(setManualSearchString(""));
  38. sorting.clear();
  39. };
  40. }, []);
  41. const checkedUsers = useMemo(() => {
  42. if (queryObject.queryObject.companyName) {
  43. return users?.filter?.((user) =>
  44. queryObject?.queryObject?.companyName?.includes(user.companyName)
  45. );
  46. } else {
  47. return "";
  48. }
  49. }, [users, queryObject?.queryObject]);
  50. const checkedUsersIds =
  51. checkedUsers && checkedUsers?.map((item) => item?._id);
  52. const paymentsToShow = useMemo(() => {
  53. if (payments && !queryObject?.queryObject?.companyName) {
  54. return adminSortMethod(payments, manualSearchString, sorting);
  55. }
  56. if (queryObject?.queryObject?.companyName.length > 0) {
  57. const filteredPayments = payments?.filter((payment) =>
  58. checkedUsersIds.includes(payment?.user?._id)
  59. );
  60. return adminSortMethod(filteredPayments, manualSearchString, sorting);
  61. }
  62. }, [
  63. payments,
  64. manualSearchString,
  65. sorting.selectedSortOptionLocally,
  66. queryObject.queryObject.companyName,
  67. ]);
  68. const handleSearch = (value) => {
  69. dispatch(setManualSearchString(value));
  70. };
  71. const showAddPaymentModal = () => {
  72. dispatch(toggleCreatePaymentModal());
  73. };
  74. return (
  75. <AdminPaymentPageContainer>
  76. <AdminPaymentsSearchField
  77. placeholder={t("admin.payment.placeholder")}
  78. isAdmin
  79. handleSearch={handleSearch}
  80. />
  81. <NewPaymentButton
  82. variant="contained"
  83. buttoncolor={selectedTheme.colors.iconYellowColor}
  84. textcolor={selectedTheme.colors.messageText}
  85. onClick={showAddPaymentModal}
  86. >
  87. {t("admin.payment.addPayment")}
  88. </NewPaymentButton>
  89. <AdminPaymentsHeader
  90. myOffers
  91. payments
  92. hideGrid
  93. isAdmin
  94. sorting={sorting}
  95. hideBackButton
  96. />
  97. <PaymentsList>
  98. {paymentsToShow?.map((payment) => (
  99. <CategoryCard
  100. key={payment._id}
  101. category={payment}
  102. type="payment"
  103. secondLabel={t("admin.payment.date")}
  104. dontNavigate
  105. hideCheckButton
  106. payments
  107. />
  108. ))}
  109. </PaymentsList>
  110. </AdminPaymentPageContainer>
  111. );
  112. };
  113. AdminPaymentPage.propTypes = {
  114. children: PropTypes.node,
  115. };
  116. export default AdminPaymentPage;