import React, { useEffect, useState, useRef } from 'react'; import { StyleSheet } from 'react-native'; import { useForm, Controller } from 'react-hook-form'; import Recaptcha from 'react-native-recaptcha-that-works' import { TouchableOpacity } from 'react-native-gesture-handler'; import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'; import { ForgotPasswordForm as ForgotPasswordController, useLanguage, useToast, ToastType, useConfig } from 'ordering-components/native'; import { useTheme } from 'styled-components/native'; import NavBar from '../NavBar'; import { FormInput, FormSide, RecaptchaButton } from '../LoginForm/styles' import { Container } from './styles' import { OButton, OInput, OText } from '../shared'; const ForgotPasswordUI = (props: any) => { const { navigation, formState, handleButtonForgotPasswordClick, handleReCaptcha, enableReCaptcha, reCaptchaValue } = props; const [, t] = useLanguage(); const [, { showToast }] = useToast(); const [{ configs }] = useConfig(); const { control, handleSubmit, errors } = useForm(); const [recaptchaConfig, setRecaptchaConfig] = useState({}) const [recaptchaVerified, setRecaptchaVerified] = useState(false) const theme = useTheme(); const styles = StyleSheet.create({ inputStyle: { marginBottom: 25, borderWidth: 1, borderColor: theme.colors.border, minHeight: 40, borderRadius: 7.6, } }); const [emailSent, setEmailSent] = useState(null); const recaptchaRef = useRef({}); const onSubmit = (values: any) => { setEmailSent(values.email) handleButtonForgotPasswordClick && handleButtonForgotPasswordClick(values) } const handleChangeInputEmail = (value: string, onChange: any) => { onChange(value.toLowerCase().replace(/[&,()%";:รง?<>{}\\[\]\s]/g, '')) } const handleOpenRecaptcha = () => { setRecaptchaVerified(false) handleReCaptcha(null) if (reCaptchaValue) return if (!recaptchaConfig?.siteKey) { showToast(ToastType.Error, t('NO_RECAPTCHA_SITE_KEY', 'The config doesn\'t have recaptcha site key')); return } if (!recaptchaConfig?.baseUrl) { showToast(ToastType.Error, t('NO_RECAPTCHA_BASE_URL', 'The config doesn\'t have recaptcha base url')); return } recaptchaRef.current.open() } const onRecaptchaVerify = (token: any) => { setRecaptchaVerified(true) handleReCaptcha(token) } const handleRecaptchaExpire = () => { setRecaptchaVerified(false) handleReCaptcha(null) } useEffect(() => { if (!formState.loading && emailSent) { if (formState.result?.error) { setEmailSent(null) formState.result?.result && showToast( ToastType.Error, typeof formState.result?.result === 'string' ? formState.result?.result : formState.result?.result[0] ) return } showToast( ToastType.Success, t('IF_ACCOUNT_EXIST_EMAIL_SEND_PASSWORD', 'If an account exists with this email a password will be sent') ) } }, [formState]) useEffect(() => { if (configs && Object.keys(configs).length > 0 && enableReCaptcha) { setRecaptchaConfig({ siteKey: configs?.security_recaptcha_site_key?.value || null, baseUrl: configs?.security_recaptcha_base_url?.value || null }) } }, [configs, enableReCaptcha]) return ( navigation?.canGoBack() && navigation.goBack()} showCall={false} paddingTop={0} style={{ flexDirection: 'column', alignItems: 'flex-start' }} titleStyle={{ width: '100%', marginLeft: 0, paddingLeft: 0 }} titleWrapStyle={{ paddingHorizontal: 0, paddingStart: 0, flex: 1 }} /> {t('FORGOT_PASSWORD_TEXT_MESSAGE', "Enter your email address and we'll sent a link to reset your password.")} {errors?.email && ( {errors?.email?.message}{errors?.email?.type === 'required' && '*'} )} ( { handleChangeInputEmail(e, onChange) }} value={value} autoCapitalize='none' autoCorrect={false} type='email-address' autoCompleteType='email' returnKeyType='done' blurOnSubmit onSubmitEditing={handleSubmit(onSubmit)} /> )} name="email" rules={{ required: t('VALIDATION_ERROR_EMAIL_REQUIRED', 'The field Email is required').replace('_attribute_', t('EMAIL', 'Email')), pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, message: t('INVALID_ERROR_EMAIL', 'Invalid email address').replace('_attribute_', t('EMAIL', 'Email')) } }} defaultValue="" /> {enableReCaptcha && ( <> {recaptchaVerified ? ( ) : ( )} {t('VERIFY_ReCAPTCHA', 'Verify reCAPTCHA')} )} { } : handleSubmit(onSubmit)} style={{ borderRadius: 7.6, shadowOpacity: 0 }} /> ); }; export const ForgotPasswordForm = (props: any) => { const ForgotPasswordProps = { ...props, isRecaptchaEnable: true, UIComponent: ForgotPasswordUI } return }