All files / src/features/Merchants/MerchantSidePanel/Modals BusinessProfileUpdateModal.tsx

0% Statements 0/23
0% Branches 0/11
0% Functions 0/6
0% Lines 0/22

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                                                                                                                                                                                                                                                                                                   
import NiceModal from "@ebay/nice-modal-react";
import CommentModalBase, { FormValues } from "./CommentModalBase";
import { IParsedData } from "@components/Merchants/MerchantPreview/data.types";
import { useUpdateBusinessProfileStatus } from "@components/Merchants/MerchantPreview/hooks/useUpdateBusinessProfileStatus";
import { SubmitHandler } from "react-hook-form";
import { useOpenGiveConversation } from "@features/GiveConversation/hooks/useOpenGiveConversation";
import { useSendConversationMessage } from "@features/GiveConversation/hooks/useSendConversationMessage";
import { fetchThread } from "@features/GiveConversation/hooks/useManageApi";
import { capitalizeFirstLetter } from "@utils/index";
import { GENERAL_CONVERSATION_ERROR } from "@features/GiveConversation/constants";
import { showMessage } from "@common/Toast";
 
type BusinessProfileUpdateProps = {
  merchantId: number;
  data: IParsedData;
  status: "suspended" | "declined";
  isProvider?: boolean;
};
 
const statusMaps = (isProvider?: boolean) => ({
  suspended: {
    title: "Suspend Business Profile",
    button: "Confirm",
    confirmButtonColor: "primary",
    description: `Are you sure you want to suspend this business profile? Once suspended, the ${
      isProvider ? "provider" : "merchant"
    } will not be able to process and transfer money.`,
  },
  move_back_to_pending: {
    title: "Move Business Profile back to pending",
    button: "Confirm",
    confirmButtonColor: "primary",
  },
  declined: {
    title: "Decline Business Profile",
    button: "Decline",
    confirmButtonColor: "destructive",
    description:
      "Are you sure you want to decline this business profile? Once declined, you will not be able to edit any information in the profile.",
  },
});
 
const threadName = "Business Profile";
 
const createStatusChangeMessage = (
  merchantName: string,
  status: string,
  reason: string,
  isProvider?: boolean,
) => {
  return `The ${
    isProvider ? "provider" : "merchant"
  } "${merchantName}" business status has been changed to ${capitalizeFirstLetter(
    status,
  )} status due to: ${reason}`;
};
 
const BusinessProfileUpdateModal = NiceModal.create(
  ({ merchantId, status, data, isProvider }: BusinessProfileUpdateProps) => {
    const { handleOpenConversation } = useOpenGiveConversation();
 
    const merchantData = {
      id: merchantId,
    };
 
    const { handleSendMessage } = useSendConversationMessage({
      useOptimisticUpdate: false,
    });
 
    const onSuccess = async (response: any) => {
      const messageText = createStatusChangeMessage(
        data.merchantInfo.merchantName,
        status,
        response.reason,
      );
 
      const threadPayload = {
        title: threadName,
        subjectAccID: merchantId,
        message: {
          body: messageText,
        },
        isInternal: true,
      };
      try {
        const threadData = await fetchThread(merchantId, threadName);
        // If thread id exists, we can use it to send message, otherwise we need to create a new thread
        const threadId = threadData?.data[0]?.id;
 
        handleSendMessage(
          {
            body: messageText,
          },
          {
            merchantId,
            threadId,
            threadData: threadId ? undefined : threadPayload,
            onSuccess: (responseData: any) => {
              handleOpenConversation({
                merchantData,
                // If threadId exists, we are opening existing thread so we can use it.
                // Otherwise, we need to pass a new thread returned from response.
                initialThreadID: threadId || responseData?.id,
                threadName,
              });
            },
          },
        );
      } catch (error: any) {
        showMessage(
          "Error",
          error?.response?.data?.message ||
            error?.message ||
            GENERAL_CONVERSATION_ERROR,
        );
      }
    };
 
    const { handleUpdateStatus } = useUpdateBusinessProfileStatus({
      merchantId,
      data,
      onSuccess,
    });
 
    const onSubmit: SubmitHandler<FormValues> = (data) => {
      handleUpdateStatus(status, data.comment);
    };
 
    const statusMap = statusMaps(isProvider)[status];
 
    return (
      <CommentModalBase
        title={statusMap?.title}
        description={statusMap?.description}
        onConfirm={onSubmit}
        confirmButtonLabel={statusMap?.button}
        confirmButtonColor={
          statusMap?.confirmButtonColor as "destructive" | "primary"
        }
      />
    );
  },
);
 
export default BusinessProfileUpdateModal;