forked from Fan03z/enroll_system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputWithButton.tsx
57 lines (49 loc) · 1.83 KB
/
InputWithButton.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
"use client";
import { Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useState, useContext } from "react";
import { toast } from "react-hot-toast";
import { useRouter } from "next/navigation";
import submitKey from "@/bin/Submit";
import { RegisterContext } from "@/context/RegisterContext";
export function InputWithButton() {
const [InputKey, setInputKey] = useState("");
const [IsSubmit, setIsSubmit] = useState(false);
const router = useRouter();
const { Register, setRegister } = useContext(RegisterContext);
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
setInputKey(e.target.value);
}
async function handleSubmit(e: React.MouseEvent<HTMLButtonElement>) {
e.preventDefault();
if (InputKey.length === 16) {
setIsSubmit(true);
let register = await submitKey(InputKey);
if (register.name == undefined || register.name == "") {
toast.error("Invalid key");
setIsSubmit(false);
} else {
setRegister(register);
router.push("/register");
}
} else {
toast.error("Key must be 16 characters long");
}
}
return (
<div className="flex w-full max-w-lg items-center space-x-2">
<Input type="type" placeholder="Key" value={InputKey} onChange={handleChange} className="w-fix" />
{IsSubmit ? (
<Button disabled>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Sumiting
</Button>
) : (
<Button type="submit" onClick={handleSubmit}>
Submit
</Button>
)}
</div>
);
}