'use client'; import { useState } from "react"; import { useRouter } from 'next/navigation'; import Link from "next/link"; type FormType = { name: string; phone: string; email: string; address: string; note: string; }; type ErrorType = { name?: string; phone?: string; email?: string; address?: string; }; export default function Form() { const router = useRouter(); const [form, setForm] = useState({ name: "", phone: "", email: "", address: "", note: "" }); const [ errors, setErrors ] = useState({}); const [ submit, setSubmit ] = useState(false); // ========================= // Handle change // ========================= const handleChange = ( e: React.ChangeEvent ) => { const { name, value } = e.target; setForm(prev => ({ ...prev, [name]: value })); // clear error khi user sửa lại field đó if (errors[name as keyof ErrorType]) { setErrors(prev => ({ ...prev, [name]: undefined })); } }; // ========================= // Validate // ========================= const checkField = () => { const newErrors: ErrorType = {}; if (!form.name.trim()) { newErrors.name = "Vui lòng nhập họ và tên"; } if (!form.phone.trim()) { newErrors.phone = "Vui lòng nhập số điện thoại"; } else if (!/^[0-9]{10,11}$/.test(form.phone)) { newErrors.phone = "Số điện thoại không hợp lệ"; } if (!form.email.trim()) { newErrors.email = "Vui lòng nhập email"; } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)) { newErrors.email = "Email không hợp lệ"; } if (!form.address.trim()) { newErrors.address = "Vui lòng nhập địa chỉ nhận hàng"; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; // ========================= // Submit // ========================= const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); setSubmit(false) if (!checkField()) return; console.log("Form hợp lệ:", form); // call API đặt hàng ở đây setSubmit(true) router.push('/send-cart'); }; return (

Thông tin nhận hàng

Để tiếp tục đặt hàng, quý khách xin vui lòng Đăng nhập hoặc nhập thông tin bên dưới. Tư vấn viên sẽ liên hệ theo thông tin bạn cung cấp để xác nhận, không mua không sao
{/* Họ tên */} {errors.name && (

{errors.name}

)} {/* Số điện thoại */} {errors.phone && (

{errors.phone}

)} {/* Email */} {errors.email && (

{errors.email}

)} {/* Địa chỉ */} {errors.address && (

{errors.address}

)}

Nhập Số nhà, Đường, Phường/xã, Quận, Tỉnh

{/* Ghi chú */}