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

0% Statements 0/33
0% Branches 0/23
0% Functions 0/8
0% Lines 0/33

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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { Box, FormControlLabel, RadioGroup, Stack } from "@mui/material";
import GiveButton from "@shared/Button/GiveButton";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveText from "@shared/Text/GiveText";
import { Controller } from "react-hook-form";
import { SubmitHandler } from "react-hook-form";
import { useUpdateUnderwriting } from "@components/Merchants/MerchantPreview/hooks/useUnderwritingUpdate";
import { useQueryClient } from "react-query";
import { useForm } from "react-hook-form";
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_MERCHANT_STATS,
} from "@constants/queryKeys";
import NiceModal from "@ebay/nice-modal-react";
import { styled, useAppTheme } from "@theme/v2/Provider";
import GiveRadio from "@shared/Radio/GiveRadio";
import { CustomTheme } from "@theme/v2/palette.interface";
import GiveMotionWrapper from "@shared/Motion/GiveMotionWrapper";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { showMessage } from "@common/Toast";
import { useSponsorUpdate } from "@components/Merchants/MerchantPreview/hooks/useSponsorUpdate";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useRefetchCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
 
const approvalTypes = [
  {
    value: "auto-approval",
    title: "Auto-approval",
    subtitle:
      "Recommended for merchants categorized as 'Normal' risk. This allows a quicker and more efficient approval process for low-risk merchants.",
  },
  {
    value: "pre-approval",
    title: "Pre-approval",
    subtitle:
      "Recommended for merchants categorized as 'Restricted' or 'High' risk. This allows additional checks and balances before the final approval is granted.",
  },
];
 
const approvalTypesRefactored = [
  {
    value: "auto_approval",
    title: "Auto-approval",
    subtitle:
      "Recommended for merchants categorized as 'Normal' risk. This allows a quicker and more efficient approval process for low-risk merchants.",
  },
  {
    value: "pre_approval",
    title: "Pre-approval",
    subtitle:
      "Recommended for merchants categorized as 'Restricted' or 'High' risk. This allows additional checks and balances before the final approval is granted.",
  },
];
 
type Props = {
  merchantId: number;
  defaultStatus?: string;
  isSponsorUpdate?: boolean;
  title?: string;
  description?: string;
  primaryActionLabel?: string;
};
 
type ApprovalTypeInputs = {
  status: string;
};
 
const schema = Yup.object().shape({
  status: Yup.string(),
});
 
const ApproveMerchantModal = NiceModal.create(
  ({
    merchantId,
    defaultStatus = "pre-approval",
    isSponsorUpdate,
    description,
    title,
    primaryActionLabel,
  }: Props) => {
    const { open, onClose } = useNiceModal();
    const { isMerchantStatusRefactorEnabled, isNewApprovalFlowEnabled } =
      useGetFeatureFlagValues();
    const { handleRefetchCounters } = useRefetchCounters();
    const theme = useAppTheme();
    const queryClient = useQueryClient();
 
    const {
      updateUnderwriting: { isLoading, mutate },
    } = useUpdateUnderwriting(merchantId);
 
    const {
      updateSponsorStatus: {
        isLoading: isLoadingSponsor,
        mutate: mutateSponsor,
      },
    } = useSponsorUpdate(merchantId);
 
    const usedMutation =
      isMerchantStatusRefactorEnabled && isSponsorUpdate
        ? mutateSponsor
        : mutate;
 
    const usedLoading =
      isMerchantStatusRefactorEnabled && isSponsorUpdate
        ? isLoadingSponsor
        : isLoading;
 
    const methods = useForm<ApprovalTypeInputs>({
      mode: "onSubmit",
      reValidateMode: "onChange",
      defaultValues: {
        status: isMerchantStatusRefactorEnabled
          ? defaultStatus.replace("-", "_")
          : defaultStatus,
      },
      resolver: yupResolver(schema),
    });
 
    const onConfirm: SubmitHandler<ApprovalTypeInputs> = async (data) => {
      const payload =
        isMerchantStatusRefactorEnabled && isSponsorUpdate
          ? { status: "approved" }
          : data;
      usedMutation(payload, {
        onSuccess: () => {
          queryClient.invalidateQueries(QKEY_LIST_MERCHANT_STATS);
          handleRefetchCounters();
          queryClient.invalidateQueries(QKEY_LIST_ACQUIRER_MERCHANTS);
 
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
            merchantId,
          ]);
          onClose();
        },
        onError: () => {
          showMessage(
            "Error",
            "Merchant account approval failed. Ensure all required information is accurate and approved",
          );
        },
      });
    };
 
    const handleConfirm = () => {
      methods.handleSubmit(onConfirm);
      onClose();
    };
 
    const isValid = methods.formState.isValid;
    const types = isMerchantStatusRefactorEnabled
      ? approvalTypesRefactored
      : approvalTypes;
 
    return (
      <GiveBaseModal
        open={open}
        title="Move to Sponsor"
        width="543px"
        height="57%"
        onClose={onClose}
        buttons={
          <Stack gap="12px" flexDirection="row">
            <GiveButton
              onClick={onClose}
              label="Cancel"
              variant="ghost"
              size="large"
            />
            <GiveButton
              label="Confirm"
              variant="filled"
              size="large"
              disabled={!isValid || usedLoading}
              onClick={methods.handleSubmit(onConfirm)}
            />
          </Stack>
        }
        closeIconProps={{
          bgColor: "solidWhite",
        }}
      >
        <Stack sx={{ overflowY: "hidden" }} gap="12px">
          <Controller
            control={methods.control}
            name="status"
            render={({ field: { onChange, value }, fieldState: { error } }) => (
              <Box>
                <RadioGroup sx={{ gap: 2 }}>
                  {types.map((option, idx) => (
                    <GiveMotionWrapper
                      key={option.title}
                      delay={(idx + 1) * 50}
                    >
                      <StyledBox
                        selected={option.value === value}
                        theme={theme}
                      >
                        <FormControlLabel
                          control={<GiveRadio />}
                          label={
                            <Stack gap="4px">
                              <GiveText
                                sx={{
                                  color: theme.palette.text.primary,
                                }}
                                variant="bodyS"
                              >
                                {option.title}
                              </GiveText>
                              <GiveText
                                sx={{
                                  color: theme.palette.text.secondary,
                                }}
                                variant="bodyS"
                              >
                                {option.subtitle}
                              </GiveText>
                            </Stack>
                          }
                          value={option.value}
                          onChange={onChange}
                          checked={value === option.value}
                          sx={{
                            "&.MuiFormControlLabel-root": {
                              margin: "0px",
                              gap: "12px",
                              alignItems: "flex-start",
                            },
                          }}
                        />
                      </StyledBox>
                    </GiveMotionWrapper>
                  ))}
                </RadioGroup>
                {error && (
                  <GiveText pt="12px" pl="12px" color={"error"}>
                    {error.message}
                  </GiveText>
                )}
              </Box>
            )}
          />
        </Stack>
      </GiveBaseModal>
    );
  },
);
 
export default ApproveMerchantModal;
 
const StyledBox = styled(Box)(
  ({ selected, theme }: { selected: boolean; theme: CustomTheme }) => ({
    padding: "16px",
    borderRadius: "8px",
    borderWidth: "1px",
    borderStyle: "solid",
    borderColor: selected
      ? theme.palette.border?.active
      : theme.palette.border?.secondary,
  }),
);