|
1 | 1 | import { useState, useEffect } from "react";
|
2 |
| -import {Button} from "react-bootstrap"; |
| 2 | +import ButtonSubmitDefault from "../../components/Buttons/ButtonSubmitDefault.jsx"; |
| 3 | +import {Create} from "../../axios/userAxios.js" |
| 4 | +import {useNavigate} from "react-router-dom"; |
3 | 5 |
|
4 | 6 | const Register = () => {
|
5 | 7 | const [firstName, setFirstName] = useState('');
|
6 | 8 | const [lastName, setLastName] = useState('');
|
7 | 9 | const [email, setEmail] = useState('');
|
8 | 10 | const [password, setPassword] = useState('');
|
9 | 11 | const [rePassword, setRePassword] = useState('');
|
| 12 | + |
10 | 13 | const [error, setError] = useState('');
|
| 14 | + const navigate = useNavigate(); |
11 | 15 | const [isLoading, setIsLoading] = useState(false);
|
12 | 16 |
|
13 |
| - const getSubmitData = async (e) => { |
| 17 | + const handleSignUp = async (e) => { |
14 | 18 | e.preventDefault();
|
15 |
| - // setError(null); |
16 |
| - // setIsLoading(true); |
17 |
| - // try { |
18 |
| - // await authenticateUser(email, password); |
19 |
| - // } catch (error) { |
20 |
| - // setError(error.message || "Failed to login. Please try again."); |
21 |
| - // } finally { |
22 |
| - // setIsLoading(false); |
23 |
| - // } |
| 19 | + setError(''); |
| 20 | + |
| 21 | + // Check if passwords match |
| 22 | + if (password !== rePassword) { |
| 23 | + setError('Passwords do not match.'); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const userData = { firstName, lastName ,email, password }; |
| 28 | + |
| 29 | + try { |
| 30 | + setError(''); |
| 31 | + // Registration |
| 32 | + await Create(userData); |
| 33 | + navigate("/login"); |
| 34 | + } catch (error) { |
| 35 | + if (error.response && error.response.data && error.response.data.message) { |
| 36 | + setError(error.response.data.message); |
| 37 | + } else { |
| 38 | + setError("This email is already registered"); |
| 39 | + } |
| 40 | + console.log(error); |
| 41 | + } |
24 | 42 | };
|
25 | 43 |
|
26 | 44 | return (
|
27 |
| - <div className="flex flex-col justify-start pt-6 items-center min-h-80vh bg-gray-100"> |
28 |
| - <div className="max-w-lg w-full p-6 bg-base-100 rounded-lg shadow-lg"> |
| 45 | + <div className="flex flex-col justify-start pt-6 items-center min-h-[60vh]"> |
| 46 | + <div className="max-w-lg w-[95vw] p-6 bg-gray-300 rounded-lg shadow-lg"> |
29 | 47 |
|
30 |
| - <h2 className="font-semibold text-start mt-4 mb-4">Register to get access to write your own reviews</h2> |
31 |
| - <form onSubmit={getSubmitData}> |
| 48 | + <h2 className="font-semibold text-center mt-4 mb-4">Register and write your own reviews</h2> |
| 49 | + <form onSubmit={handleSignUp}> |
32 | 50 | <div className="mb-4">
|
33 | 51 | <label htmlFor="firstName" className="block mb-2 text-sm text-accent-content">
|
34 | 52 | First Name
|
@@ -103,12 +121,14 @@ const Register = () => {
|
103 | 121 |
|
104 | 122 | <div className="text-center">
|
105 | 123 | {error?.password && <p>{error.password}</p>}
|
106 |
| - <Button disabled={isLoading || email === "" |
| 124 | + <ButtonSubmitDefault |
| 125 | + buttonName={isLoading ? "Registering..." : "Register"} |
| 126 | + type="submit" |
| 127 | + disabled={isLoading || email === "" |
107 | 128 | || firstName === "" || lastName === ""
|
108 |
| - || password === "" || rePassword === ""}> |
109 |
| - {isLoading ? 'Registering...' : 'Register'} |
110 |
| - </Button> |
| 129 | + || password === "" || rePassword === ""} /> |
111 | 130 | </div>
|
| 131 | + |
112 | 132 | </form>
|
113 | 133 | <div className="text-center">
|
114 | 134 | {error && <p className="text-error text-sm mt-2">{error}</p>}
|
|
0 commit comments