All files / src/features/MerchantPortal/DeveloperApi/modals DeveloperApiCreationModal.tsx

100% Statements 11/11
75% Branches 6/8
100% Functions 2/2
100% Lines 11/11

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                                2x   2x   2x                 30x   30x       30x 30x 30x   30x                                                                                               30x                                                                                     120x                        
import NiceModal from "@ebay/nice-modal-react";
import { Stack } from "@mui/material";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { FormProvider } from "react-hook-form";
import GiveBaseModal from "@shared/modals/GiveBaseModal";
import { useAppTheme } from "@theme/v2/Provider";
import GiveButton from "@shared/Button/GiveButton";
import { MAX_TEXT_LENGTH } from "@constants/constants";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import GiveText from "@shared/Text/GiveText";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import AllowedOriginsInput from "../components/AllowedOriginInput";
import ApiKeyEnvInput from "../components/ApiKeyEnvInput";
import { useGenerateApiKey } from "@features/MerchantPortal/DeveloperApi/hooks/useGenerateApiKey";
import { MERCHANT_API_KEY_SUCCESS_MODAL } from "modals/modal_names";
 
const MODAL_WIDTH = "640";
const MODAL_TITLE =
  "Generate a new API key by providing the necessary information below.";
 
const DeveloperApiCreationModal = NiceModal.create(
  ({
    isProductionOptionEnabled,
    isDisabledCreation,
  }: {
    isProductionOptionEnabled: boolean;
    isDisabledCreation?: boolean;
  }) => {
    const { open, handleCloseModal, methods, onSubmit, isLoading } =
      useGenerateApiKey(MERCHANT_API_KEY_SUCCESS_MODAL);
 
    const { isMobileView } = useCustomTheme();
    const {
      formState: { isValid },
      watch,
    } = methods;
    const originInput = watch("origin");
    const { palette } = useAppTheme();
 
    const inputList = [
      {
        input: (
          <HFGiveInput
            name="name"
            label="API Key Name"
            placeholder="eg. Staging API Key"
            data-testid="generate-key-name-input"
            helperText="A unique name for your API key to help you identify its purpose"
            fullWidth
            FormHelperTextProps={{
              sx: {
                paddingLeft: "0px !important",
                marginTop: "8px !important",
                color: palette.text.secondary,
              },
            }}
          />
        ),
      },
      {
        input: (
          <HFGiveInput
            name="description"
            label="Description (Optional)"
            placeholder="Add a description for your API key"
            data-testid="generate-key-description-input"
            multiline
            rows={8}
            fullWidth
            inputProps={{
              maxLength: `${MAX_TEXT_LENGTH}`,
            }}
          />
        ),
      },
      {
        input: <AllowedOriginsInput />,
      },
      {
        input: (
          <ApiKeyEnvInput
            isProductionOptionEnabled={isProductionOptionEnabled}
          />
        ),
      },
    ];
 
    return (
      <GiveBaseModal
        title="Generate API Key"
        open={open}
        width={isMobileView ? "100%" : `${MODAL_WIDTH}px`}
        scroll="paper"
        onClose={handleCloseModal}
        slotProps={{
          backdrop: { sx: { backgroundColor: palette.surface?.overlay } },
        }}
        buttons={
          <>
            <GiveButton
              size="large"
              variant="ghost"
              onClick={handleCloseModal}
              disabled={isLoading}
              label={isMobileView ? "Close" : "Cancel"}
            />
            <GiveButton
              size="large"
              type="submit"
              disabled={
                isLoading || !isValid || !!originInput || isDisabledCreation
              }
              label="Generate"
              form="generate-merchant-api-key-form"
              data-testid="generate-key-submit-button"
            />
          </>
        }
      >
        <FormProvider {...methods}>
          <Stack
            gap="24px"
            component="form"
            onSubmit={methods.handleSubmit(onSubmit)}
            id="generate-merchant-api-key-form"
          >
            <GiveText color="secondary" variant="bodyS">
              {MODAL_TITLE}
            </GiveText>
            {inputList.map(({ input }, index) => (
              <FadeUpWrapper delay={70 * (index + 1)} key={index}>
                {input}
              </FadeUpWrapper>
            ))}
          </Stack>
        </FormProvider>
      </GiveBaseModal>
    );
  },
);
 
export default DeveloperApiCreationModal;