All files / src/components/Merchants/modals/NotifyMerchant NotifyMerchantV2.tsx

0% Statements 0/23
0% Branches 0/17
0% Functions 0/5
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 147 148 149 150 151                                                                                                                                                                                                                                                                                                             
import ModalFactory from "@common/Modal/ModalFactory/ModalFactory";
import { ModalActions, ModalTitle } from "@common/Modal/ModalFactory/atoms";
import NiceModal from "@ebay/nice-modal-react";
import GenericNotificationForm from "./GenericNotificationForm";
import { useState } from "react";
import {
  TCustomSubmitHandler,
  TNotificationType,
  TNotifyMerchantContent,
  TSubmitStatus,
  TFormSubmitHandler,
} from "./types";
import { showMessage } from "@common/Toast";
import {
  GenericNotificationPayload,
  notifyMerchant,
} from "@services/api/notifications/merchant";
import { useMutation } from "react-query";
import {
  getGlobalTopic,
  useConversationsModal,
} from "features/Minibuilders/Conversations/hooks/useConversationsModal";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
 
type NotifyMerchantProps = {
  notificationType?: TNotificationType;
  merchantName: string;
  merchantId: number;
  isEnterprise?: boolean;
  customSubmit?: TCustomSubmitHandler;
};
 
const Content: Record<TNotificationType, TNotifyMerchantContent> = {
  generic: GenericNotificationForm,
};
 
const NotifyMerchantBase = ({
  notificationType = "generic",
  merchantName,
  merchantId,
  isEnterprise,
  customSubmit,
}: NotifyMerchantProps) => {
  const [submitStatus, setSubmitStatus] = useState<TSubmitStatus>({
    isDisabled: true,
    message: "A message is required",
  });
 
  const { open, onClose, TransitionProps } = useNiceModal();
  const { openConversationHandler } = useConversationsModal();
 
  const Form = Content[notificationType];
 
  const notifyMerchantMutation = useMutation(
    (data: GenericNotificationPayload) => {
      return notifyMerchant(merchantId, data);
    },
    {
      onSuccess: () => {
        showMessage(
          "Success",
          "",
          true,
          `Notification was successfully sent to the “${merchantName}”`,
        );
      },
      onError: () => {
        showMessage(
          "Warning",
          "",
          true,
          `An error occured while sending the notification`,
        );
      },
    },
  );
 
  const onSubmit: TFormSubmitHandler = async (data) => {
    const { data: topic } = await getGlobalTopic({ topicName: "underwriting" });
    const underwritingTopicID = topic?.[0]?.ID || 0;
    if (!underwritingTopicID) return;
 
    if (customSubmit) {
      customSubmit(data);
    } else {
      notifyMerchantMutation.mutate({
        subject: data?.subject || "",
        message: data?.message,
      });
    }
 
    if (data.openConversation) {
      openConversationHandler({
        id: underwritingTopicID,
        name: "Underwriting",
        forceTopicOpen: true,
        paths: [],
        defaultMessage: `#BusinessProfile merchant was notified due to :\n${data.message}`,
      });
    }
    onClose();
  };
 
  return (
    <ModalFactory
      variant="dialog"
      modalProps={{
        open,
        onClose,
        DialogProps: {
          TransitionProps,
        },
      }}
    >
      <ModalTitle
        padding="24px 24px 20px"
        title={isEnterprise ? "Notify Provider" : "Notify Merchant"}
        onClose={onClose}
        textStyles={{
          title: {
            fontSize: "18px",
            lineHeight: "21.6px",
            letterSpacing: "-0.18px",
          },
        }}
      />
      <Form onSubmit={onSubmit} setSubmitStatus={setSubmitStatus} />
      <ModalActions
        padding="24px 24px 20px"
        animationDelay={100}
        secondaryAction={{
          onClick: onClose,
        }}
        primaryAction={{
          label: "Send",
          form: "notify-merchant-form",
          disabled: submitStatus.isDisabled || notifyMerchantMutation.isLoading,
          tooltipProps: {
            show: submitStatus.isDisabled && !!submitStatus.message,
            message: submitStatus.message,
          },
        }}
      />
    </ModalFactory>
  );
};
 
const NotifyMerchantV2 = NiceModal.create(NotifyMerchantBase);
 
export default NotifyMerchantV2;