From 333c2bf025c052e8739b669d1c56a9fdc26f54ca Mon Sep 17 00:00:00 2001
From: haniyeroozmand <147385975+haniyeroozmand@users.noreply.github.com>
Date: Mon, 27 Apr 2026 14:23:07 +0330
Subject: [PATCH] edit/delete address api
---
app/checkout/page.tsx | 289 +++++++++++++++++++---------
public/src/services/address/api.tsx | 67 ++++++-
2 files changed, 262 insertions(+), 94 deletions(-)
diff --git a/app/checkout/page.tsx b/app/checkout/page.tsx
index 972dee5..2869a0d 100644
--- a/app/checkout/page.tsx
+++ b/app/checkout/page.tsx
@@ -15,7 +15,7 @@ import {
} from "lucide-react";
import PaymentMethodsSection from "@/components/PaymentMethods";
import { getCartApi } from "@/public/src/services/cart/api";
-import { fetchUserAddresses, type Address, addAddressApi, NewAddressData } from "@/public/src/services/address/api";
+import { fetchUserAddresses, type Address, addAddressApi, NewAddressData, updateAddressApi, deleteAddressApi } from "@/public/src/services/address/api";
@@ -133,12 +133,122 @@ export default function CheckoutPage() {
}
};
+ const [editingAddressId, setEditingAddressId] = useState(null);
+
+ // 2. تابع کلیک روی دکمه ویرایش
+ const handleEditClick = (address: any, e: any) => {
+ e.preventDefault();
+ e.stopPropagation(); // برای جلوگیری از انتخاب شدن radio button هنگام کلیک روی ویرایش
+
+ setEditingAddressId(address.id);
+ // پر کردن مقادیر فرم با دیتای آدرس انتخاب شده (فرض بر این است که استیت شما setNewAddress نام دارد)
+ setNewAddress({
+ title: address.title || "",
+ recipientName: address.recipientName || "",
+ phone: address.phone || "",
+ province: address.province || "",
+ city: address.city || "",
+ postalCode: address.postalCode || "",
+ addressLine: address.addressLine || "",
+ plaque: address.plaque || "",
+ unit: address.unit || "",
+ isDefault: address.isDefault || false
+ });
+ setShowNewAddressForm(true);
+ };
+
+ // 3. تابع فراخوانی API ویرایش
+ const handleUpdateAddress = async () => {
+ // اضافه کردن این شرط برای جلوگیری از خطای تایپ و توقف اجرا در صورت null بودن ID
+ if (!editingAddressId) return;
+
+ try {
+ const response = await updateAddressApi(editingAddressId, newAddress);
+ if (response.success) {
+ // آپدیت کردن آدرس ویرایش شده در لیست آدرسها
+ setAddresses(prevAddresses =>
+ prevAddresses.map(addr => addr.id === editingAddressId ? response.data : addr)
+ );
+
+ // خروج از حالت فرم و ریست کردن مقادیر
+ setShowNewAddressForm(false);
+ setEditingAddressId(null);
+ setNewAddress({
+ title: "", recipientName: "", phone: "", province: "", city: "", postalCode: "", addressLine: "", plaque: "", unit: "", isDefault: false
+ });
+ }
+ } catch (error) {
+ // مدیریت خطا (در صورت نیاز آلرت یا توست نمایش دهید)
+ console.error("خطا در بهروزرسانی آدرس:", error);
+ }
+ };
+
+ // 4. تابع انصراف یکپارچه شده
+ const handleCancelForm = () => {
+ setShowNewAddressForm(false);
+ setEditingAddressId(null);
+ setNewAddress({
+ title: "", recipientName: "", phone: "", province: "", city: "", postalCode: "", addressLine: "", plaque: "", unit: "", isDefault: false
+ });
+ };
+
+ const handleDeleteAddress = async (addressId: string, e: React.MouseEvent) => {
+ e.stopPropagation(); // جلوگیری از انتخاب شدن آدرس هنگام کلیک روی دکمه حذف
+
+ // گرفتن تاییدیه از کاربر قبل از حذف
+ const confirmDelete = window.confirm("آیا از حذف این آدرس اطمینان دارید؟");
+ if (!confirmDelete) return;
+
+ try {
+ const response = await deleteAddressApi(addressId);
+
+ if (response.success) {
+ // حذف آدرس از لیست موجود در State
+ setAddresses(prevAddresses =>
+ prevAddresses.filter(addr => addr.id !== addressId)
+ );
+
+ // اگر آدرسی که پاک شد همان آدرس انتخابشده بود، انتخاب را لغو کن
+ if (selectedAddressId === addressId) {
+ setSelectedAddressId(null);
+ }
+
+ // اگر آدرسی که پاک شد در حال ویرایش بود، فرم ویرایش را ببند
+ if (editingAddressId === addressId) {
+ setShowNewAddressForm(false);
+ setEditingAddressId(null);
+ setNewAddress({
+ title: "", recipientName: "", phone: "", province: "", city: "", postalCode: "", addressLine: "", plaque: "", unit: "", isDefault: false
+ });
+ }
+ }
+ } catch (error) {
+ console.error("خطا در حذف آدرس:", error);
+ // اینجا میتوانید یک Toast یا Alert برای نمایش خطا به کاربر اضافه کنید
+ }
+ };
+
+
+
+
+
+
+
+
+
+
// محاسبه قیمت کل و تعداد
const parsePrice = (priceStr?: number | null | string) => {
if (!priceStr) return 0;
return Number(priceStr.toString().replace(/,/g, ''));
};
+
+
+
+
+
+
// ۴. استفاده از اطلاعات سرور در صورت لاگین بودن، در غیر این صورت استفاده از Context
const totalPrice = isAuthenticated && serverSummary
? serverSummary.totalPrice || serverSummary.total || 0
@@ -239,11 +349,13 @@ export default function CheckoutPage() {
{showNewAddressForm ? (
-
+
{addresses.length > 0 && (
@@ -386,7 +498,11 @@ export default function CheckoutPage() {
انتخاب آدرس ارسال
setShowNewAddressForm(true)}
+ onClick={() => {
+ setEditingAddressId(null);
+ setNewAddress({ title: "", recipientName: "", phone: "", province: "", city: "", postalCode: "", addressLine: "", plaque: "", unit: "", isDefault: false });
+ setShowNewAddressForm(true);
+ }}
className="text-sm cursor-pointer text-blue-600 hover:text-blue-800 font-medium flex items-center gap-1"
>
+ افزودن آدرس جدید
@@ -402,118 +518,89 @@ export default function CheckoutPage() {
{addresses.map((address) => (