source: imaps-frontend/src/pages/Signup/Signup.jsx@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.3 KB
Line 
1import React, { useState } from "react";
2import { Link, useNavigate } from "react-router-dom";
3import illustration from "../../assets/illustration_img.png";
4import styles from "./Signup.module.css";
5
6export default function Signup() {
7 const [name, setName] = useState("");
8 const [email, setEmail] = useState("");
9 const [password, setPassword] = useState("");
10 const [message, setMessage] = useState("");
11 const [messageType, setMessageType] = useState(""); // New state to manage message type
12 const navigate = useNavigate();
13
14 const handleSubmit = async (e) => {
15 e.preventDefault();
16
17 const payload = {
18 username: name,
19 email: email,
20 password: password,
21 };
22
23 try {
24 const response = await fetch("http://localhost:8080/api/auth/register", {
25 method: "POST",
26 headers: {
27 "Content-Type": "application/json",
28 },
29 body: JSON.stringify(payload),
30 });
31
32 if (response.ok) {
33 setMessageType("success");
34 setMessage("User registered successfully!");
35
36 // Wait 3 seconds and then redirect to login page
37 setTimeout(() => {
38 navigate("/login");
39 }, 3000);
40 } else if (response.status === 409) {
41 setMessageType("error");
42 setMessage("Email is already taken.");
43 } else {
44 setMessageType("error");
45 setMessage("Registration failed.");
46 }
47 } catch (error) {
48 console.error("Error:", error);
49 setMessageType("error");
50 setMessage("Error registering user.");
51 }
52 };
53
54 return (
55 <div className={styles.wrapper}>
56 <div className={styles.illustration}>
57 <img src={illustration} alt="illustration" />
58 </div>
59 <div className={styles.form}>
60 <div className={styles.heading}>CREATE AN ACCOUNT</div>
61 <form onSubmit={handleSubmit}>
62 <div>
63 <label htmlFor="name">Username</label>
64 <input
65 type="text"
66 id="name"
67 value={name}
68 onChange={(e) => setName(e.target.value)}
69 placeholder="Enter your username"
70 required
71 />
72 </div>
73 <div>
74 <label htmlFor="email">E-Mail</label>
75 <input
76 type="email"
77 id="email"
78 value={email}
79 onChange={(e) => setEmail(e.target.value)}
80 placeholder="Enter your email"
81 required
82 />
83 </div>
84 <div>
85 <label htmlFor="password">Password</label>
86 <input
87 type="password"
88 id="password"
89 value={password}
90 onChange={(e) => setPassword(e.target.value)}
91 placeholder="Enter your password"
92 required
93 />
94 </div>
95 <button type="submit">Submit</button>
96
97 {/* Display message with appropriate styling */}
98 {message && (
99 <p className={messageType === "success" ? styles.successMessage : styles.errorMessage}>
100 {message}
101 </p>
102 )}
103 <h2 align="center" className={styles.or}>
104 OR
105 </h2>
106 </form>
107 <p>
108 Have an account? <Link to="/Login"> Login </Link>
109 </p>
110 </div>
111 </div>
112 );
113}
Note: See TracBrowser for help on using the repository browser.