aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/SignUp.js
blob: 11ab367dc053db17593cc6cbf823d86d3a1f1dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, {useState} from "react";
import { useNavigate } from "react-router-dom";
import FlexibleForm from "../components/FlexibleForm";
import benefitTwoImg from "../assets/img/benefit-two.png";

const SignUp = () => {
    const navigate = useNavigate();

    const [formData, setFormData] = useState({
        name: "",
        email: "",
        password: "",
    });

    const handleChange = (e) => {
        const { name, value } = e.target;
        setFormData((prevData) => ({
            ...prevData,
            [name]: value,
        }));
    };


    const handleSubmit = async (event) => {
        event.preventDefault();

        try {
            const response = await fetch(process.env.REACT_APP_REGISTER_ROUTE, {
                method: "POST",
                body: JSON.stringify(formData),
                headers: {
                    'Content-Type': 'application/json'
                },
            });

            if (!response.ok) {
                throw new Error(await response.text());
            }

            console.log('Form submitted successfully');
            navigate("/login");
        } catch (error) {
            console.error('Error submitting form:', error);
        }
    }

    return (
        <>
            <FlexibleForm
                onFormSubmit={handleSubmit}
                data={{
                    image: benefitTwoImg,
                    title: "Create your account",
                    subtitle: "maybe edit this text hmm",
                    formFields: [
                        { label: "Full Name", name: "name", type: "text", autoComplete: "name", required: true, value: formData.name, onChange: handleChange  },
                        { label: "Email address", name: "email", type: "email", autoComplete: "email", required: true, value: formData.email, onChange: handleChange },
                        { label: "Password", name: "password", type: "password", autoComplete: "new-password", required: true, value: formData.password, onChange: handleChange },
                    ],
                    ctaText: "Create Account",
                    ctaLink: { text: "Already have an account?", linkText: "Log in instead!", url: "/login" },
                    underneathButton: {url: "", text: "" }
                }}
            />
        </>
    );
};
export default SignUp;