import React, { useState, FC, useRef } from "react"; import { Turnstile } from "@marsidev/react-turnstile"; import type { TurnstileInstance } from "@marsidev/react-turnstile"; import ReactTooltip from "react-tooltip"; import Row from "../core/Row"; import Column from "../core/Column"; import Title from "../core/Title"; import { getNonce, toBoolean, getValidationStatus } from "../Helpers"; import Icons from "../icons"; import VideoCover from "../images/video_cover.png"; import SiteKeyTooltip from "../images/tooltip/sitekey_infotip.png"; import SecretKeyToolTIp from "../images/tooltip/secretkey_infotip.png"; type Props = { settings: { secret_key: string; site_key: string; }; store: { settings: { secret_key: string; site_key: string; }; }; siteKey: string; setSiteKey: any; secretKey: string; setSecretKey: any; validation: boolean; setValidation: any; }; const Dashboard: FC = ({ siteKey, setSiteKey, secretKey, setSecretKey, validation, setValidation, }) => { const [status, setStatus] = useState(); const [token, setToken] = useState(); const [saveChanges, setSaveChanges] = useState(false); const [showTheVideo, setShowTheVideo] = useState(false); const [displaySaveButton, setDisplaySaveButton] = useState(true); const siteKeyRef = useRef | undefined>(); const secretKeyRef = useRef | undefined>(); const widgetRef = useRef(null); const handleShowVideo = () => { setShowTheVideo((prevState) => !prevState); }; const saveSettings = (event): void => { event.preventDefault(); if (siteKey === "") { siteKeyRef.current.style.borderColor = "red"; siteKeyRef.current.className = "error"; } else { siteKeyRef.current.style.borderColor = ""; siteKeyRef.current.className = ""; } if (secretKey === "") { secretKeyRef.current.style.borderColor = "red"; secretKeyRef.current.className = "error"; } else { secretKeyRef.current.style.borderColor = ""; secretKeyRef.current.className = ""; } if (siteKey === "" || secretKey === "") { return; } wp.ajax.send("save_settings", { data: { nonce: getNonce(), settings: JSON.stringify({ site_key: siteKey, secret_key: secretKey, }), }, success({ validated }) { setValidation(validated); setSaveChanges(false); if (!token || "invalid_sitekey" === token) { siteKeyRef.current.style.borderColor = "red"; siteKeyRef.current.className = "error"; } else { siteKeyRef.current.style.borderColor = ""; siteKeyRef.current.className = ""; } }, error(err: any) { console.error(err); }, }); }; const handleVerification = (): void => { if (!secretKey) { console.log(secretKey); return; } wp.ajax.send("verify_connection", { data: { nonce: getNonce(), secret_key: secretKey, token, }, success({ validated }) { setValidation(validated); setSaveChanges(!validated); }, error(err: any) { setValidation(false); setSaveChanges(true); setStatus("error"); }, }); }; const handleOnSuccess = (data: string) => { setToken(data); setDisplaySaveButton(false); siteKeyRef.current.style.borderColor = ""; siteKeyRef.current.className = ""; }; const handleError = (data: string) => { setToken(data); setDisplaySaveButton(true); siteKeyRef.current.style.borderColor = "red"; siteKeyRef.current.className = "error"; }; if (status === "error") { secretKeyRef.current.style.borderColor = "red"; secretKeyRef.current.className = "error"; } return (
{!validation && (!siteKey || (siteKey && !token)) && (
{Icons.key}{" "} Click here {" "} to get your Site Key and{" "} Secret Key and paste them below
)} {!saveChanges && !validation && siteKey && ( <> handleError(e)} onSuccess={(e) => handleOnSuccess(e)} options={{ theme: "light", }} /> )} {!validation && siteKey && secretKey && "error" === status && (
{Icons.error_icon}
Error! Your Secret Key is not match with the Cloudflare Turnstile

Please re-check and match the Secret Key from{" "} {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */} here {Icons.link_icon}

)} {!validation && token === "invalid_sitekey" && siteKey && (
{Icons.error_icon}
Error! Your Site Key is not match with the Cloudflare Turnstile

Please re-check and match the Site Key from{" "} {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */} here {Icons.link_icon}

)} {validation && siteKey && secretKey && (
{Icons.success_icon}
Success! Your website is connected to Cloudflare Turnstile

You can get your Site Key and Secret Key always from{" "} {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */} here {Icons.link_icon}

)}
{ setSiteKey(e.target.value); setSaveChanges(true); setStatus("init"); setDisplaySaveButton(true); setToken(null); }} value={siteKey} ref={siteKeyRef} />
Copy your Secret Key from CloudFlare Dashboard and paste it below
Image
{ setSecretKey(e.target.value); setSaveChanges(true); setStatus("init"); setDisplaySaveButton(true); setToken(null); }} value={secretKey} ref={secretKeyRef} />
{(!validation || saveChanges) && displaySaveButton ? ( ) : null} {!validation && token && "invalid_sitekey" != token && siteKey && ( )} {validation && !saveChanges && ( )}
<h3> How to get Site Key and Secret Key?{" "} <span className="intro-vide-duration">01:53</span> </h3>
{showTheVideo ? ( ) : (
Video Coder
)}
); }; export default Dashboard;