chore: lint
This commit is contained in:
@@ -2,124 +2,124 @@ import axios from 'axios';
|
|||||||
import { useState, useContext, useEffect } from 'react';
|
import { useState, useContext, useEffect } from 'react';
|
||||||
import { useNavigate, Link } from 'react-router-dom';
|
import { useNavigate, Link } from 'react-router-dom';
|
||||||
import { AuthContext } from '../context/auth';
|
import { AuthContext } from '../context/auth';
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
Typography,
|
Typography,
|
||||||
TextField,
|
TextField,
|
||||||
Button,
|
Button,
|
||||||
CircularProgress,
|
CircularProgress,
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
import { useGlobalInfoStore } from "../context/globalInfo";
|
import { useGlobalInfoStore } from "../context/globalInfo";
|
||||||
|
|
||||||
const SignIn = () => {
|
const SignIn = () => {
|
||||||
const [form, setForm] = useState({
|
const [form, setForm] = useState({
|
||||||
email: '',
|
email: '',
|
||||||
password: '',
|
password: '',
|
||||||
});
|
});
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const { notify } = useGlobalInfoStore();
|
const { notify } = useGlobalInfoStore();
|
||||||
const { email, password } = form;
|
const { email, password } = form;
|
||||||
|
|
||||||
const { state, dispatch } = useContext(AuthContext);
|
const { state, dispatch } = useContext(AuthContext);
|
||||||
const { user } = state;
|
const { user } = state;
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user) {
|
if (user) {
|
||||||
navigate('/');
|
navigate('/');
|
||||||
}
|
}
|
||||||
}, [user, navigate]);
|
}, [user, navigate]);
|
||||||
|
|
||||||
const handleChange = (e: any) => {
|
const handleChange = (e: any) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setForm({ ...form, [name]: value });
|
setForm({ ...form, [name]: value });
|
||||||
};
|
};
|
||||||
|
|
||||||
const submitForm = async (e: any) => {
|
const submitForm = async (e: any) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.post(`/api/login`, { email, password });
|
const { data } = await axios.post(`/api/login`, { email, password });
|
||||||
dispatch({ type: 'LOGIN', payload: data });
|
dispatch({ type: 'LOGIN', payload: data });
|
||||||
notify('success', 'Welcome to Maxun!');
|
notify('success', 'Welcome to Maxun!');
|
||||||
window.localStorage.setItem('user', JSON.stringify(data));
|
window.localStorage.setItem('user', JSON.stringify(data));
|
||||||
navigate('/');
|
navigate('/');
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
notify('error', err.response.data || 'Login Failed. Please try again.');
|
notify('error', err.response.data || 'Login Failed. Please try again.');
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
flexDirection: 'column',
|
flexDirection: 'column',
|
||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
mt: 5,
|
mt: 5,
|
||||||
}}
|
}}
|
||||||
>
|
|
||||||
<Typography variant="h4" gutterBottom>
|
|
||||||
Login
|
|
||||||
</Typography>
|
|
||||||
|
|
||||||
<Typography variant="body1" align="center" gutterBottom>
|
|
||||||
OR
|
|
||||||
</Typography>
|
|
||||||
|
|
||||||
{/* Login Form */}
|
|
||||||
<Box component="form" onSubmit={submitForm} sx={{ maxWidth: 400, width: '100%' }}>
|
|
||||||
<TextField
|
|
||||||
fullWidth
|
|
||||||
label="Email"
|
|
||||||
name="email"
|
|
||||||
value={email}
|
|
||||||
onChange={handleChange}
|
|
||||||
margin="normal"
|
|
||||||
variant="outlined"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
<TextField
|
|
||||||
fullWidth
|
|
||||||
label="Password"
|
|
||||||
name="password"
|
|
||||||
type="password"
|
|
||||||
value={password}
|
|
||||||
onChange={handleChange}
|
|
||||||
margin="normal"
|
|
||||||
variant="outlined"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
fullWidth
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
sx={{ mt: 2, mb: 2 }}
|
|
||||||
disabled={loading || !email || !password}
|
|
||||||
>
|
>
|
||||||
{loading ? (
|
<Typography variant="h4" gutterBottom>
|
||||||
<>
|
Login
|
||||||
<CircularProgress size={20} sx={{ mr: 2 }} />
|
</Typography>
|
||||||
Loading
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
'Login'
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
{/* Redirect to Register */}
|
<Typography variant="body1" align="center" gutterBottom>
|
||||||
<Typography variant="body2" align="center">
|
OR
|
||||||
Don’t have an account?{' '}
|
</Typography>
|
||||||
<Link to="/register" style={{ textDecoration: 'none', color: '#1976d2' }}>
|
|
||||||
Register
|
{/* Login Form */}
|
||||||
</Link>
|
<Box component="form" onSubmit={submitForm} sx={{ maxWidth: 400, width: '100%' }}>
|
||||||
</Typography>
|
<TextField
|
||||||
</Box>
|
fullWidth
|
||||||
</Box>
|
label="Email"
|
||||||
);
|
name="email"
|
||||||
|
value={email}
|
||||||
|
onChange={handleChange}
|
||||||
|
margin="normal"
|
||||||
|
variant="outlined"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label="Password"
|
||||||
|
name="password"
|
||||||
|
type="password"
|
||||||
|
value={password}
|
||||||
|
onChange={handleChange}
|
||||||
|
margin="normal"
|
||||||
|
variant="outlined"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
fullWidth
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
sx={{ mt: 2, mb: 2 }}
|
||||||
|
disabled={loading || !email || !password}
|
||||||
|
>
|
||||||
|
{loading ? (
|
||||||
|
<>
|
||||||
|
<CircularProgress size={20} sx={{ mr: 2 }} />
|
||||||
|
Loading
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
'Login'
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
{/* Redirect to Register */}
|
||||||
|
<Typography variant="body2" align="center">
|
||||||
|
Don’t have an account?{' '}
|
||||||
|
<Link to="/register" style={{ textDecoration: 'none', color: '#1976d2' }}>
|
||||||
|
Register
|
||||||
|
</Link>
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SignIn;
|
export default SignIn;
|
||||||
|
|||||||
Reference in New Issue
Block a user