All files / src/sections/VerifyAccountHolder_v2/components SelfieButtons.tsx

45.45% Statements 15/33
55.55% Branches 10/18
25% Functions 1/4
45.45% Lines 15/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                                    9x 9x 9x 9x 9x 9x 9x 9x 9x                                                           9x 1x                         8x 7x                                   1x 1x                                                                                                            
import { Button } from "@common/Button";
import { SelfieProps } from "./SelfieComponent";
import { isEmpty } from "lodash";
import { ArrowLeftIcon, CameraIcon } from "@phosphor-icons/react";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { isLocalOrOnlineImage } from "../utils";
import { useUploadFiles } from "@hooks/upload-api/uploadHooks";
import { challengeSlugs } from "@constants/challengeSlugs";
import { useGetCurrentMerchantId } from "@hooks/common";
import { base64ToFile } from "@utils/helpers";
import { useQueryClient } from "react-query";
import { checkPortals } from "@utils/routing";
import {
  QKEY_GET_MERCHANT_BY_ID,
  QKEY_IDENTIFICATION_FILES,
} from "@constants/queryKeys";
 
function SelfieButtons(props: Partial<SelfieProps>) {
  const isStreamOn = !!props.stream;
  const isImageTaken = !isEmpty(props?.image);
  const { isMobileView } = useCustomTheme();
  const { isLocal } = isLocalOrOnlineImage(props?.image || "");
  const { handleUpload } = useUploadFiles();
  const queryClient = useQueryClient();
  const { merchantId } = useGetCurrentMerchantId();
  const { isMerchantPortal } = checkPortals();
  const handleUploadImage = async () => {
    if (!isLocal) {
      props?.stopCamera?.();
      props?.setCompleted?.(true);
      return;
    }
    props.setIsUploading?.(true);
    props?.setIsloading?.(true);
 
    await handleUpload(
      {
        list: [
          { file: base64ToFile(props?.image || "", "account_owner_selfie") },
        ],
        merchantId: merchantId,
        resourceID: merchantId,
        attachmentType: "account_owner_selfie",
        label: "",
        tag: `${isMerchantPortal ? "merchant" : "provider"} upload`,
      },
      challengeSlugs.PRIMARY_5,
    );
 
    props.setIsUploading?.(false);
    props?.setIsloading?.(false);
    props?.setCompleted?.(true);
    queryClient.invalidateQueries([QKEY_IDENTIFICATION_FILES, merchantId]);
    queryClient.invalidateQueries(QKEY_GET_MERCHANT_BY_ID);
  };
 
  if (isImageTaken)
    return (
      <>
        <Button
          size="medium"
          background="primary"
          sx={{ padding: "12px 32px", margin: "auto", marginTop: "16px" }}
          onClick={handleUploadImage}
          disabled={props?.isLoading}
        >
          Finish
        </Button>
      </>
    );
  if (!isStreamOn && !isImageTaken)
    return (
      <Button
        size="medium"
        background="primary"
        sx={{
          padding: "12px 32px",
          margin: "auto",
          marginTop: "16px",
          minWidth: "181px !important",
        }}
        onClick={props.requestCameraAccess}
        startIcon={<CameraIcon size={20} />}
        data-testid="allow-access-button"
      >
        Allow Access
      </Button>
    );
 
  Eif (isStreamOn)
    return (
      <>
        <Button
          size="medium"
          background="primary"
          sx={{ padding: "12px 32px", margin: "auto", marginTop: "16px" }}
          onClick={() => {
            props?.takePicture?.();
            props?.stopCamera?.();
          }}
          startIcon={<CameraIcon size={20} />}
        >
          Take Selfie
        </Button>
 
        {isMobileView && (
          <Button
            size="medium"
            background="tertiary"
            sx={{
              padding: "12px 32px",
              marginTop: "16px",
            }}
            onClick={() => {
              props?.stopCamera?.();
              props?.setImage?.(null);
              props?.onBack?.();
            }}
            startIcon={<ArrowLeftIcon />}
          >
            Back
          </Button>
        )}
      </>
    );
  return (
    <div>
      <Button
        size="medium"
        background="primary"
        sx={{
          padding: "12px 32px",
          margin: "auto",
          marginTop: "16px",
        }}
        onClick={props.requestCameraAccess}
      >
        Allow Access
      </Button>
    </div>
  );
}
 
export default SelfieButtons;