-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNav.tsx
More file actions
74 lines (67 loc) · 1.79 KB
/
Copy pathNav.tsx
File metadata and controls
74 lines (67 loc) · 1.79 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
'use client'
import React, { ChangeEvent, FC, FormEvent, useState } from 'react'
import { User } from '@/app/types'
import useUser from '@/app/hooks/useUser'
const LoginForm: FC<{ defaultUser: User | undefined }> = ({ defaultUser }) => {
const { login, logout, user } = useUser(defaultUser)
const [formData, setFormData] = useState({
email: '',
password: 'example',
})
const disableLogin = !formData.email || !formData.password
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target
setFormData({
...formData,
[name]: value,
})
}
const handleLogin = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (!disableLogin) {
login(formData)
}
}
const handleLogout = () => {
logout()
}
return (
<div className='d-flex p-4 flex-row justify-content-end'>
{user ? (
<button onClick={handleLogout} className='btn btn-link'>
Logout
</button>
) : (
<form className='d-flex flex-row gap-4' onSubmit={handleLogin}>
<input
className='form-control'
id='email'
name='email'
placeholder='Username'
value={formData.email}
onChange={handleChange}
required
/>
<input
placeholder='Password'
type='password'
className='form-control'
id='password'
name='password'
value={formData.password}
onChange={handleChange}
required
/>
<button
disabled={disableLogin}
type='submit'
className='btn btn-primary'
>
Login
</button>
</form>
)}
</div>
)
}
export default LoginForm