import React, { FC, useRef, useState } from 'react' import { Keyboard } from 'react-native' import { LoginCore, LoginCoreHandle } from '../../core' import { IUserProfilePublic } from '../../types' import LoginView from './LoginView' import { ILoginFields, ILoginProps, LoginContentType } from './types' const Login: FC = ({ onLoginSuccessful, onLoginError, isLoginDialogVisible, showLoginDialog, hideLoginDialog, userFirstName, onLogoutSuccess, onLogoutError, texts, styles, refs, brandImages, isShowPasswordButtonVisible, onRestorePasswordError, onRestorePasswordSuccess, }) => { //#region State const [isLoading, setIsLoading] = useState(false) const [loginApiError, setLoginApiError] = useState('') const [restorePasswordApiError, setRestorePasswordApiError] = useState('') const [restorePasswordSuccessMessage, setRestorePasswordSuccessMessage] = useState('') const [userProfileData, setUserProfileData] = useState() const [loginContentType, setLoginContentType] = useState('login') const [isRestorePasswordLoading, setIsRestorePasswordLoading] = useState(false) //#endregion State const loginCoreRef = useRef(null) //#region Handlers const handleOnPressForgotPassword = () => setLoginContentType('restorePassword') const handleOnPressLogin = async (fields: ILoginFields) => { if (loginApiError) { setLoginApiError('') } if (!loginCoreRef.current?.login) { return onLoginError?.({ message: 'LoginCoreRef is not initialized', }) } setIsLoading(true) const { userProfile, error: authorizationError, accessTokenData, } = await loginCoreRef.current.login(fields) if (authorizationError || !userProfile) { setIsLoading(false) setLoginApiError(authorizationError?.message || 'Auth error') return onLoginError?.(authorizationError!) } Keyboard.dismiss() setUserProfileData(userProfile) onLoginSuccessful({ userProfile, accessTokenData, }) setIsLoading(false) hideLoginDialog() } const handleOnPressLogout = async () => { if (!loginCoreRef.current?.logout) { return onLogoutError?.({ message: 'LoginCoreRef is not initialized' }) } await loginCoreRef.current.logout() setUserProfileData(undefined) onLogoutSuccess?.() } const handleOnPressCancelRestorePasswordButton = () => setLoginContentType('login') const handleOnPressRestorePasswordButton = async (email: string) => { if (!loginCoreRef.current?.restorePassword) { return onRestorePasswordError?.({ message: 'LoginCoreRef is not initialized', }) } setIsRestorePasswordLoading(true) const { data: restorePasswordSuccessData, error: restorePassError } = await loginCoreRef.current.restorePassword(email) setIsRestorePasswordLoading(false) if (restorePassError || !restorePasswordSuccessData) { setRestorePasswordApiError( restorePassError?.message || 'Restore password unknown error' ) setIsRestorePasswordLoading(false) onRestorePasswordError?.(restorePassError!) return } setLoginContentType('restorePasswordSuccess') setRestorePasswordSuccessMessage(restorePasswordSuccessData.message) onRestorePasswordSuccess?.() } const handleOnPressRestorePasswordSuccessButton = () => { handleHideDialog() } const handleHideDialog = () => { setLoginContentType('login') hideLoginDialog() } //#endregion //#region Render Main Content return ( ) //#endregion Render Main Content } export default Login