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

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.9 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";
5import Logo from "../../components/Logo/Logo";
6import netconfig from "../../scripts/net/netconfig.js";
7
8export default function Signup() {
9 const [name, setName] = useState("");
10 const [email, setEmail] = useState("");
11 const [password, setPassword] = useState("");
12 const [confirmPassword, setConfirmPassword] = useState("");
13 const [message, setMessage] = useState("");
14 const [messageType, setMessageType] = useState("");
15 const navigate = useNavigate();
16
17 const handleSubmit = async (e) => {
18 e.preventDefault();
19
20 if (password !== confirmPassword) {
21 setMessageType("error");
22 setMessage("Passwords do not match.");
23 return;
24 }
25
26 const payload = {
27 username: name,
28 email: email,
29 password: password,
30 };
31
32 try {
33 const response = await fetch(netconfig.auth.register, {
34 method: "POST",
35 headers: {
36 "Content-Type": "application/json",
37 },
38 body: JSON.stringify(payload),
39 });
40
41 if (response.ok) {
42 setMessageType("success");
43 setMessage("User registered successfully!");
44
45 setTimeout(() => {
46 navigate("/login");
47 }, 1000);
48 } else if (response.status === 409) {
49 setMessageType("error");
50 setMessage("Email is already taken.");
51 } else {
52 setMessageType("error");
53 setMessage("Registration failed.");
54 }
55 } catch (error) {
56 console.error("Error:", error);
57 setMessageType("error");
58 setMessage("Error registering user.");
59 }
60 };
61
62 return (
63 <div className={styles.wrapper}>
64 <Logo></Logo>
65 <div className={styles.illustration}>
66 <img src={illustration} alt="illustration" />
67 </div>
68 <div className={styles.form}>
69 <div className={styles.heading}>CREATE AN ACCOUNT</div>
70 <form onSubmit={handleSubmit}>
71 <div>
72 <label htmlFor="name">Username</label>
73 <input
74 type="text"
75 id="name"
76 value={name}
77 onChange={(e) => setName(e.target.value)}
78 placeholder="Enter your username"
79 required
80 />
81 </div>
82 <div>
83 <label htmlFor="email">E-Mail</label>
84 <input
85 type="email"
86 id="email"
87 value={email}
88 onChange={(e) => setEmail(e.target.value)}
89 placeholder="Enter your email"
90 required
91 />
92 </div>
93 <div>
94 <label htmlFor="password">Password</label>
95 <input
96 type="password"
97 id="password"
98 value={password}
99 onChange={(e) => setPassword(e.target.value)}
100 placeholder="Enter your password"
101 required
102 />
103 </div>
104 <div>
105 <label htmlFor="confirmPassword">Confirm Password</label>
106 <input
107 type="password"
108 id="confirmPassword"
109 value={confirmPassword}
110 onChange={(e) => setConfirmPassword(e.target.value)}
111 placeholder="Confirm your password"
112 required
113 />
114 </div>
115 <button type="submit">Submit</button>
116 {message && (
117 <p className={messageType === "success" ? styles.successMessage : styles.errorMessage}>
118 {message}
119 </p>
120 )}
121 </form>
122 <p>
123 Have an account? <Link to="/Login"> Login </Link>
124 </p>
125 </div>
126 </div>
127 );
128}
Note: See TracBrowser for help on using the repository browser.