import { useGoogleLogin } from '@react-oauth/google' import { emailRegex } from '@/utils/formRegexs' import { useDispatch, useSelector } from 'react-redux' import { AppDispatch, RootState } from '@/rtk/store' import { loginAsync, loginERPAsync, loginWithGoogleAsync, } from '@/rtk/slices/authSlice' import { Box, Button, PasswordInput, TextInput } from '@mantine/core' import { useForm } from '@mantine/form' import classes from './AuthenticationImage.module.css' import { useNavigate } from 'react-router-dom' import ImgERP from '../../lib/images/erp.jpg' import { useState } from 'react' type TLogin = { email: string password: string } const Login = () => { const navigate = useNavigate() const dispatch = useDispatch() const { status } = useSelector((state: RootState) => state.auth) const [isLoginERP, setIsLoginERP] = useState(false) const formLogin = useForm({ initialValues: { email: '', password: '', }, validate: (values) => ({ email: values.email === '' ? 'Email is required' : isLoginERP ? null : emailRegex.test(values.email) ? null : 'Invalid email', password: values.password === '' ? 'Password is required' : null, }), }) const handleLogin = async (values: TLogin) => { if (isLoginERP) { const payload = { userEmail: values.email, password: values.password, } const resultAction = await dispatch(loginERPAsync(payload)) if (loginERPAsync.fulfilled.match(resultAction)) { // set interval to wait for localStorage to be set window.location.href = '/dashboard' } } else { const resultAction = await dispatch(loginAsync(values)) if (loginAsync.fulfilled.match(resultAction)) { navigate('/dashboard') } } } const handleLoginGG = useGoogleLogin({ onSuccess: async (codeResponse) => { const accessToken = codeResponse.access_token const resultAction = await dispatch(loginWithGoogleAsync(accessToken)) if (loginWithGoogleAsync.fulfilled.match(resultAction)) { navigate('/dashboard') } }, onError: (error) => console.log('Login Failed:', error), }) return (

{isLoginERP ? 'Login with ERP account' : 'Login with ATC account'}

{ formLogin.setFieldValue('email', e.target.value!) }} required size="md" /> { formLogin.setFieldValue('password', e.target.value!) }} required mt="md" size="md" /> {!isLoginERP ? ( ) : ( )} ) } export default Login