| 1 | import React, {useState} from "react";
|
|---|
| 2 | import {Container, Box, Typography, TextField, Button, Paper, Alert, AlertColor} from "@mui/material";
|
|---|
| 3 | import {registerNewUser} from "./register.telefunc";
|
|---|
| 4 |
|
|---|
| 5 | export default function RegisterPage() {
|
|---|
| 6 | const [username, setUsername] = useState("");
|
|---|
| 7 | const [email, setEmail] = useState("");
|
|---|
| 8 | const [password, setPassword] = useState("");
|
|---|
| 9 | const [loading, setLoading] = useState(false);
|
|---|
| 10 | const [message, setMessage] = useState<string | null>(null);
|
|---|
| 11 | const [severity, setSeverity] = useState<AlertColor>("info");
|
|---|
| 12 |
|
|---|
| 13 | async function onSubmit(e: React.FormEvent) {
|
|---|
| 14 | e.preventDefault();
|
|---|
| 15 | setLoading(true);
|
|---|
| 16 | setMessage(null);
|
|---|
| 17 |
|
|---|
| 18 | try {
|
|---|
| 19 | const result = await registerNewUser({username, email, password});
|
|---|
| 20 | if (result?.success) {
|
|---|
| 21 | setSeverity("success");
|
|---|
| 22 | setMessage("Registration successful! Redirecting...");
|
|---|
| 23 | window.location.href = "/auth/login";
|
|---|
| 24 | } else {
|
|---|
| 25 | setSeverity("error");
|
|---|
| 26 | setMessage("Registration failed.");
|
|---|
| 27 | }
|
|---|
| 28 | } catch (err) {
|
|---|
| 29 | setSeverity("error");
|
|---|
| 30 | setMessage("An error occurred. Please try again.");
|
|---|
| 31 | } finally {
|
|---|
| 32 | setLoading(false);
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | return (
|
|---|
| 37 | <Container component="main" maxWidth="xs">
|
|---|
| 38 | <Box sx={{marginTop: 8, display: "flex", flexDirection: "column", alignItems: "center", color: "white"}}>
|
|---|
| 39 | <Paper elevation={3} sx={{p: 4, width: '100%'}}>
|
|---|
| 40 | <Typography component="h1" variant="h5" align="center">
|
|---|
| 41 | Register
|
|---|
| 42 | </Typography>
|
|---|
| 43 |
|
|---|
| 44 | <Box component="form" onSubmit={onSubmit} sx={{mt: 3}}>
|
|---|
| 45 | <TextField
|
|---|
| 46 | margin="normal"
|
|---|
| 47 | required
|
|---|
| 48 | fullWidth
|
|---|
| 49 | label="Username"
|
|---|
| 50 | value={username}
|
|---|
| 51 | onChange={(e) => setUsername(e.target.value)}
|
|---|
| 52 | />
|
|---|
| 53 | <TextField
|
|---|
| 54 | margin="normal"
|
|---|
| 55 | required
|
|---|
| 56 | fullWidth
|
|---|
| 57 | label="Email Address"
|
|---|
| 58 | type="email"
|
|---|
| 59 | value={email}
|
|---|
| 60 | onChange={(e) => setEmail(e.target.value)}
|
|---|
| 61 | />
|
|---|
| 62 | <TextField
|
|---|
| 63 | margin="normal"
|
|---|
| 64 | required
|
|---|
| 65 | fullWidth
|
|---|
| 66 | label="Password"
|
|---|
| 67 | type="password"
|
|---|
| 68 | value={password}
|
|---|
| 69 | onChange={(e) => setPassword(e.target.value)}
|
|---|
| 70 | />
|
|---|
| 71 |
|
|---|
| 72 | <Button
|
|---|
| 73 | type="submit"
|
|---|
| 74 | fullWidth
|
|---|
| 75 | variant="contained"
|
|---|
| 76 | disabled={loading}
|
|---|
| 77 | sx={{mt: 3, mb: 2, color: "white"}}
|
|---|
| 78 | >
|
|---|
| 79 | {loading ? "Registering..." : "Register"}
|
|---|
| 80 | </Button>
|
|---|
| 81 |
|
|---|
| 82 | {message && <Alert severity={severity}>{message}</Alert>}
|
|---|
| 83 | </Box>
|
|---|
| 84 | </Paper>
|
|---|
| 85 | </Box>
|
|---|
| 86 | </Container>
|
|---|
| 87 | );
|
|---|
| 88 | }
|
|---|