"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: "" }); const [errors, setErrors] = useState({ name: "", phone: "", password: "", eateryName: "", submit: "" }); useEffect(() => { fetch("/api/manager/signup") .then((res) => { setPageState("available") // if (res.ok) ; // else if (res.status === 403) setPageState("closed"); // else setPageState("error"); }) .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: "", 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"; setErrors(next); return !next.name && !next.phone && !next.password && !next.eateryName; }; 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.

Quay lại đăng nhập
)} {/* Error */} {pageState === "error" && (

Không thể kết nối

Không thể kiểm tra trạng thái đăng ký. Vui lòng thử lại.

Quay lại đăng nhập
)} {/* Signup Form */} {pageState === "available" && (
{[ { id: "name", label: "Họ tên", icon: "fa-user", placeholder: "Nguyễn Văn A", field: "name" as const, type: "text" }, { id: "phone", label: "Số điện thoại", icon: "fa-phone", placeholder: "0987654321", field: "phone" as const, type: "tel" }, { id: "password", label: "Mật khẩu", icon: "fa-lock", placeholder: "Ít nhất 6 ký tự", field: "password" as const, type: "password" }, { id: "eateryName", label: "Tên nhà hàng", icon: "fa-store", placeholder: "Coffee & More", field: "eateryName" as const, type: "text" }, ].map(({ id, label, icon, placeholder, field, type }) => (
{errors[field] && (

{errors[field]}

)}
))} {errors.submit && (
{errors.submit}
)}
Đã có tài khoản? Đăng nhập
)}
); }