All files / src/sections/PayBuilder/Checkout/components RecaptchaChallengeDialog.tsx

86.79% Statements 46/53
65% Branches 13/20
90% Functions 9/10
86.79% Lines 46/53

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                                        1x   17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x   17x 4x     4x 4x 4x     17x 17x 6x 6x       17x 12x 5x 5x   12x 6x   6x   1x     1x 1x 1x               1x     1x 1x         1x       5x     1x     1x 1x         1x       17x       1x     1x                                                    
import { Box, Dialog, DialogContent, DialogTitle } from "@mui/material";
import { useCallback, useEffect, useRef } from "react";
import * as Sentry from "@sentry/react";
import NiceModal from "@ebay/nice-modal-react";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import {
  renderRecaptchaCheckbox,
  resetRecaptchaCheckbox,
} from "@utils/getRecaptchaToken";
import GiveButton from "@shared/Button/GiveButton";
import GiveText from "@shared/Text/GiveText";
import { showMessage } from "@common/Toast";
import { CHECKOUT_ERROR_MESSAGES } from "@sections/PayBuilder/constants";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
 
type RecaptchaChallengeDialogProps = {
  onVerify: (token: string) => void;
  onDismiss: () => void;
};
 
export const RecaptchaChallengeDialog = NiceModal.create(
  ({ onVerify, onDismiss }: RecaptchaChallengeDialogProps) => {
    const { open, onClose, TransitionProps } = useNiceModal();
    const { isDesktopView } = useCustomThemeV2();
    const dismissedRef = useRef(false);
    const verifiedRef = useRef(false);
    const widgetIdRef = useRef<number | null>(null);
    const onVerifyRef = useRef(onVerify);
    const onCloseRef = useRef(onClose);
    const onDismissRef = useRef(onDismiss);
    const isDesktopViewRef = useRef(isDesktopView);
    onVerifyRef.current = onVerify;
    onCloseRef.current = onClose;
    onDismissRef.current = onDismiss;
    isDesktopViewRef.current = isDesktopView;
 
    const dismiss = useCallback(() => {
      Iif (dismissedRef.current || verifiedRef.current) {
        return;
      }
      dismissedRef.current = true;
      onDismissRef.current();
      onCloseRef.current();
    }, []);
 
    useEffect(() => {
      if (open) {
        dismissedRef.current = false;
        verifiedRef.current = false;
      }
    }, [open]);
 
    const setContainer = useCallback((el: HTMLDivElement | null) => {
      if (widgetIdRef.current !== null) {
        resetRecaptchaCheckbox(widgetIdRef.current);
        widgetIdRef.current = null;
      }
      if (!el) {
        return;
      }
      void renderRecaptchaCheckbox(el, {
        callback: (token: string) => {
          Iif (dismissedRef.current) {
            return;
          }
          verifiedRef.current = true;
          onVerifyRef.current(token);
          onCloseRef.current();
        },
        "expired-callback": () => {
          if (widgetIdRef.current !== null) {
            resetRecaptchaCheckbox(widgetIdRef.current);
          }
        },
        "error-callback": () => {
          Iif (dismissedRef.current) {
            return;
          }
          Sentry.captureException(new Error("reCAPTCHA checkbox error"));
          showMessage(
            "Error",
            CHECKOUT_ERROR_MESSAGES.RECAPTCHA_FAILED,
            isDesktopViewRef.current,
          );
          dismiss();
        },
      })
        .then((id) => {
          widgetIdRef.current = id;
        })
        .catch((err) => {
          Iif (dismissedRef.current) {
            return;
          }
          Sentry.captureException(err);
          showMessage(
            "Error",
            CHECKOUT_ERROR_MESSAGES.RECAPTCHA_FAILED,
            isDesktopViewRef.current,
          );
          dismiss();
        });
    }, [dismiss]);
 
    return (
      <Dialog
        open={open}
        onClose={(_, reason) => {
          Iif (reason === "backdropClick") {
            return;
          }
          dismiss();
        }}
        TransitionProps={TransitionProps}
      >
        <DialogTitle>{CHECKOUT_ERROR_MESSAGES.CHALLENGE_TITLE}</DialogTitle>
        <DialogContent>
          <GiveText variant="bodyS" color="secondary" sx={{ mb: 2 }}>
            {CHECKOUT_ERROR_MESSAGES.CHALLENGE_BODY}
          </GiveText>
          <Box display="flex" justifyContent="center" sx={{ mb: 2 }}>
            <div ref={setContainer} />
          </Box>
          <Box display="flex" justifyContent="center">
            <GiveButton
              type="button"
              variant="ghost"
              label={CHECKOUT_ERROR_MESSAGES.CHALLENGE_CANCEL}
              onClick={dismiss}
            />
          </Box>
        </DialogContent>
      </Dialog>
    );
  },
);