"use client"; import Button from "@/components/atoms/buttons/Button"; import { SHOP_INFO } from "@/lib/constants"; import Image from "next/image"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { FormEvent, useEffect, useState } from "react"; type PageState = "checking" | "available" | "closed" | "error"; export default function ManagerSignupPage() { const router = useRouter(); const [pageState, setPageState] = useState("checking"); const [isLoading, setIsLoading] = useState(false); const [form, setForm] = useState({ name: "", phone: "", password: "", eateryName: "", bankAccount: "", // 1. Thêm bankAccount vào state }); const [errors, setErrors] = useState({ name: "", phone: "", password: "", eateryName: "", bankAccount: "", // 2. Thêm bankAccount vào errors state submit: "", }); useEffect(() => { fetch("/api/manager/signup") .then((res) => { setPageState("available"); }) .catch(() => setPageState("error")); }, []); 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: "", eateryName: "", bankAccount: "", submit: "", }; if (!form.name.trim()) next.name = "Please enter your full name"; if (!form.phone.trim()) next.phone = "Please enter your phone number"; else if (!validatePhone(form.phone)) next.phone = "Invalid phone number (e.g. 0987654321)"; if (!form.password.trim()) next.password = "Please enter your password"; else if (form.password.length < 6) next.password = "Password must be at least 6 characters"; if (!form.eateryName.trim()) next.eateryName = "Please enter the restaurant name"; if (!form.bankAccount.trim()) next.bankAccount = "Please enter your bank account number"; setErrors(next); return ( !next.name && !next.phone && !next.password && !next.eateryName && !next.bankAccount ); }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (!validate()) return; setIsLoading(true); try { const res = await fetch("/api/manager/signup", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form), }); if (res.ok || res.status === 201) { router.push("/login"); } else { const errorCode = (await res.text().catch(() => "")).trim(); const errorMap: Record = { ExistedUser: "Phone number already registered", InvalidPhoneNumber: "Invalid phone number", }; 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 (
{/* Logo */}
{SHOP_INFO.name}

{SHOP_INFO.name}

Create a manager account

{/* Checking */} {pageState === "checking" && (

Checking...

)} {/* Closed */} {pageState === "closed" && (

Registration closed

The system already has a restaurant. Registration is no longer available.

Back to login
)} {/* Error */} {pageState === "error" && (

Unable to connect

Unable to check registration status. Please try again.

Back to login
)} {/* Signup Form */} {pageState === "available" && (
{[ { 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", }, { id: "eateryName", label: "Restaurant name", icon: "fa-store", placeholder: "Coffee & More", field: "eateryName" as const, type: "text", }, { id: "bankAccount", label: "Bank account number (example: acb-44359797)", icon: "fa-credit-card", placeholder: "Enter account number (for QR payment)", field: "bankAccount" as const, type: "text", }, ].map(({ id, label, icon, placeholder, field, type }) => (
{errors[field] && (

{errors[field]}

)}
))} {errors.submit && (
{errors.submit}
)}
Already have an account? Sign in
)}
); }