-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoginPanel.tsx
155 lines (145 loc) · 4.5 KB
/
LoginPanel.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
155
import React, { useEffect, useState } from "react";
import { AxiosError } from "axios";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import { Input } from "../ui/input";
import AuthService from "@/service/authService";
import useAuth from "@/hooks/useAuth";
const LoginPanel = () => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [isRegistering, setIsRegistering] = useState(false);
const [open, setOpen] = useState(false);
const { saveToken } = useAuth();
useEffect(() => {
setTimeout(() => {
setError("");
}, 5000);
}, [error]);
useEffect(() => {
if (!open) {
setUsername("");
setPassword("");
setError("");
}
}, [open]);
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
if (!username || !password) {
setError("Please enter a username and password");
return;
}
if (isRegistering) {
const authResponse = await AuthService.register(username, password);
saveToken(authResponse.token);
setOpen(false);
return;
}
const authResponse = await AuthService.login(username, password);
saveToken(authResponse.token);
setOpen(false);
}
catch (error: unknown) {
console.error(error);
if (error instanceof AxiosError) {
setError(error.response?.data.error || "An error occurred");
return;
}
setError("An unkown error occurred");
}
}
return (
<Dialog open={open} onOpenChange={setOpen} modal>
<DialogTrigger asChild>
<button
className=""
onClick={() => setOpen(true)}
aria-label="Open login dialog"
data-testid="open-login-button"
>
LOGIN
</button>
</DialogTrigger>
<DialogContent
className="bg-slate-100"
onCloseAutoFocus={(event) => {
event.preventDefault();
}}
>
<DialogHeader>
<DialogTitle className='text-muted-foreground'>Login</DialogTitle>
<DialogDescription>
Please enter your credentials to continue
</DialogDescription>
</DialogHeader>
<form className="flex flex-col" onSubmit={handleSubmit} data-testid="login-form">
<div className="p-2">
<label htmlFor="username" className="sr-only">
Username
</label>
<Input
id="username"
type="text"
placeholder="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
aria-label="Username"
data-testid="username-input"
/>
</div>
<div className="p-2">
<label htmlFor="password" className="sr-only">
Password
</label>
<Input
id="password"
type="password"
placeholder="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
aria-label="Password"
data-testid="password-input"
/>
</div>
<div className="flex justify-end">
<div className="flex flex-col items-end">
{error && (
<p className="text-right text-red-500" data-testid="error-message">
{error}
</p>
)}
<button
type="submit"
className="p-2 mt-2 text-white transition-colors rounded-md bg-slate-500 hover:bg-slate-400"
aria-label={isRegistering ? "Register" : "Login"}
data-testid="submit-button"
>
{isRegistering ? "Register" : "Login"}
</button>
</div>
</div>
</form>
<DialogFooter>
<button
className="border-none text-muted-foreground bg-none test-sm"
onClick={() => setIsRegistering(!isRegistering)}
aria-label={isRegistering ? "Switch to Login" : "Switch to Register"}
data-testid="toggle-register-button"
>
{isRegistering ? "Click here to Login instead" : "Click here to Register instead"}
</button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};
export default LoginPanel;