"use client"; import { Button, TextInput } from "@/components/atoms"; import Link from "next/link"; import { useState } from "react"; interface FormState { name: string; phone: string; password: string; } interface FormErrors { name?: string; phone?: string; password?: string; } const PHONE_RE = /^0\d{9}$/; function validate(data: FormState): FormErrors { const errors: FormErrors = {}; if (!data.name.trim()) errors.name = "Name is required"; if (!data.phone.trim()) { errors.phone = "Phone number is required"; } else if (!PHONE_RE.test(data.phone.trim())) { errors.phone = "Must be 10 digits starting with 0 (e.g. 0912345678)"; } if (!data.password) { errors.password = "Password is required"; } else if (data.password.length < 6) { errors.password = "Password must be at least 6 characters"; } return errors; } export default function CreateStaffPage() { const [form, setForm] = useState({ name: "", phone: "", password: "" }); const [errors, setErrors] = useState({}); const [apiError, setApiError] = useState(null); const [success, setSuccess] = useState(false); const [loading, setLoading] = useState(false); const [showPassword, setShowPassword] = useState(false); function handleChange(e: React.ChangeEvent) { const { name, value } = e.target; setForm((prev) => ({ ...prev, [name]: value })); setErrors((prev) => ({ ...prev, [name]: undefined })); setApiError(null); setSuccess(false); } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); const validation = validate(form); if (Object.keys(validation).length > 0) { setErrors(validation); return; } setLoading(true); setApiError(null); try { const res = await fetch("/api/Staff/signup", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name.trim(), phone: form.phone.trim(), password: form.password, }), }); if (res.ok) { setSuccess(true); setForm({ name: "", phone: "", password: "" }); setErrors({}); } else { const text = await res.text(); setApiError(text || "Failed to create account. Please try again."); } } catch { setApiError("Connection error. Please check your network and try again."); } finally { setLoading(false); } } return (
Back to Dashboard

Create Staff Account

Fill in the details below to register a new staff member.

{success && (
Staff account created successfully.
)} {apiError && (
{apiError}
)}
setShowPassword((v) => !v)} autoComplete="new-password" />
); }