diff --git a/src/components/Captcha/index.tsx b/src/components/Captcha/index.tsx new file mode 100644 index 0000000..e0982b0 --- /dev/null +++ b/src/components/Captcha/index.tsx @@ -0,0 +1,87 @@ +import React, { useCallback } from 'react'; +import HCaptcha from '@hcaptcha/react-hcaptcha'; +import { + InputContainer, + InputContainerProps, +} from '@ifrc-go/ui'; + +export type HCaptchaProps = Omit & { + name: T, + siteKey: string | undefined; + onChange: (value: string | undefined, name: T) => void; + elementRef?: React.RefObject; +}; + +function HCaptchaInput(props: HCaptchaProps) { + const { + actions, + actionsContainerClassName, + className, + disabled, + error, + errorContainerClassName, + hint, + hintContainerClassName, + icons, + iconsContainerClassName, + inputSectionClassName, + label, + readOnly, + + name, + siteKey, + onChange, + elementRef, + } = props; + + const handleVerify = useCallback( + (token: string) => { + onChange(token, name); + }, + [onChange, name], + ); + const handleError = useCallback( + (err: string) => { + // eslint-disable-next-line no-console + console.error(err); + onChange(undefined, name); + }, + [onChange, name], + ); + const handleExpire = useCallback( + () => { + onChange(undefined, name); + }, + [onChange, name], + ); + + return ( + + )} + /> + ); +} + +export default HCaptchaInput; diff --git a/src/views/Login/index.tsx b/src/views/Login/index.tsx index 282c51c..620544d 100644 --- a/src/views/Login/index.tsx +++ b/src/views/Login/index.tsx @@ -1,4 +1,8 @@ -import { useMemo } from 'react'; +import { + useMemo, + useState, +} from 'react'; +import HCaptcha from '@hcaptcha/react-hcaptcha'; import { Button, PasswordInput, @@ -30,13 +34,24 @@ export function Component() { const formValue = defaultFormValue; const fieldError: FormFields = {}; + const [captchaToken, setCaptchaToken] = useState(null); + const handleFormSubmit = useMemo( () => (event: React.FormEvent) => { event.preventDefault(); + + if (!captchaToken) { + // Handle the case when captcha token is missing + } }, - [], + [captchaToken], ); + const handleCaptchaVerify = (token: string) => { + if (setCaptchaToken) { + setCaptchaToken(token); + } + }; const signupInfo = resolveToComponent( strings.loginDontHaveAccount, { @@ -99,6 +114,10 @@ export function Component() {
+