-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnotes.txt
158 lines (146 loc) · 3.89 KB
/
notes.txt
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// imports
import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
const Registration = ({personData}) => {
const navigate = useNavigate();
const [profile, setProfile] = useState({
name: "",
profession: "",
email: "",
phone: "",
password: "",
});
const [confirmPassword, setConfirmPassword] = useState("");
const sendData = async()=>{
try{
const response = await fetch('http://localhost:5000/profiles',{
method:'POST',
headers: {
'Content-Type': 'application/json',
},
body:JSON.stringify(profile)
})
if(response.ok){
console.log('form data sent')
}else{
console.log('data could not be send')
}
}catch(error){
console.log(error)
}
navigate('/');
}
// functions
function handleInputChange(e) {
e.preventDefault();
const { name, value } = e.target;
setProfile((prevProfile) => ({
...prevProfile,
[name]: value,
}));
}
// function to set confirm password
function confirmPasswordHandler(e) {
// cancels browser refresh while allowing us to execute any code we write here
e.preventDefault();
// we set the value from input
const { value } = e.target;
setConfirmPassword(value);
}
// validate info function
function validateInputInfo(e) {
e.preventDefault();
// create the error variables
let emptyValue ='';
for (const key in profile) {
if(profile[key] === ""){
emptyValue = 'please fill all the attributes'
}
}
let error = "";
if(emptyValue){
console.log(emptyValue)
}else{
if (profile.phone.length !== 10) {
error = "please enter 10 digit numbers";
}
if (confirmPassword !== profile.password) {
error = "confirm Password";
}
if(error) {
console.log(`please fix this ${error}`);
} else {
sendData()
}
}
}
return (
<div className="Registration">
<h2>Heading</h2>
<form>
<label>Full Name</label>
<input
type="text"
name="name"
required
value={personData.name }
placeholder="Enter Full Name...."
onChange={handleInputChange}
/>
{/* */}
<label>Profession</label>
<input
type="text"
name="profession"
required
value={personData ? personData.profession : profile.profession}
placeholder="Enter name of your profession"
onChange={handleInputChange}
/>
{/* */}
<label>Email</label>
<input
type="email"
name="email"
required
value={personData ? personData.email : profile.email}
placeholder="Enter email email@email.com"
onChange={handleInputChange}
/>
{/* */}
<label>Phone number</label>
<input
type="phone"
name="phone"
required
value={personData ? personData.phone : profile.phone}
placeholder="Enter your phone number"
onChange={handleInputChange}
/>
{/* */}
<label>Password</label>
<input
type="password"
name="password"
required
value={personData ? personData.password : profile.password}
placeholder="Enter your password"
onChange={handleInputChange}
/>
{/* */}
<label>Confirm password</label>
<input
type="password"
name="confirmPassword"
required
value={confirmPassword}
placeholder="Please confirm your password"
onChange={confirmPasswordHandler}
/>
{/* */}
<button onClick={validateInputInfo}>Submit</button>
</form>
</div>
);
};
export default Registration;