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

100% Statements 8/8
80% Branches 4/5
100% Functions 2/2
100% Lines 8/8

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                                            1x                 14x 14x         14x 14x               14x   14x                                                                     2x                                                  
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import { Stack } from "@mui/material";
import GiveButton from "@shared/Button/GiveButton";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import GiveText from "@shared/Text/GiveText";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { FormProvider, useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { createReserveSchema } from "@validation/merchant";
import { MinimumReserveBPSFormInputs } from "../types";
import HFGiveCustomAmount from "@shared/HFInputs/HFGiveCustomAmount/HFGiveCustomAmount";
import { useUpdateReserveToggle } from "../hooks/useUpdateReserveToggle";
 
interface Props {
  initialValue: number;
  merchantID: number;
  reserveKey: "minimumReserveBPS" | "rollingReserveBPS";
  title: string;
  description: string;
  label: string;
}
 
const EditReserveModal = NiceModal.create(
  ({
    initialValue,
    merchantID,
    reserveKey,
    title,
    description,
    label = "Basis Points",
  }: Props) => {
    const { visible, remove } = useModal();
    const { handleSubmit, isLoading } = useUpdateReserveToggle({
      id: merchantID,
      reserveKey,
    });
 
    const { isMobileView } = useCustomThemeV2();
    const methods = useForm<MinimumReserveBPSFormInputs>({
      mode: "onChange",
      resolver: yupResolver(createReserveSchema(reserveKey)),
      defaultValues: {
        [reserveKey]: initialValue,
      },
    });
 
    const value = methods.watch(reserveKey);
 
    return (
      <GiveBaseModal
        open={visible}
        title={title}
        width="560px"
        {...(isMobileView && {
          height: "fit-content",
        })}
        onClose={remove}
        buttons={
          <Stack gap="12px" flexDirection="row">
            <GiveButton
              onClick={remove}
              label="Cancel"
              variant="ghost"
              size="large"
            />
            <GiveButton
              label="Confirm"
              variant="filled"
              size="large"
              disabled={value === initialValue || isLoading}
              type="submit"
              sx={{ border: "none" }}
              form="reserve-form"
            />
          </Stack>
        }
      >
        <FormProvider {...methods}>
          <Stack
            gap="24px"
            component="form"
            id="reserve-form"
            onSubmit={methods.handleSubmit((data) =>
              handleSubmit(data, remove),
            )}
          >
            <GiveText variant="bodyS" color="secondary">
              {description}
            </GiveText>
            <HFGiveCustomAmount
              name={reserveKey}
              id={reserveKey}
              label={label}
              placeholder=""
              endIcon="BPS"
              min={0}
              bounded={true}
              initialValue=""
              disableAutoFormat
            />
          </Stack>
        </FormProvider>
      </GiveBaseModal>
    );
  },
);
 
export default EditReserveModal;