-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignup.tsx
154 lines (116 loc) · 5.95 KB
/
Signup.tsx
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
import { ReactElement, useState } from 'react'
import { Link} from "react-router-dom";
import { useForm } from "react-hook-form"
function Signup():ReactElement {
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,
formState: { errors, isSubmitting }
} = useForm()
const [showPassword, setShowPassword] = useState<boolean>(false)
const [errorSentence, seterrorSentence] = useState<string>('')
//Eye for showing password
let set_Eye_for_password = (): void => {
setShowPassword(!showPassword)
}
//onSumit function
let onSubmit = async <t,>(data: t) => {
await fetch("http://localhost:3000/signup", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then(res => {
if (!res.ok) {
return res.json().then(err => { throw new Error(err.message) });
}
return res.json();
})
.then((sentence) => {
setResponseSentence(sentence.message);
setdisabledsSwitch(true);
})
.catch((error) => {
seterrorSentence(error.message)
setResponseSentence(
<span className='text-red-500 font-semibold text-sm'>{error.message}</span>
);
});
}
//placeholder color for dropdown
return (
<>
<div className="bg-slate-200 h-screen w-screen grid place-content-center text-white">
<div className='bg-[#383838] p-[30px] rounded-lg whitespace-nowrap flex justify-center items-center gap-auto flex-col'>
<h2 className='font-semibold mb-3'>Signup form</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='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:\none uppercase letter,\none lowercase letter,\none number, and one special character`
}
})}
/>
</div>
{errors.password && <pre className="text-red-600 font-light text-xs">{`${errors.password.message}`}</pre>}
</div>
<div>
</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='self-start'> <span className='italic font-light text-xs '>{responseSentence}</span> {(disabledsSwitch || errorSentence === 'Your credientials already exist, Go to login page ') && (
<span className='text-sm pl-1 italic underline text-blue-500'>
<Link to="/login">Login</Link>
</span>
)}
</div> )}
</div>
</div>
</>
)
}
export default Signup