All files / src/features/Merchants/MerchantSidePanel/WithRepository/Challenges/Modals/components ChallengeMessage.tsx

0% Statements 0/31
0% Branches 0/21
0% Functions 0/10
0% Lines 0/26

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                                                                                                                                                                                                                                                                                                                   
import {
  Accordion,
  AccordionDetails,
  AccordionSummary,
  Skeleton,
} from "@mui/material";
import { useState } from "react";
import { useQuery } from "react-query";
import { getGlobalTopic } from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import { customInstance } from "@services/api";
import { showMessage } from "@common/Toast";
import { useAppTheme, styled } from "@theme/v2/Provider";
import GiveText from "@shared/Text/GiveText";
import { MinusIcon, PlusIcon } from "@phosphor-icons/react";
import { TChallengeTypeName } from "./types";
 
const ICON_SIZE = 20;
 
type Props = {
  challengeTypeName: TChallengeTypeName;
  message: React.ReactNode;
  threadId: number;
  merchantID: number;
};
 
const getUnderwritingTopicID = (
  challengeTypeName: TChallengeTypeName,
  data: any,
) => {
  const activityTopic = data.find((item: any) => item.Type === "activity");
  const internalTopic = data.find((item: any) => item.Type === "internal");
  switch (challengeTypeName) {
    case "customer_due_diligence":
      return internalTopic.ID;
    case "enhanced_due_diligence":
      return activityTopic.ID;
    default:
      data[0].ID;
  }
};
 
const ChallengeMessage = ({
  message,
  merchantID,
  challengeTypeName,
  threadId,
}: Props) => {
  const [expanded, setExpanded] = useState(false);
  const theme = useAppTheme();
 
  const { data: topicData, isLoading } = useQuery(
    [`get-message-body-${threadId}`, threadId, merchantID, challengeTypeName],
    async () => {
      const { data } = await getGlobalTopic({ topicName: "underwriting" });
      const underwritingTopicID = getUnderwritingTopicID(
        challengeTypeName,
        data,
      );
      if (!underwritingTopicID) {
        return { message: "" };
      }
 
      const { data: _threads } = await customInstance({
        url: `/merchants/${merchantID}/topics/${underwritingTopicID}/threads?filter=id:${threadId}`,
        method: "GET",
      });
 
      if (_threads) {
        return { message: _threads[0]?.messages[0]?.body || "" };
      }
      return { message: "" };
    },
    {
      enabled:
        expanded &&
        !!threadId &&
        !!merchantID &&
        challengeTypeName !== "customer_due_diligence",
      onError(err: any) {
        showMessage(
          // TODO: use rebranding snackbar
          "Error",
          err?.response?.data?.message || "Unable to fetch message body",
        );
      },
    },
  );
 
  const ExpandIcon = expanded ? (
    <MinusIcon size={ICON_SIZE} fill={theme.palette.text.primary} />
  ) : (
    <PlusIcon size={ICON_SIZE} fill={theme.palette.text.primary} />
  );
 
  return (
    <StyledAccordion
      onChange={() => setExpanded((curr) => !curr)}
      disableGutters
    >
      <StyledAccordionSummary expandIcon={ExpandIcon}>
        <GiveText color="primary" variant="bodyS">
          How to complete this challenge?
        </GiveText>
      </StyledAccordionSummary>
      <AccordionDetails>
        {isLoading ? (
          <Skeleton
            sx={{
              height: "70px",
            }}
          />
        ) : (
          <GiveText
            sx={{
              wordWrap: "break-word",
              whiteSpace: "normal",
            }}
            color="secondary"
            variant="bodyS"
          >
            {topicData?.message || message}
          </GiveText>
        )}
      </AccordionDetails>
    </StyledAccordion>
  );
};
const StyledAccordion = styled(Accordion)(({ theme }) => ({
  boxShadow: "none",
  borderRadius: `8px !important`,
  padding: "0px !important",
  backgroundColor: theme.palette.primitive?.transparent["darken-5"],
  ".MuiButtonBase-root": {
    padding: "12px 16px !important",
  },
  ".MuiAccordionDetails-root": {
    padding: "0px 16px 12px 16px !important",
    backggroundColor: "red",
  },
  "& .MuiAccordionSummary-content": {
    padding: "0px",
    margin: "0px",
  },
}));
 
const StyledAccordionSummary = styled(AccordionSummary)(({ theme }) => ({
  borderRadius: `8px !important`,
  "&:hover": {
    backgroundColor: theme.palette.neutral[10],
  },
}));
 
export default ChallengeMessage;