All files / src/sections/VerifyAccountHolder_v2 NewSelfie.tsx

73.33% Statements 22/30
73.07% Branches 19/26
66.66% Functions 4/6
78.57% Lines 22/28

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                                    13x                           13x 13x   13x 13x 13x       13x   13x                     13x 1x                     12x 1x                   11x   11x                                                                                                                                                                                           19x               1x   1x                                                                         19x         1x                       19x               19x 1x        
import React from "react";
import { SelfieProps } from "./components/SelfieComponent";
import { Box, Stack, styled } from "@mui/material";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { Button } from "@common/Button";
import CameraAccessIcon from "@assets/icons/CameraAccessIcon";
import { QRCodeSVG } from "qrcode.react";
import { Text } from "@common/Text";
import { palette } from "@palette";
import { delay, isEmpty } from "lodash";
import SelfieButtons from "./components/SelfieButtons";
import { CameraIcon } from "@phosphor-icons/react";
import { useGetIdentificationFiles, useGetSelfieOTC } from "./hooks/useCamera";
import InstructionsComponent from "./components/InstructionsComponent";
import LoadingSelfie from "./components/LoadingSelfie";
import { isLocalOrOnlineImage } from "./utils";
 
function NewSelfie(props: Partial<SelfieProps>) {
  const { isMobileView } = useCustomTheme();
 
  const {
    stream,
    image,
    stopCamera,
    setImage,
    videoRef,
    canvasRef,
    takePicture,
    isSessionActive,
    handleEndSession,
    requestCameraAccess,
    streamLoading,
  } = props || {};
  const isStreamOn = !!stream;
 
  const isImageTaken = !isEmpty(image);
  const selfieButtons = isMobileView ? <></> : <SelfieButtons {...props} />;
  const { data: otcData, isLoading } = useGetSelfieOTC({
    enabled: true,
  });
 
  const LINK = `https://${window.location.hostname}/make_selfie?token=${otcData?.token}&exp_date=${otcData?.expiresdAt}`;
 
  useGetIdentificationFiles({
    enablePolling: false, // !isLoading && !isImageTaken && !isStreamOn, // @TODO enable again when BE fixes response not including files
    onSuccess: async (data: any) => {
      const files = data?.data;
      if (isEmpty(files)) return setImage?.(null);
      //  await deleteDocumentsInLoop(files, merchantId, false);
      setImage?.(files?.[0]?.fileURL);
    },
    filter: `?filter=attTypeName:"account_owner_selfie"%3BisUploaded:true`,
  });
 
  if (isImageTaken) {
    return (
      <SelfieImage
        isLoading={props.isLoading}
        image={image}
        stopCamera={stopCamera}
        setImage={setImage}
        selfieButtons={selfieButtons}
        requestCameraAccess={requestCameraAccess}
      />
    );
  }
  if (isStreamOn) {
    return (
      <SelfieStream
        videoRef={videoRef}
        canvasRef={canvasRef}
        takePicture={takePicture}
        selfieButtons={selfieButtons}
      />
    );
  }
 
  Iif (streamLoading) return null;
 
  return (
    <Box pb={isSessionActive ? 20 : undefined}>
      {isSessionActive && (
        <Box
          borderRadius="12px"
          mb="32px"
          bgcolor={palette.tag.warning.bg}
          p="16px"
        >
          <Text color={palette.warning.text}>
            Session active on another device. Finish or invalidate it to
            continue
          </Text>
 
          <Text
            sx={{
              cursor: "pointer",
            }}
            mt="12px"
            color={palette.neutral[70]}
            onClick={handleEndSession}
          >
            End Old Session
          </Text>
        </Box>
      )}
      <InstructionsComponent />
 
      {!isSessionActive && (
        <Stack
          display={{ xs: "none", sm: "flex" }}
          flexDirection="row"
          borderRadius="16px"
          gap="16px"
          bgcolor={palette.neutral[10]}
          mt="32px"
          p="24px 16px"
          justifyContent="space-between"
        >
          <Box
            justifyContent="center"
            alignItems="center"
            display="flex"
            flexDirection="column"
            flex={1}
            gap="16px"
          >
            <CameraAccessIcon width="80" height="80" />
            <Text fontWeight={400} fontSize="14px" textAlign="center">
              Allow camera access on your computer and take the photo.
            </Text>
            {selfieButtons}
          </Box>
          <Box
            display="flex"
            flexDirection="column"
            alignItems="center"
            height="192px"
            justifyContent="space-between"
          >
            <Box bgcolor={palette.neutral[60]} width="1px" height="40%" />
            <Text fontSize="14px" fontWeight={400}>
              OR
            </Text>
            <Box bgcolor={palette.neutral[60]} width="1px" height="40%" />
          </Box>
          <Box
            gap="17px"
            display="flex"
            flexDirection="column"
            alignItems="center"
            flex={1}
          >
            <QRCodeSVG
              style={{ height: "124px", width: "124px" }}
              bgColor="transparent"
              value={LINK}
            />
            <Text fontWeight={400} fontSize="14px" textAlign="center">
              Scan the QR code with your smartphone and capture the photo.
            </Text>
          </Box>
        </Stack>
      )}
    </Box>
  );
}
 
export default NewSelfie;
 
interface ChildProps extends Partial<SelfieProps> {
  selfieButtons: JSX.Element;
}
 
const SelfieImage = ({
  image,
  selfieButtons,
  setImage,
  requestCameraAccess,
  isLoading,
  stopCamera,
}: ChildProps) => {
  const { isLocal } = isLocalOrOnlineImage(image || "");
 
  return (
    <Stack gap="115px" alignItems="center" justifyContent="space-between">
      {isLoading ? (
        <Stack style={base} alignItems="center" justifyContent="center">
          <LoadingSelfie />
        </Stack>
      ) : (
        <Box height="auto" minWidth="100%">
          <Box
            data-testid="selfie-image"
            src={image || ""}
            style={base}
            component="img"
          />
          {isLocal && (
            <Button
              size="medium"
              background="text"
              sx={{ padding: "12px 32px", margin: "auto", marginTop: "16px" }}
              onClick={() => {
                stopCamera?.();
                setImage?.(null);
                requestCameraAccess?.();
              }}
              startIcon={<CameraIcon size={20} />}
            >
              Retake Selfie
            </Button>
          )}
        </Box>
      )}
 
      {selfieButtons}
    </Stack>
  );
};
 
export const SelfieStream = ({
  videoRef,
  canvasRef,
  selfieButtons,
}: ChildProps) => (
  <Stack
    data-testid="Video-stream-Container"
    gap="115px"
    alignItems="center"
    justifyContent="space-between"
  >
    <Video ref={videoRef} autoPlay playsInline />
    <canvas ref={canvasRef} style={{ display: "none" }} />
    {selfieButtons}
  </Stack>
);
 
const base: React.CSSProperties = {
  width: "100%",
  height: "300px",
  borderRadius: "16px",
  objectFit: "cover",
  backgroundColor: palette.neutral[10],
};
 
const Video = styled("video")(() => {
  return {
    ...base,
  };
});