update login dan password
This commit is contained in:
@@ -1,42 +1,52 @@
|
||||
/* ----------------------------------- yup ---------------------------------- */
|
||||
import * as Yup from 'yup';
|
||||
import React, { useContext, useRef, useState, useEffect } from 'react';
|
||||
/* ---------------------------------- form ---------------------------------- */
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { yupResolver } from '@hookform/resolvers/yup';
|
||||
/* ---------------------------------- @mui ---------------------------------- */
|
||||
import { Stack, Alert } from '@mui/material';
|
||||
import { LoadingButton } from '@mui/lab';
|
||||
import { Link, Stack, Alert, IconButton, InputAdornment } from '@mui/material';
|
||||
/* ---------------------------------- hooks --------------------------------- */
|
||||
import useAuth from '../../../hooks/useAuth';
|
||||
import useIsMountedRef from '../../../hooks/useIsMountedRef';
|
||||
/* ------------------------------- components ------------------------------- */
|
||||
import { FormProvider, RHFTextField } from '../../../components/hook-form';
|
||||
import Iconify from '../../../components/Iconify';
|
||||
import { FormProvider, RHFTextField, RHFCheckbox } from '../../../components/hook-form';
|
||||
import { enqueueSnackbar } from 'notistack';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
/* ---------------------------------- types --------------------------------- */
|
||||
|
||||
type LoginFormProps = {
|
||||
setEmailOrPhone: Function;
|
||||
setLoginOrVerifyCode: Function;
|
||||
};
|
||||
// type LoginFormProps = {
|
||||
// setEmailOrPhone: Function;
|
||||
// setLoginOrVerifyCode: Function;
|
||||
// };
|
||||
|
||||
type FormValuesProps = {
|
||||
email: string;
|
||||
password: string;
|
||||
remember: boolean;
|
||||
afterSubmit?: string;
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
export default function LoginForm({ setEmailOrPhone, setLoginOrVerifyCode }: LoginFormProps) {
|
||||
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().email('Email must be a valid email address').required('Email is required'),
|
||||
email: Yup.string().email('Format email tidak valid').required('Email harus diisi'),
|
||||
password: Yup.string().required('Password harus diisi'),
|
||||
});
|
||||
|
||||
const defaultValues = {
|
||||
const defaultValues = {
|
||||
email: '',
|
||||
password: '',
|
||||
remember: true,
|
||||
};
|
||||
|
||||
const methods = useForm<FormValuesProps>({
|
||||
@@ -51,17 +61,34 @@ export default function LoginForm({ setEmailOrPhone, setLoginOrVerifyCode }: Log
|
||||
formState: { errors, isSubmitting },
|
||||
} = methods;
|
||||
|
||||
// const onSubmit = async (data: FormValuesProps) => {
|
||||
// try {
|
||||
// const loginResult = await login(data.email, data.password, data.remember);
|
||||
// // setEmailOrPhone(data.email);
|
||||
// // setLoginOrVerifyCode(true);
|
||||
// // reset();
|
||||
// console.log('test');
|
||||
// navigate('/dashboard');
|
||||
// // enqueueSnackbar('Kode OTP telah dikirim, silahkan cek email dan spam folder', {
|
||||
// // variant: 'success',
|
||||
// // autoHideDuration: 5000,
|
||||
// // });
|
||||
// } catch (error: any) {
|
||||
// reset();
|
||||
// console.log(error, 'test');
|
||||
|
||||
// if (isMountedRef.current) {
|
||||
// setError('afterSubmit', { ...error, message: error.data.message });
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
|
||||
const onSubmit = async (data: FormValuesProps) => {
|
||||
try {
|
||||
await login(data.email);
|
||||
setEmailOrPhone(data.email);
|
||||
setLoginOrVerifyCode(true);
|
||||
reset();
|
||||
enqueueSnackbar('Kode OTP telah dikirim, silahkan cek email dan spam folder', {
|
||||
variant: 'success',
|
||||
autoHideDuration: 5000,
|
||||
});
|
||||
} catch (error: any) {
|
||||
const loginResult = await login(data.email, data.password, data.remember);
|
||||
navigate('/dashboard');
|
||||
} catch (error) {
|
||||
|
||||
reset();
|
||||
|
||||
if (isMountedRef.current) {
|
||||
@@ -73,10 +100,25 @@ export default function LoginForm({ setEmailOrPhone, setLoginOrVerifyCode }: Log
|
||||
return (
|
||||
<FormProvider methods={methods} onSubmit={handleSubmit(onSubmit)}>
|
||||
<Stack spacing={3}>
|
||||
<Alert severity="info">Masukkan akun yang telah terdaftar</Alert>
|
||||
<Alert severity="info">Masukan Email atau Username dan Password</Alert>
|
||||
{!!errors.afterSubmit && <Alert severity="error">{errors.afterSubmit.message}</Alert>}
|
||||
|
||||
<RHFTextField name="email" label="Email address" />
|
||||
<RHFTextField
|
||||
name="password"
|
||||
label="Password"
|
||||
type={showPassword ? 'text' : 'password'}
|
||||
InputProps={{
|
||||
endAdornment: (
|
||||
<InputAdornment position="end">
|
||||
<IconButton onClick={() => setShowPassword(!showPassword)} edge="end">
|
||||
<Iconify icon={showPassword ? 'eva:eye-fill' : 'eva:eye-off-fill'} />
|
||||
</IconButton>
|
||||
</InputAdornment>
|
||||
),
|
||||
}}
|
||||
required
|
||||
/>
|
||||
</Stack>
|
||||
|
||||
<LoadingButton
|
||||
|
||||
Reference in New Issue
Block a user