Files
parcer/src/pages/Register.tsx

166 lines
4.4 KiB
TypeScript
Raw Normal View History

import axios from "axios";
2024-11-16 23:21:59 +05:30
import { useState, useContext, useEffect } from "react";
2024-11-16 21:56:03 +05:30
import { useNavigate, Link } from "react-router-dom";
import { AuthContext } from "../context/auth";
2024-11-16 23:21:59 +05:30
import { Box, Typography, TextField, Button, CircularProgress } from "@mui/material";
2024-09-25 13:08:51 +05:30
import { useGlobalInfoStore } from "../context/globalInfo";
2024-11-01 08:26:13 +05:30
import { apiUrl } from "../apiConfig";
2024-12-08 05:41:11 +05:30
import { useThemeMode } from "../context/theme-provider";
2024-12-10 20:40:59 +05:30
import { useTranslation } from 'react-i18next';
import i18n from '../i18n';
2024-09-25 13:08:51 +05:30
const Register = () => {
2025-07-12 01:29:54 +05:30
const { t } = useTranslation();
const [form, setForm] = useState({
email: "",
password: "",
});
const [loading, setLoading] = useState(false);
const { notify } = useGlobalInfoStore();
2024-11-16 23:21:59 +05:30
const { email, password } = form;
2024-09-25 13:08:51 +05:30
const { state, dispatch } = useContext(AuthContext);
const { user } = state;
2024-12-08 05:41:11 +05:30
const { darkMode } = useThemeMode();
2024-11-16 21:56:03 +05:30
const navigate = useNavigate();
2024-09-25 13:08:51 +05:30
useEffect(() => {
2024-11-16 21:56:03 +05:30
if (user) {
navigate("/");
}
}, [user, navigate]);
2024-09-25 13:08:51 +05:30
const handleChange = (e: any) => {
const { name, value } = e.target;
setForm({ ...form, [name]: value });
};
2024-09-25 13:08:51 +05:30
const submitForm = async (e: any) => {
e.preventDefault();
2025-07-12 01:29:54 +05:30
2025-07-12 01:32:31 +05:30
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
notify("error", "Invalid email format");
return;
}
2025-07-12 01:29:54 +05:30
setLoading(true);
try {
2024-12-08 05:41:11 +05:30
const { data } = await axios.post(`${apiUrl}/auth/register`, { email, password });
2024-11-16 21:56:03 +05:30
dispatch({ type: "LOGIN", payload: data });
2024-12-10 20:40:59 +05:30
notify("success", t('register.welcome_notification'));
window.localStorage.setItem("user", JSON.stringify(data));
navigate("/");
2025-07-12 01:29:54 +05:30
} catch (error: any) {
const errorResponse = error.response?.data;
2025-07-12 01:29:54 +05:30
const errorMessage = errorResponse?.code
? t(errorResponse.code)
: t('register.error.generic');
notify("error", errorMessage);
setLoading(false);
}
};
2024-09-25 13:08:51 +05:30
return (
2024-11-16 23:21:59 +05:30
<Box
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
maxHeight: "100vh",
padding: 4,
2024-12-08 05:41:11 +05:30
backgroundColor: darkMode ? "#121212" : "#ffffff",
2024-11-16 23:21:59 +05:30
}}
>
<Box
component="form"
onSubmit={submitForm}
sx={{
2024-12-08 05:41:11 +05:30
textAlign: "center",
2025-10-13 17:28:32 +05:30
backgroundColor: darkMode ? "#121111ff" : "#ffffff",
2024-12-08 05:41:11 +05:30
color: darkMode ? "#ffffff" : "#333333",
padding: 6,
borderRadius: 5,
2025-07-12 01:29:54 +05:30
boxShadow:
"0px 20px 40px rgba(0, 0, 0, 0.2), 0px -5px 10px rgba(0, 0, 0, 0.15)",
2024-12-08 05:41:11 +05:30
display: "flex",
flexDirection: "column",
alignItems: "center",
2025-01-08 22:37:02 +05:30
maxWidth: 500,
2024-12-08 05:41:11 +05:30
width: "100%",
}}
>
<img
src="../src/assets/maxunlogo.png"
alt="logo"
height={55}
width={60}
style={{
marginBottom: 20,
borderRadius: "20%",
2024-11-19 03:21:09 +05:30
alignItems: "center",
}}
2024-12-08 05:41:11 +05:30
/>
2024-11-16 23:21:59 +05:30
<Typography variant="h4" gutterBottom>
2024-12-10 21:45:09 +05:30
{t('register.title')}
2024-11-16 23:21:59 +05:30
</Typography>
<TextField
fullWidth
2024-12-10 20:40:59 +05:30
label={t('register.email')}
2024-11-16 23:21:59 +05:30
name="email"
value={email}
onChange={handleChange}
margin="normal"
variant="outlined"
required
/>
<TextField
fullWidth
2024-12-10 20:40:59 +05:30
label={t('register.password')}
2024-11-16 23:21:59 +05:30
name="password"
type="password"
value={password}
onChange={handleChange}
margin="normal"
variant="outlined"
required
/>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
2024-12-08 05:41:11 +05:30
sx={{
mt: 2,
mb: 2,
}}
2024-11-16 23:21:59 +05:30
disabled={loading || !email || !password}
2024-11-16 21:56:03 +05:30
>
2024-11-16 23:21:59 +05:30
{loading ? (
<>
2025-01-08 22:37:02 +05:30
<CircularProgress size={20} sx={{ mr: 2 }} />
2024-11-16 23:21:59 +05:30
Loading
</>
) : (
2024-12-10 20:40:59 +05:30
t('register.button')
2024-11-16 23:21:59 +05:30
)}
</Button>
2025-07-12 01:29:54 +05:30
<Typography
variant="body2"
align="center"
sx={{ color: darkMode ? "#ffffff" : "#333333" }}
>
2024-12-10 20:40:59 +05:30
{t('register.register_prompt')}{" "}
2025-07-12 01:29:54 +05:30
<Link to="/login" style={{ textDecoration: "none", color: "#ff33cc" }}>
2024-12-10 20:40:59 +05:30
{t('register.login_link')}
2024-11-16 23:21:59 +05:30
</Link>
</Typography>
</Box>
</Box>
);
2024-09-25 13:08:51 +05:30
};
2025-07-12 01:29:54 +05:30
export default Register;