feat(checkout): unify manual address form with AddressModal, auto-save address and sync user profile on order
This commit is contained in:
1 parent
aa0dde6a35
commit
41f7e98d97
13 files changed
+9829
-9439
No files matched your search
@@ -8,6 +8,53 @@ import { IRAN_PROVINCES, PROVINCE_CITIES } from "../lib/data/provinces";
|
||||
import SearchableSelect from "./SearchableSelect";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export interface AddressFormData {
|
||||
title: string;
|
||||
receptorName: string;
|
||||
phone: string;
|
||||
province: string;
|
||||
city: string;
|
||||
detail: string;
|
||||
zipCode: string;
|
||||
isDefault: boolean;
|
||||
}
|
||||
|
||||
export const normalizeDigits = (str: string) => {
|
||||
if (!str) return "";
|
||||
const persianDigits = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g];
|
||||
const arabicDigits = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
str = str.replace(persianDigits[i], i.toString()).replace(arabicDigits[i], i.toString());
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
export const validateAddressForm = (formData: AddressFormData): Record<string, string> => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!formData.title?.trim()) newErrors.title = "لطفاً عنوان آدرس را وارد کنید";
|
||||
if (!formData.receptorName?.trim()) newErrors.receptorName = "نام گیرنده را وارد کنید";
|
||||
|
||||
const normalizedPhone = normalizeDigits(formData.phone || "");
|
||||
if (!normalizedPhone) {
|
||||
newErrors.phone = "شماره تماس الزامی است";
|
||||
} else if (!/^09\d{9}$/.test(normalizedPhone)) {
|
||||
newErrors.phone = "شماره موبایل وارد شده معتبر نیست";
|
||||
}
|
||||
|
||||
if (!formData.province?.trim()) newErrors.province = "استان را مشخص کنید";
|
||||
if (!formData.city?.trim()) newErrors.city = "شهر را مشخص کنید";
|
||||
if (!formData.detail?.trim()) newErrors.detail = "آدرس دقیق پستی را وارد کنید";
|
||||
|
||||
const normalizedZip = normalizeDigits(formData.zipCode || "");
|
||||
if (!normalizedZip) {
|
||||
newErrors.zipCode = "کد پستی الزامی است";
|
||||
} else if (!/^\d{10}$/.test(normalizedZip)) {
|
||||
newErrors.zipCode = "کد پستی باید ۱۰ رقم باشد";
|
||||
}
|
||||
|
||||
return newErrors;
|
||||
};
|
||||
|
||||
interface AddressModalProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
@@ -16,7 +63,7 @@ interface AddressModalProps {
|
||||
}
|
||||
|
||||
export default function AddressModal({ isOpen, onClose, onSave, editingAddress }: AddressModalProps) {
|
||||
const [formData, setFormData] = useState<Omit<Address, "id">>({
|
||||
const [formData, setFormData] = useState<AddressFormData>({
|
||||
title: "",
|
||||
receptorName: "",
|
||||
phone: "",
|
||||
@@ -60,45 +107,11 @@ export default function AddressModal({ isOpen, onClose, onSave, editingAddress }
|
||||
});
|
||||
}, [editingAddress, isOpen]);
|
||||
|
||||
const normalizeDigits = (str: string) => {
|
||||
const persianDigits = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g];
|
||||
const arabicDigits = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
str = str.replace(persianDigits[i], i.toString()).replace(arabicDigits[i], i.toString());
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
const validate = () => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!formData.title) newErrors.title = "لطفاً عنوان آدرس را وارد کنید";
|
||||
if (!formData.receptorName) newErrors.receptorName = "نام گیرنده را وارد کنید";
|
||||
|
||||
const normalizedPhone = normalizeDigits(formData.phone);
|
||||
if (!normalizedPhone) {
|
||||
newErrors.phone = "شماره تماس الزامی است";
|
||||
} else if (!/^09\d{9}$/.test(normalizedPhone)) {
|
||||
newErrors.phone = "شماره موبایل وارد شده معتبر نیست";
|
||||
}
|
||||
|
||||
if (!formData.province) newErrors.province = "استان را مشخص کنید";
|
||||
if (!formData.city) newErrors.city = "شهر را مشخص کنید";
|
||||
if (!formData.detail) newErrors.detail = "آدرس دقیق پستی را وارد کنید";
|
||||
|
||||
const normalizedZip = normalizeDigits(formData.zipCode);
|
||||
if (!normalizedZip) {
|
||||
newErrors.zipCode = "کد پستی الزامی است";
|
||||
} else if (!/^\d{10}$/.test(normalizedZip)) {
|
||||
newErrors.zipCode = "کد پستی باید ۱۰ رقم باشد";
|
||||
}
|
||||
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (validate()) {
|
||||
const newErrors = validateAddressForm(formData);
|
||||
setErrors(newErrors);
|
||||
if (Object.keys(newErrors).length === 0) {
|
||||
setIsSubmitting(true);
|
||||
// Simulate small delay for UX
|
||||
setTimeout(() => {
|
||||
@@ -112,7 +125,7 @@ export default function AddressModal({ isOpen, onClose, onSave, editingAddress }
|
||||
toast.success(editingAddress ? "تغییرات آدرس با موفقیت ذخیره شد" : "آدرس جدید با موفقیت اضافه شد");
|
||||
setIsSubmitting(false);
|
||||
onClose();
|
||||
}, 600);
|
||||
}, 300);
|
||||
} else {
|
||||
toast.error("لطفاً موارد خطای فرم را اصلاح کنید");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user