All files / src/sections/PayBuilder/Modals DonorsModal.tsx

100% Statements 18/18
96.29% Branches 26/27
100% Functions 5/5
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143                                                      1x   29x 29x     29x     29x               29x   29x   29x 14x     29x 25x     29x                                                 1x                     12x                                                         1x                     1x                    
import NiceModal from "@ebay/nice-modal-react";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import GiveTabs from "@shared/Tabs/GiveTabs";
import { useEffect, useState } from "react";
import { useInView } from "react-intersection-observer";
import { Stack } from "@mui/material";
import {
  DonorComponent,
  DonorsSkeleton,
  ParticipantComponent,
  WinnerComponent,
} from "../views/fundraiser/components/Atom.component";
import {
  useGetDonorsList,
  useGetSweepstakeWinner,
} from "../provider/useManageApi";
import { donorModalType } from "../types";
 
interface DonorsModalProps {
  type: donorModalType;
  accentColor: string;
  id: string | number;
  isSweepstake?: boolean;
  hasEnded?: boolean;
}
 
const DonorsModal = NiceModal.create(
  ({ type, accentColor, id, isSweepstake, hasEnded }: DonorsModalProps) => {
    const { open, SlideProps } = useNiceModal();
    const [typeState, setTypeState] = useState<donorModalType>(type);
 
    const { winner, isWinnerSelected, isLoadingWinner } =
      useGetSweepstakeWinner(id, hasEnded && isSweepstake);
 
    const { donors, isLoading, hasNextPage, fetchNextPage, total } =
      useGetDonorsList({
        id,
        max: 20,
        typeState,
        isWinnerSelected: isSweepstake && isWinnerSelected, //only sweepstakes can have winners
        isEnabled: !isLoadingWinner,
      });
 
    const totalWithWinner = hasEnded && isWinnerSelected ? total + 1 : total;
 
    const [ref, inView] = useInView();
 
    useEffect(() => {
      if (inView) fetchNextPage();
    }, [inView]);
 
    const getDonorsList = () => {
      Eif (id) return donors;
    };
 
    return (
      <GiveBaseModal
        open={open}
        title={`${totalWithWinner} ${
          isSweepstake ? "Participants" : "Donations"
        }`}
        width="560px"
        onClose={SlideProps.onExit}
        showFooter={false}
        sx={{
          "& .MuiDialogContent-root": {
            paddingBottom: "20px !important",
          },
        }}
      >
        <GiveTabs
          selected={typeState}
          containerSx={{
            width: "100%",
            justifyContent: "space-around",
            padding: "4px",
          }}
          tabSx={{ flex: 1, justifyContent: "center" }}
          items={isSweepstake ? participantTabs : donorsTabs}
          onClick={(e) => {
            setTypeState(e as donorModalType);
          }}
          type="segmented"
        />
        <Stack mt={3}>
          {isLoading || isLoadingWinner ? (
            <DonorsSkeleton count={7} />
          ) : (
            <>
              {hasEnded && isSweepstake && <WinnerComponent {...winner} />}
              {getDonorsList()?.map((donor) =>
                isSweepstake ? (
                  <ParticipantComponent
                    key={donor?.id}
                    accentColor={accentColor}
                    {...donor}
                  />
                ) : (
                  <DonorComponent
                    key={donor?.id}
                    accentColor={accentColor}
                    {...donor}
                  />
                ),
              )}
              {hasNextPage && (
                <div ref={ref}>
                  <DonorsSkeleton count={1} />
                </div>
              )}
            </>
          )}
        </Stack>
      </GiveBaseModal>
    );
  },
);
 
export default DonorsModal;
 
const donorsTabs = [
  {
    label: "Most Recent",
    value: "most_recent",
  },
  {
    label: "Top Donors",
    value: "top_donors",
  },
];
 
const participantTabs = [
  {
    label: "Most Recent",
    value: "most_recent",
  },
  {
    label: "Top Participants",
    value: "top_donors",
  },
];