fix shiping

This commit is contained in:
hamid zarghami
2025-10-27 16:28:41 +03:30
parent adbab2f94e
commit e56e73bf7b
+21 -1
View File
@@ -44,6 +44,20 @@ const CartShipping: NextPage = () => {
} }
}, [selectedShipments]) }, [selectedShipments])
// پاک کردن انتخاب‌های نامعتبر وقتی shippingData تغییر می‌کند
useEffect(() => {
if (shippingData?.results?.shipping) {
const validShopIds = new Set(shippingData.results.shipping.map(shop => shop.shopId))
const filteredShipments = Object.fromEntries(
Object.entries(selectedShipments).filter(([shopId]) => validShopIds.has(shopId))
)
if (Object.keys(filteredShipments).length !== Object.keys(selectedShipments).length) {
setSelectedShipments(filteredShipments)
}
}
}, [shippingData, selectedShipments])
// هندلر انتخاب روش ارسال // هندلر انتخاب روش ارسال
const handleShipmentSelect = (shopId: string, shipperId: number) => { const handleShipmentSelect = (shopId: string, shipperId: number) => {
setSelectedShipments(prev => ({ setSelectedShipments(prev => ({
@@ -69,12 +83,18 @@ const CartShipping: NextPage = () => {
setIsSavingShipment(true) setIsSavingShipment(true)
// فیلتر کردن selectedShipments تا فقط shop هایی که در shippingData وجود دارند
const validShopIds = new Set(shippingData?.results?.shipping?.map(shop => shop.shopId) || [])
// تبدیل selectedShipments به فرمت مورد نیاز API // تبدیل selectedShipments به فرمت مورد نیاز API
const shipmentsInfo = Object.entries(selectedShipments).map(([shopId, shipperId]) => ({ const shipmentsInfo = Object.entries(selectedShipments)
.filter(([shopId]) => validShopIds.has(shopId))
.map(([shopId, shipperId]) => ({
shopId, shopId,
shipmentId: shipperId shipmentId: shipperId
})) }))
// ذخیره انتخاب‌ها در API // ذخیره انتخاب‌ها در API
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
saveShipmentCart({ shipmentsInfo }, { saveShipmentCart({ shipmentsInfo }, {