All files / src/features/Merchants/MerchantSidePanel/WithRepository/Challenges ChallengesCategories.tsx

92.59% Statements 25/27
78.12% Branches 25/32
80% Functions 4/5
92% Lines 23/25

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                        21x             6x 6x     6x 6x 6x 6x 6x 6x   6x     6x 3x     6x   2x                           2x   2x   2x   2x 2x   2x     1x       1x               1x                                                                                                                                                                        
import { Box, Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { useConversationsModal } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import ImagesStacker from "features/Minibuilders/Conversations/utils/ImagesStacker";
import { useMerchantSidePanelContext } from "../../Provider/MerchantSidePanelProvider";
import ChallengeLayout from "./ChallengeLayout";
import { TChallengeCategory } from "./types";
import { useGetChallenges } from "./useChallengesRepository";
import { iconMapper } from "./utils";
import { ChatsCircleIcon } from "@phosphor-icons/react";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { useAppDispatch } from "@redux/hooks";
import { setSearchQueryByKey } from "@redux/slices/search";
import { useEffect } from "react";
import { checkPortals } from "@utils/routing";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { ACTION_DENY_MESSAGE } from "@constants/permissions";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
 
const ChallengesCategories = () => {
  const {
    setFirstPanelContent,
    setOpenChallenge,
    isLoadingUnderwriting,
    setIsLoadingUnderwriting,
    isOnboardingView,
  } = useMerchantSidePanelContext();
  const filter = isOnboardingView
    ? `filter=%3B(name:"merchant_onboarding")&`
    : "";
  const { data, isLoading } = useGetChallenges(filter);
  const { palette } = useAppTheme();
  const { openConversationHandler } = useConversationsModal();
  const dispatch = useAppDispatch();
  const { isEnterprisePortal } = checkPortals();
  const { merchant_underwriting } = useEnterprisePermissions();
 
  const isDisabledConversation = isEnterprisePortal && !merchant_underwriting;
 
  //if at this point we are still loading underwriting, set it to false
  useEffect(() => {
    if (!isLoading && isLoadingUnderwriting) setIsLoadingUnderwriting(false);
  }, [isLoading, isLoadingUnderwriting]);
 
  if (isLoading) return <></>;
 
  return (
    <Stack gap={1.5}>
      <GiveText
        fontSize="14px"
        sx={{
          color: palette.primitive?.neutral[60],
        }}
      >
        Challenge Categories
      </GiveText>
      <Stack gap={1}>
        {data.challengesCategoriesData?.data.map(
          (challenge: TChallengeCategory, id) => {
            const isOnboardingComplete =
              challenge.name === "merchant_onboarding" && !isOnboardingView;
            const isOnboardingChallenges =
              challenge.name === "merchant_onboarding" && isOnboardingView;
            const total =
              (challenge.totalChallenges || 0) - (challenge.totalEmpty || 0);
            const isIncomplete =
              challenge.totalClosed !== total && !isOnboardingComplete;
            const status = isIncomplete ? "In Progress" : "Approved";
 
            return (
              <ChallengeLayout
                onClick={() => {
                  setOpenChallenge({
                    identifier: challenge.name,
                    displayName: challenge.displayName,
                  });
                  dispatch(
                    setSearchQueryByKey({
                      queryKey: "items_all_challenges",
                      params: {
                        value: "",
                      },
                    }),
                  );
                  setFirstPanelContent("challenges");
                }}
                icon={<>{iconMapper(challenge.name)}</>}
                attachConversations={
                  <Stack direction="row" alignItems="center">
                    <ImagesStacker data={[]} />
                    <GiveTooltip
                      width="compact"
                      alignment="left"
                      title={ACTION_DENY_MESSAGE}
                      color="default"
                      disableHoverListener={
                        !isOnboardingChallenges || !isDisabledConversation
                      }
                    >
                      <GiveIconButton
                        Icon={ChatsCircleIcon}
                        variant="ghost"
                        disabled={
                          isOnboardingChallenges && isDisabledConversation
                        }
                        onClick={(e) => {
                          e.stopPropagation();
                          openConversationHandler({
                            id: 0,
                            name: challenge.displayName,
                            paths: [],
                          });
                        }}
                      />
                    </GiveTooltip>
                  </Stack>
                }
                main={
                  <Stack>
                    <GiveText
                      fontSize="14px"
                      sx={{
                        color: palette.primitive?.neutral[90],
                      }}
                    >
                      {challenge.displayName}
                    </GiveText>
                    <GiveText
                      fontSize="14px"
                      sx={{
                        color: isIncomplete
                          ? palette.primitive?.neutral[60]
                          : palette.primitive?.success[50],
                      }}
                    >
                      {challenge.totalClosed + "/" + total} Completed
                    </GiveText>
                  </Stack>
                }
                end={
                  <Box
                    sx={{
                      backgroundColor: !isIncomplete
                        ? palette.primitive?.success[25]
                        : palette.primitive?.neutral[10],
                      padding: "4px 8px",
                      borderRadius: "8px",
                    }}
                  >
                    <GiveText
                      fontSize="12px"
                      color={isIncomplete ? "primary" : "success1"}
                    >
                      {status}
                    </GiveText>
                  </Box>
                }
                key={id}
              />
            );
          },
        )}
      </Stack>
    </Stack>
  );
};
 
export default ChallengesCategories;