-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm.tsx
More file actions
229 lines (198 loc) · 11.4 KB
/
Form.tsx
File metadata and controls
229 lines (198 loc) · 11.4 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React, { useState, ChangeEvent} from 'react';
import { Link } from "react-router-dom";
import { useForm } from "react-hook-form"
import './Form.css'
// interface formProps{
// callbackForUserData:(a:any)=> void
// }
let Form: React.FC = (): React.ReactElement => {
// useEffect(() => {
// callbackForUserData(formDataVariable)
// }, [formDataVariable]);
const [showPassword, setShowPassword] = useState<boolean>(false)
const [disabledsSwitch, setdisabledsSwitch] = useState<boolean>(false)
//Message upon successfull and un-successfull submission of form
const [responseSentence, setResponseSentence] = useState<any>()
//React Hook Forms
const {
register,
handleSubmit,
watch,
formState: { errors, isSubmitting }
} = useForm()
//onSumit function
let onSubmit = async <t,>(data: t) => {
localStorage.setItem('userData', JSON.stringify(data));
await fetch("http://localhost:3000/form", {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(data)
})
.then((res) => res.json())
.then((sentence) => {
setResponseSentence(sentence.message)
setdisabledsSwitch(true)
})
.catch((error) => {
setResponseSentence(
error.message ?
<span className='text-red-500 font-semibold text-sm'>{error.message}</span> : 'unknown error')
})
}
const password = watch("password")
//Eye for showing password
let set_Eye_for_password = (): void => {
setShowPassword(!showPassword)
}
//placeholder color for dropdown
const [placeholderColor, setPlaceholderColor] = useState<boolean>(true)
const PlaceholderColorvalue = (e: ChangeEvent<HTMLSelectElement>): void => {
setPlaceholderColor(e.target.value === "")
}
return (
<>
<div className="bg-zinc-950 h-dvh w-screen grid place-content-center text-white ">
<div className='bg-[#383838] p-[30px] rounded-lg w-fit flex justify-center items-center gap-auto flex-col'>
<h2 className='font-semibold mb-3'>User Information</h2>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="grid grid-cols-2 gap-y-2.5 gap-x-8">
<div className='flex flex-col justify-start gap-1'>
<label htmlFor='Firstname' className='text-sm'>Firstname</label>
<input id='Firstname' className='placeholder:text-sm placeholder:pl-1 rounded-sm px-1 text-black focus:ring-2 focus:outline-none focus:ring-zinc-400 ' placeholder="Firstname"{...register("firstname", { required: { value: true, message: "Must fill this field" }, minLength: { value: 3, message: "Min length is 3 words" } })} />
{errors.firstname && <p className="text-red-600 font-light text-xs">{String(errors.firstname.message)}</p>}
</div>
<div className='flex flex-col justify-start gap-1'>
<label htmlFor='Lastname' className='text-sm' >Lastname</label>
<input id='Lastname' className='placeholder:text-sm placeholder:pl-1 rounded-sm px-1 text-black focus:ring-2 focus:outline-none focus:ring-zinc-400 ' placeholder='Lasttname' {...register("Lastname", { required: { value: true, message: "Must fill this field" }, minLength: { value: 3, message: "Min length is 3 words" } })} />
{errors.Lastname && <p className="text-red-600 font-light text-xs">{String(errors.Lastname.message)}</p>}
</div>
<div className='flex flex-col justify-start gap-1'>
<label htmlFor='E-mail' className='text-sm' >E-mail</label>
<input id='E-mail' className='placeholder:text-sm placeholder:pl-1 rounded-sm px-1 text-black focus:ring-2 focus:outline-none focus:ring-zinc-400' placeholder='E-mail' type="text"
{...register('email', {
required: 'Email is required',
pattern: {
value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
message: 'Invalid email address'
},
minLength: {
value: 5,
message: 'Email must be at least 5 characters long'
},
maxLength: {
value: 50,
message: 'Email must be less than 50 characters long'
}
})}
/>
{errors.email && <p className="text-red-600 font-light text-xs">{String(errors.email.message)}</p>}
</div>
<div className='flex flex-col justify-start gap-1'>
<label htmlFor='Password' className='text-sm' >Password</label>
<div id='Password' className="bg-white rounded-sm overflow-clip flex justify-start flex-row items-center">
{showPassword ? <i className="fa-solid fa-eye-slash text-black w-6 h-4 pl-1" onClick={set_Eye_for_password}></i> : <i className="fa-solid fa-eye text-black w-6 h-4 pl-1" onClick={set_Eye_for_password}></i>}
<input className='shrink-1 placeholder:text-sm text-black rounded-sm placeholder:pl-1 px-1 focus:outline-none ' placeholder='Password'
type={showPassword ? "text" : "password"} autoComplete="new-password"
{...register('password', {
required: 'Password is required',
minLength: {
value: 8,
message: 'Password must be at least 8 characters long'
},
maxLength: {
value: 20,
message: 'Password must be less than 20 characters long'
},
pattern: {
value: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/,
message: 'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character'
}
})}
/>
</div>
{errors.password && <p className="text-red-600 font-light text-xs">{String(errors.password.message)}</p>}
</div>
<div className='flex flex-col justify-start gap-1'>
<label htmlFor='Confirm Password' className='text-sm' >Confirm Password</label>
<input id='Confirm Password' className='placeholder:text-sm placeholder:pl-1 rounded-sm text-black focus:ring-2 focus:outline-none focus:ring-zinc-400 px-1' placeholder='Confirm Password'
type="password"
onPaste={(e) => {
e.preventDefault()
return false
}
}
{...register('confirmPassword', {
required: 'Confirm Password is required',
validate: (value) =>
value === password || "This should match password"
})}
/>
{errors.confirmPassword && <p className="text-red-600 font-light text-xs">{String(errors.confirmPassword.message)}</p>}
</div>
{/* country dropdown */}
<div className='flex flex-col justify-start gap-1'>
<label htmlFor='Country' className='text-sm'>Country</label>
<select
id='Country' className={`placeholder:text-sm placeholder:pl-1 rounded-sm px-1 ${placeholderColor ? "text-placeholdergrey " : "text-black"} focus:ring-2 focus:outline-none focus:ring-zinc-400`}
{...register("country", { required: "Country is required" })} onChange={PlaceholderColorvalue}
>
<option className="text-black" value="">Select your country</option>
<option className="text-black" value="usa">USA</option>
<option className="text-black" value="canada">Canada</option>
<option className="text-black" value="mexico">Mexico</option>
<option className="text-black" value="uk">UK</option>
</select>
{errors.country && <p className="text-red-600 font-light text-xs">{String(errors.country.message)}</p>}
</div>
<div className='col-span-2'>
<p className='text-sm pb-2'>Type of employment</p>
<div className='flex flex-col sm:flex-row sm:justify-start sm:w-max sm:flex gap-2 '>
<label className='text-xs '>
<input {...register("employment", { required: "Employment selection is required" })} className='mr-1' type='radio' value='Govt Employee' />
Govt Employee
</label>
<label className='text-xs'>
<input {...register("employment", { required: "Employment selection is required" })} className='mr-1' type='radio' value='Private Employee' />
Private Employee
</label>
<label className='text-xs'>
<input className='mr-1' {...register("employment", { required: "Employment selection is required" })} type='radio' value='Business' />
Business
</label>
<label className='text-xs'>
<input {...register("employment", { required: "Employment selection is required" })} className='mr-1' type='radio' value='Intelligence/Spy' />
Intelligence/Spy
</label>
</div>
</div>
<div className='flex flex-col justify-start gap-1 col-span-2'>
<label htmlFor="image" className='text-sm'>Upload an image:</label>
<input className='text-xs rounded-lg bg-zinc-600 cursor-pointer focus:outline-none focus:ring-2 focus:ring-slate-500 w-fit'
type="file"
id="image"
{...register('image', {
required: 'Image is required',
validate: {
lessThan15MB: (files) =>
files[0]?.size < 10 * 1024 * 1024 || 'Max image size allowed is 10MB',
isImage: (files) =>
files[0]?.type.startsWith('image/') || 'Only image files are allowed',
},
})}
accept="image/*"
/>
{errors.image && <p className="text-red-600 font-light text-xs">{String(errors.image.message)}</p>}
</div>
</div>
<button type="submit" className="text-white bg-zinc-800 focus:outline-none focus:ring-2 mt-4 focus:ring-zinc-600 font-medium rounded-md text-sm px-3 py-1 hover:bg-zinc-700 disabled:opacity-20" disabled={isSubmitting || disabledsSwitch} >{disabledsSwitch ? "submitted" : "submit"}</button>
</form>
{responseSentence && <div className='italic font-light self-start'>{responseSentence}</div>}
{disabledsSwitch && <div className=' font-normal self-start'>Click here to see your Info <Link to='/userinfo' ><button className="text-white bg-zinc-800 focus:outline-none focus:ring-2 mt-4 focus:ring-zinc-600 font-medium rounded-md text-sm px-3 py-1 hover:bg-zinc-700 disabled:opacity-20">😊</button></Link></div>}
</div>
</div>
</>
)
}
export default Form