import React, { useEffect, useState, useRef } from 'react'; import { Pressable, StyleSheet, View, Keyboard } from 'react-native'; import Spinner from 'react-native-loading-spinner-overlay'; import { useForm, Controller } from 'react-hook-form'; import { PhoneInputNumber } from '../PhoneInputNumber' import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons' import { LoginForm as LoginFormController, useLanguage, useConfig, useSession, ToastType, useToast } from 'ordering-components/native'; import { FacebookLogin } from '../FacebookLogin'; import { VerifyPhone } from '../../../../../src/components/VerifyPhone'; import { OModal } from '../../../../../src/components/shared'; import { Container, ButtonsWrapper, LoginWith, FormSide, FormInput, OTabs, OTab, SocialButtons, OrSeparator, LineSeparator } from './styles'; import { _removeStoreData } from '../../providers/StoreUtil'; import NavBar from '../NavBar' import { OText, OButton, OInput } from '../shared'; import { LoginParams } from '../../types'; import { useTheme } from 'styled-components/native'; const LoginFormUI = (props: LoginParams) => { const { loginTab, formState, navigation, useLoginByEmail, useLoginByCellphone, loginButtonText, forgotButtonText, verifyPhoneState, checkPhoneCodeState, registerButtonText, setCheckPhoneCodeState, handleButtonLoginClick, handleSendVerifyCode, handleCheckPhoneCode, onNavigationRedirect, notificationState } = props const theme = useTheme() const loginStyle = StyleSheet.create({ btnOutline: { backgroundColor: '#FFF', color: theme.colors.primary, borderRadius: 0 }, inputStyle: { marginBottom: 25, backgroundColor: theme.colors.backgroundGray, borderRadius: 0 }, wrapperTxt: { flexDirection: 'row', width: '100%', justifyContent: 'space-between', marginBottom: 8 }, linkTxt: { flexDirection: 'row', justifyContent: 'center', marginBottom: 40 } }); const [, { showToast }] = useToast() const [, t] = useLanguage() const [{ configs }] = useConfig() const [, { login }] = useSession() const { control, handleSubmit, errors } = useForm(); const [passwordSee, setPasswordSee] = useState(false); const [isLoadingVerifyModal, setIsLoadingVerifyModal] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false); const [isFBLoading, setIsFBLoading] = useState(false); const [phoneInputData, setPhoneInputData] = useState({ error: '', phone: { country_phone_code: null, cellphone: null } }); const inputRef = useRef({}) const handleChangeTab = (val: string) => { props.handleChangeTab(val); setPasswordSee(false); } const onSubmit = (values: any) => { Keyboard.dismiss() if (phoneInputData.error) { showToast(ToastType.Error, phoneInputData.error); return } handleButtonLoginClick({ ...values, ...phoneInputData.phone }); } const handleVerifyCodeClick = () => { if (phoneInputData.error) { showToast(ToastType.Error, phoneInputData.error); return } if ( !phoneInputData.error && !phoneInputData.phone.country_phone_code && !phoneInputData.phone.cellphone ) { showToast(ToastType.Error, t('VALIDATION_ERROR_MOBILE_PHONE_REQUIRED', 'The field Mobile phone is required.')) return } handleSendVerifyCode && handleSendVerifyCode(phoneInputData.phone) setIsLoadingVerifyModal(true) } const handleSuccessFacebook = (user: any) => { _removeStoreData('isGuestUser') login({ user, token: user.session.access_token }) } const handleChangeInputEmail = (value : string, onChange : any) => { onChange(value.toLowerCase().replace(/[&,()%";:รง?<>{}\\[\]\s]/g, '')) } useEffect(() => { if (!formState.loading && formState.result?.error) { formState.result?.result && showToast( ToastType.Error, typeof formState.result?.result === 'string' ? formState.result?.result : formState.result?.result[0] ) } }, [formState]) useEffect(() => { if (verifyPhoneState && !verifyPhoneState?.loading) { if (verifyPhoneState.result?.error) { const message = typeof verifyPhoneState?.result?.result === 'string' ? verifyPhoneState?.result?.result : verifyPhoneState?.result?.result[0] verifyPhoneState.result?.result && showToast( ToastType.Error, message ) setIsLoadingVerifyModal(false) return } const okResult = verifyPhoneState.result?.result === 'OK' if (okResult) { !isModalVisible && setIsModalVisible(true) setIsLoadingVerifyModal(false) } } }, [verifyPhoneState]) useEffect(() => { if (Object.keys(errors).length > 0) { // Convert all errors in one string to show in toast provider const list = Object.values(errors) let stringError = '' if (phoneInputData.error) { list.unshift({ message: phoneInputData.error }) } if ( loginTab === 'cellphone' && !phoneInputData.error && !phoneInputData.phone.country_phone_code && !phoneInputData.phone.cellphone ) { list.unshift({ message: t('VALIDATION_ERROR_MOBILE_PHONE_REQUIRED', 'The field Mobile phone is required.') }) } list.map((item: any, i: number) => { stringError += (i + 1) === list.length ? `- ${item.message}` : `- ${item.message}\n` }) showToast(ToastType.Error, stringError) } }, [errors]) return ( navigation?.canGoBack() && navigation.goBack()} showCall={false} style={{ marginHorizontal: 10 }} paddingTop={0} /> {useLoginByEmail && useLoginByCellphone && ( {useLoginByEmail && ( handleChangeTab('email')}> {t('BY_EMAIL', 'by Email')} )} {useLoginByCellphone && ( handleChangeTab('cellphone')}> {t('BY_PHONE', 'by Phone')} )} )} {(useLoginByCellphone || useLoginByEmail) && ( {useLoginByEmail && loginTab === 'email' && ( <> {t('EMAIL', 'Email')} ( { handleChangeInputEmail(e, onChange) }} value={value} autoCapitalize='none' autoCorrect={false} type='email-address' autoCompleteType='email' returnKeyType='next' onSubmitEditing={() => inputRef.current?.focus()} blurOnSubmit={false} /> )} 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="" /> )} {useLoginByCellphone && loginTab === 'cellphone' && ( {t('MOBILE_CHECKOUT_PHONE', 'Phone')} setPhoneInputData(val)} textInputProps={{ returnKeyType: 'next', onSubmitEditing: () => inputRef?.current?.focus?.(), }} /> )} {t('PASSWORD', 'Password')} {onNavigationRedirect && forgotButtonText && ( onNavigationRedirect('Forgot')}> {forgotButtonText} )} ( setPasswordSee(!passwordSee)} /> : setPasswordSee(!passwordSee)} /> } value={value} forwardRef={inputRef} onChange={(val: any) => onChange(val)} returnKeyType='done' onSubmitEditing={handleSubmit(onSubmit)} blurOnSubmit /> )} name="password" rules={{ required: t('VALIDATION_ERROR_PASSWORD_REQUIRED', 'The field Password is required').replace('_attribute_', t('PASSWORD', 'Password')) }} defaultValue="" /> {onNavigationRedirect && registerButtonText && ( {t('NEW_ON_PLATFORM', 'New on Ordering?')} onNavigationRedirect('Signup')}> {registerButtonText} )} )} {useLoginByCellphone && loginTab === 'cellphone' && configs && Object.keys(configs).length > 0 && (configs?.twilio_service_enabled?.value === 'true' || configs?.twilio_service_enabled?.value === '1') && configs?.twilio_module?.value && ( <> {t('OR', 'Or')} ) } {configs && Object.keys(configs).length > 0 && ( (configs?.facebook_login?.value === 'true' || configs?.facebook_login?.value === '1') && configs?.facebook_id?.value && ( showToast(ToastType.Error, err)} handleLoading={(val: boolean) => setIsFBLoading(val)} handleSuccessFacebookLogin={handleSuccessFacebook} /> ) )} setIsModalVisible(false)} entireModal title={t('VERIFY_PHONE', 'Verify Phone')} > setIsModalVisible(false)} /> ); }; export const LoginForm = (props: any) => { const loginProps = { ...props, UIComponent: LoginFormUI, handleSuccessLogin: () => _removeStoreData('isGuestUser') }; return ; };