All files / src/features/Merchants/MerchantSidePanel/components/PrimaryAccountHolderSection IDUploaderComponents.tsx

73.07% Statements 19/26
41.37% Branches 12/29
77.77% Functions 7/9
76% Lines 19/25

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                                                                  63x                             231x       231x   231x                         231x   231x               2x                                                                                                                                             2x 2x 2x                   63x       63x                 79x                                                         63x 79x               63x 79x             63x 231x        
import { Box, CircularProgress, Skeleton, Stack } from "@mui/material";
import { IInputProps, ILayoutProps } from "react-dropzone-uploader";
import { Image } from "@components/common/StyledImage/Image";
import { UploaderState } from "@components/Merchants/MerchantPreview/components/PrimaryAccountHolder/PAHUploaderMachine";
import { TOwnerFile } from "@components/Merchants/MerchantPreview/components/PrimaryAccountHolder/types";
import { useGetOperatingSystem } from "@hooks/common/useGetOperatingSystem";
import { Icon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { MouseEvent, useRef } from "react";
import { IDUploaderMapper } from "./IDUploaderMapper";
 
type Props = IInputProps & {
  isUploadable: boolean;
  state: UploaderState;
  fileUrl: string;
  type: TOwnerFile;
  fileName?: string;
  isMobile?: boolean;
  disabled?: boolean;
  Icon?: Icon;
  previewDocument: ({
    tmpUrl,
    type,
    fileName,
  }: {
    tmpUrl?: string | undefined;
    type?: TOwnerFile | undefined;
    fileName?: string;
  }) => void;
  error?: string;
};
 
export const IDUploaderInput = ({
  accept,
  fileUrl,
  isMobile,
  getFilesFromEvent,
  state,
  type,
  fileName,
  onFiles,
  previewDocument,
  isUploadable,
  disabled = false,
  error,
}: Props) => {
  // There was a discrepancy between the Android and iOS.
  const { operatingSystemTracker } = useGetOperatingSystem();
  // iOS required a double click to open the preview modal, passing at first click for the hover state
  // Android opened without passing through the hover state
  // This makes both passing through the hover state at first click
  const firstClickTracker = useRef<boolean>(false);
 
  const clickHandler = (e: MouseEvent<HTMLDivElement>) => {
    const showAfterFirstClick = isMobile && firstClickTracker.current === false;
    const defaultBehavior =
      !isMobile || operatingSystemTracker.current === "iOS";
    if (
      (defaultBehavior || showAfterFirstClick) &&
      state === UploaderState.ON_UPLOADED
    ) {
      return previewDocument({ tmpUrl: fileUrl, type, fileName });
    }
    firstClickTracker.current = false;
  };
 
  const { palette } = useAppTheme();
 
  return (
    <Stack
      height="100%"
      width="100%"
      alignItems="center"
      padding="24px"
      justifyContent="center"
      onMouseEnter={() => {
        firstClickTracker.current = true;
      }}
      onMouseLeave={() => {
        if (firstClickTracker.current) firstClickTracker.current = false;
      }}
      onClick={state === UploaderState.ON_UPLOADED ? clickHandler : undefined}
      sx={{
        height: "100%",
        borderRadius: "12px",
        pointerEvents: "initial",
        cursor: "pointer",
        "&:hover": {
          backgroundColor: palette.primitive?.transparent["darken-5"],
        },
      }}
    >
      <Stack
        height="100%"
        width="100%"
        component="label"
        alignItems="center"
        justifyContent="center"
        sx={{ cursor: "pointer" }}
      >
        <StyledContainer>
          <Stack gap={0.5} justifyContent="center" alignItems="center">
            {state !== UploaderState.ON_UPLOADED && (
              <>
                <Box
                  sx={{
                    backgroundColor: palette.primitive?.transparent["darken-5"],
                    borderRadius: "50%",
                    display: "flex",
                    padding: "8px",
                  }}
                >
                  {state === UploaderState.ON_UPLOADING ? (
                    <CircularProgress
                      size="20px"
                      sx={{ color: palette.primitive?.neutral[90] }}
                    />
                  ) : (
                    IDUploaderMapper[type].icon(palette.primitive?.neutral[90])
                  )}
                </Box>
                <GiveText paddingY="8px" variant="bodyS" color="secondary">
                  {IDUploaderMapper[type].title}
                </GiveText>
              </>
            )}
            <GiveText variant="bodyXS" color="secondary">
              Drag and drop your files or{" "}
              <StyledLink>Click to browse</StyledLink>
            </GiveText>
            {error && (
              <GiveText variant="bodyXS" color="error1">
                {error}
              </GiveText>
            )}
          </Stack>
        </StyledContainer>
        {![UploaderState.ON_UPLOADED, UploaderState.ON_UPLOADING].includes(
          state,
        ) && (
          <input
            disabled={Boolean(fileUrl) || !isUploadable || disabled}
            type="file"
            id="upload"
            accept={accept}
            style={{ display: "none" }}
            onChange={async (e) => {
              const chosenFiles = await getFilesFromEvent(e);
              onFiles(chosenFiles);
              (e.target as any).value = null;
            }}
            data-testid="image-upload-input"
          />
        )}
      </Stack>
    </Stack>
  );
};
 
const StyledContainer = styled(Box)({
  textAlign: "center",
});
 
export const IDUploaderLayout = ({
  fileUrl,
  actions,
  type,
}: ILayoutProps & {
  fileUrl: string;
  actions?: React.ReactNode;
  type: TOwnerFile;
}) => {
  return (
    <StyledLayout>
      <Stack direction="row" justifyContent="space-between" alignItems="center">
        <Stack gap={2}>
          <GiveText variant="bodyS" color="secondary">
            {IDUploaderMapper[type].title}
          </GiveText>
          <Stack direction="row">{actions}</Stack>
        </Stack>
        <Box>
          {fileUrl ? (
            <StyledImg
              src={
                typeof fileUrl === "string"
                  ? fileUrl
                  : URL.createObjectURL(fileUrl)
              }
              alt="id image"
              data-testid="id-file-preview"
            />
          ) : (
            <Skeleton width="108px" height="108px" variant="rounded" />
          )}
        </Box>
      </Stack>
    </StyledLayout>
  );
};
 
const StyledLayout = styled(Box)(({ theme }) => {
  return {
    backgroundColor: theme.palette.primitive?.transparent["darken-5"],
    display: "block",
    borderRadius: "8px",
    padding: "24px",
  };
});
 
const StyledImg = styled(Image)(() => {
  return {
    width: "156px",
    height: "108px",
    objectFit: "cover",
  };
});
 
const StyledLink = styled("span")(({ theme }) => {
  return {
    color: theme.palette.primitive?.blue[100],
  };
});