| 1 | import {useState} from "react";
|
|---|
| 2 | import LockOutlinedIcon from "@mui/icons-material/LockOutlined";
|
|---|
| 3 | import {toast} from "react-toastify";
|
|---|
| 4 | import {RegisterUser, UserRole} from "../../services/user-service";
|
|---|
| 5 | import {Link, redirect, useNavigate} from "react-router-dom";
|
|---|
| 6 | import PersonIcon from '@mui/icons-material/Person';
|
|---|
| 7 | import {Avatar, Button, Container, Grid, Paper, TextField, Typography} from "@mui/material";
|
|---|
| 8 | import ChooseRole from "../../components/modals/role-modal";
|
|---|
| 9 |
|
|---|
| 10 | const Register = (props) => {
|
|---|
| 11 | const [showRoleModal, setShowRoleModal] = useState(true);
|
|---|
| 12 | const [user, setUser] = useState({
|
|---|
| 13 | username: "",
|
|---|
| 14 | password: "",
|
|---|
| 15 | email: "",
|
|---|
| 16 | address: "",
|
|---|
| 17 | phoneNumber: "",
|
|---|
| 18 | role: props?.location?.state?.role
|
|---|
| 19 | });
|
|---|
| 20 | const navigate = useNavigate();
|
|---|
| 21 | const chooseRole = role => {
|
|---|
| 22 | setUser({...user, role: role});
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | const handleChange = name => event => {
|
|---|
| 26 | setUser({...user, [name]: event.target.value});
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | const handleSubmit = event => {
|
|---|
| 30 | event.preventDefault();
|
|---|
| 31 |
|
|---|
| 32 | RegisterUser(user)
|
|---|
| 33 | .then(() => {
|
|---|
| 34 | navigate("/login");
|
|---|
| 35 | })
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | return (showRoleModal && (user.role === null || user.role === undefined) ?
|
|---|
| 40 | <ChooseRole open={showRoleModal} onClose={() => setShowRoleModal(false)} roleset={chooseRole}/>
|
|---|
| 41 | : <Container component="main" maxWidth="xs" className="register-container-custom">
|
|---|
| 42 | <Paper elevation={0} className={"d-flex flex-column align-items-center pt-5 register-paper-custom"}>
|
|---|
| 43 | <Avatar>
|
|---|
| 44 | <PersonIcon/>
|
|---|
| 45 | </Avatar>
|
|---|
| 46 | <Typography component="h1" variant="h5" className={"mb-3"}>
|
|---|
| 47 | Sign up
|
|---|
| 48 | </Typography>
|
|---|
| 49 | <form onSubmit={handleSubmit}>
|
|---|
| 50 | <Grid container spacing={2}>
|
|---|
| 51 | <Grid item xs={12}>
|
|---|
| 52 | <TextField
|
|---|
| 53 | onChange={handleChange("email")}
|
|---|
| 54 | variant="outlined"
|
|---|
| 55 | required
|
|---|
| 56 | fullWidth
|
|---|
| 57 | id="email"
|
|---|
| 58 | label="Email Address"
|
|---|
| 59 | name="email"
|
|---|
| 60 | />
|
|---|
| 61 | </Grid>
|
|---|
| 62 | <Grid item xs={12}>
|
|---|
| 63 | <TextField
|
|---|
| 64 | onChange={handleChange("username")}
|
|---|
| 65 | variant="outlined"
|
|---|
| 66 | required
|
|---|
| 67 | fullWidth
|
|---|
| 68 | id="username"
|
|---|
| 69 | label="Username"
|
|---|
| 70 | name="username"
|
|---|
| 71 | inputProps={{
|
|---|
| 72 | minLength: 2,
|
|---|
| 73 | }}
|
|---|
| 74 | />
|
|---|
| 75 | </Grid>
|
|---|
| 76 | <Grid item xs={12}>
|
|---|
| 77 | <TextField
|
|---|
| 78 | onChange={handleChange("password")}
|
|---|
| 79 | variant="outlined"
|
|---|
| 80 | required
|
|---|
| 81 | fullWidth
|
|---|
| 82 | name="password"
|
|---|
| 83 | label="Password"
|
|---|
| 84 | type="password"
|
|---|
| 85 | id="password"
|
|---|
| 86 | inputProps={{
|
|---|
| 87 | minLength: 6,
|
|---|
| 88 | }}
|
|---|
| 89 | />
|
|---|
| 90 | </Grid>
|
|---|
| 91 | {user.role === UserRole.Potrosuvac &&
|
|---|
| 92 | <>
|
|---|
| 93 | <Grid item xs={12}>
|
|---|
| 94 | <TextField
|
|---|
| 95 | onChange={handleChange("address")}
|
|---|
| 96 | variant="outlined"
|
|---|
| 97 | required
|
|---|
| 98 | fullWidth
|
|---|
| 99 | name="address"
|
|---|
| 100 | label="Address"
|
|---|
| 101 | type="text"
|
|---|
| 102 | id="address"
|
|---|
| 103 | inputProps={{
|
|---|
| 104 | minLength: 6,
|
|---|
| 105 | }}
|
|---|
| 106 | />
|
|---|
| 107 | </Grid>
|
|---|
| 108 |
|
|---|
| 109 | <Grid item xs={12}>
|
|---|
| 110 | <TextField
|
|---|
| 111 | onChange={handleChange("phoneNumber")}
|
|---|
| 112 | variant="outlined"
|
|---|
| 113 | required
|
|---|
| 114 | fullWidth
|
|---|
| 115 | name="phoneNumber"
|
|---|
| 116 | label="Phone Number"
|
|---|
| 117 | type="text"
|
|---|
| 118 | id="phoneNumber"
|
|---|
| 119 | inputProps={{
|
|---|
| 120 | minLength: 6,
|
|---|
| 121 | }}
|
|---|
| 122 | />
|
|---|
| 123 | </Grid>
|
|---|
| 124 | </>
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | </Grid>
|
|---|
| 128 | <br/>
|
|---|
| 129 |
|
|---|
| 130 | <Button
|
|---|
| 131 | type="submit"
|
|---|
| 132 | fullWidth
|
|---|
| 133 | variant="contained"
|
|---|
| 134 | >
|
|---|
| 135 | Sign Up
|
|---|
| 136 | </Button>
|
|---|
| 137 | <Grid container justify="flex-end" className={"mt-3"}>
|
|---|
| 138 | <Grid item>
|
|---|
| 139 | <Link to={"/login"}>
|
|---|
| 140 | Already have an account? Sign in
|
|---|
| 141 | </Link>
|
|---|
| 142 | </Grid>
|
|---|
| 143 | </Grid>
|
|---|
| 144 | </form>
|
|---|
| 145 | </Paper>
|
|---|
| 146 | </Container>
|
|---|
| 147 | )
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | export default Register; |
|---|