import * as Yup from 'yup'; import { useState } from 'react'; import { Link as RouterLink, useNavigate } from 'react-router-dom'; // form import { useForm } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; // @mui import { Link, Stack, Alert, IconButton, InputAdornment } from '@mui/material'; import { LoadingButton } from '@mui/lab'; // routes import { PATH_AUTH } from '../../../routes/paths'; // hooks import useAuth from '../../../hooks/useAuth'; import useIsMountedRef from '../../../hooks/useIsMountedRef'; // components import Iconify from '../../../components/Iconify'; import { FormProvider, RHFTextField, RHFCheckbox } from '../../../components/hook-form'; // ---------------------------------------------------------------------- type FormValuesProps = { email: string; password: string; remember: boolean; afterSubmit?: string; }; export default function LoginForm() { const { login } = useAuth(); const navigate = useNavigate(); const isMountedRef = useIsMountedRef(); const [showPassword, setShowPassword] = useState(false); const LoginSchema = Yup.object().shape({ email: Yup.string().required('Email is required'), password: Yup.string().required('Password is required'), }); const defaultValues = { email: '', password: '', remember: true, }; const methods = useForm({ resolver: yupResolver(LoginSchema), // defaultValues, }); const { reset, setError, handleSubmit, formState: { errors, isSubmitting }, } = methods; const onSubmit = async (data: FormValuesProps) => { try { const loginResult = await login(data.email, data.password); navigate('/dashboard'); } catch (error) { console.error(error); reset(); if (isMountedRef.current) { setError('afterSubmit', { ...error, message: error.data.message }); } } }; return ( Masukan Email atau Username dan Password {!!errors.afterSubmit && {errors.afterSubmit.message}} setShowPassword(!showPassword)} edge="end"> ), }} /> Forgot password? Login ); }