"use client"; import Button from "@/components/atoms/buttons/Button"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { FormEvent, useState } from "react"; export default function CreateStaffPage() { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [form, setForm] = useState({ name: "", phone: "", password: "" }); const [errors, setErrors] = useState({ name: "", phone: "", password: "", submit: "", }); const validatePhone = (phone: string) => /^(0[3|5|7|8|9])[0-9]{8}$/.test(phone); const handleChange = (field: keyof typeof form) => (e: React.ChangeEvent) => { setForm({ ...form, [field]: e.target.value }); setErrors({ ...errors, [field]: "", submit: "" }); }; const validate = () => { const next = { name: "", phone: "", password: "", submit: "" }; if (!form.name.trim()) next.name = "Please enter full name"; if (!form.phone.trim()) next.phone = "Please enter phone number"; else if (!validatePhone(form.phone)) next.phone = "Invalid phone number (e.g. 0987654321)"; if (!form.password.trim()) next.password = "Please enter password"; else if (form.password.length < 6) next.password = "Password must be at least 6 characters"; setErrors(next); return !next.name && !next.phone && !next.password; }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (!validate()) return; setIsLoading(true); try { const res = await fetch("/api/Staff/signup", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form), }); if (res.ok || res.status === 201) { router.push("/manager"); } else { const errorCode = (await res.text().catch(() => "")).trim(); const errorMap: Record = { ExistedUser: "Phone number already registered", InvalidPhoneNumber: "Invalid phone number", InvalidManager: "Manager account not found", }; setErrors({ ...errors, submit: errorMap[errorCode] ?? "An error occurred, please try again", }); } } catch { setErrors({ ...errors, submit: "Unable to connect, please try again" }); } finally { setIsLoading(false); } }; return (

Create Staff Account

Add a new staff member to the restaurant

{[ { id: "name", label: "Full name", icon: "fa-user", placeholder: "John Doe", field: "name" as const, type: "text", }, { id: "phone", label: "Phone number", icon: "fa-phone", placeholder: "0987654321", field: "phone" as const, type: "tel", }, { id: "password", label: "Password", icon: "fa-lock", placeholder: "At least 6 characters", field: "password" as const, type: "password", }, ].map(({ id, label, icon, placeholder, field, type }) => (
{errors[field] && (

{errors[field]}

)}
))} {errors.submit && (
{errors.submit}
)}
Back to Dashboard
); }