fix asign shipment to shop
This commit is contained in:
@@ -24,22 +24,63 @@ export const ShipmentManagementModal = ({
|
||||
const removeShipmentFromAdminMutation = useRemoveShipmentFromAdmin();
|
||||
|
||||
const shipments = shipmentData?.results?.shipper || [];
|
||||
const adminShipments: ShipmentMethod[] = adminShopData?.results?.data?.shipmentMethod || [];
|
||||
const adminShipments: ShipmentMethod[] = adminShopData?.results?.data?.shipmentMethod || (adminShopData?.results as { shop?: { shipmentMethod?: ShipmentMethod[] } })?.shop?.shipmentMethod || [];
|
||||
|
||||
// لیست روشهای ارسال که هنوز به ادمین اضافه نشدهاند
|
||||
const availableShipments = shipments.filter((shipment: Shipper) =>
|
||||
!adminShipments.some((adminShipment: ShipmentMethod) => adminShipment._id === shipment._id)
|
||||
);
|
||||
const availableShipments = shipments.filter((shipment: Shipper) => {
|
||||
// اگر adminShipments خالی باشد، همه shipments را نشان بده
|
||||
if (adminShipments.length === 0) {
|
||||
return true;
|
||||
}
|
||||
// اگر shipment._id وجود داشته باشد، از _id برای مقایسه استفاده کن
|
||||
if (shipment._id != null) {
|
||||
return !adminShipments.some((adminShipment: ShipmentMethod) =>
|
||||
adminShipment._id != null && adminShipment._id === shipment._id
|
||||
);
|
||||
}
|
||||
// اگر _id وجود نداشته باشد، از name برای مقایسه استفاده کن
|
||||
return !adminShipments.some((adminShipment: ShipmentMethod) =>
|
||||
adminShipment.name === shipment.name
|
||||
);
|
||||
});
|
||||
|
||||
const handleAddShipmentToAdmin = async () => {
|
||||
if (!selectedShipment) return;
|
||||
|
||||
await addShipmentToAdminMutation.mutateAsync(Number(selectedShipment), {
|
||||
// اگر value با "index-" شروع شود، یعنی _id وجود ندارد
|
||||
if (selectedShipment.startsWith('index-')) {
|
||||
const index = parseInt(selectedShipment.replace('index-', ''));
|
||||
const shipment = availableShipments[index];
|
||||
if (!shipment || shipment._id == null) {
|
||||
toast.error('شناسه روش ارسال یافت نشد. لطفاً صفحه را رفرش کنید.');
|
||||
return;
|
||||
}
|
||||
await addShipmentToAdminMutation.mutateAsync(Number(shipment._id), {
|
||||
onError: (error) => {
|
||||
toast.error(extractErrorMessage(error));
|
||||
},
|
||||
onSuccess: () => {
|
||||
refetchAdminShop();
|
||||
setSelectedShipment('');
|
||||
toast.success('روش ارسال با موفقیت اضافه شد');
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const shipmentId = Number(selectedShipment);
|
||||
if (isNaN(shipmentId)) {
|
||||
toast.error('شناسه روش ارسال نامعتبر است');
|
||||
return;
|
||||
}
|
||||
|
||||
await addShipmentToAdminMutation.mutateAsync(shipmentId, {
|
||||
onError: (error) => {
|
||||
toast.error(extractErrorMessage(error));
|
||||
},
|
||||
onSuccess: () => {
|
||||
refetchAdminShop();
|
||||
setSelectedShipment('');
|
||||
toast.success('روش ارسال با موفقیت اضافه شد');
|
||||
}
|
||||
});
|
||||
@@ -87,12 +128,14 @@ export const ShipmentManagementModal = ({
|
||||
placeholder="انتخاب روش ارسال"
|
||||
value={selectedShipment}
|
||||
onChange={(e: React.ChangeEvent<HTMLSelectElement>) => setSelectedShipment(e.target.value)}
|
||||
items={availableShipments
|
||||
.filter((shipment: Shipper) => shipment._id != null)
|
||||
.map((shipment: Shipper) => ({
|
||||
value: String(shipment._id),
|
||||
items={availableShipments.map((shipment: Shipper, index: number) => {
|
||||
// اگر _id وجود داشته باشد، از آن استفاده کن، در غیر این صورت از index
|
||||
const value = shipment._id != null ? String(shipment._id) : `index-${index}`;
|
||||
return {
|
||||
value,
|
||||
label: shipment.name
|
||||
}))}
|
||||
};
|
||||
})}
|
||||
/>
|
||||
<Button
|
||||
label="اضافه کردن"
|
||||
@@ -114,25 +157,25 @@ export const ShipmentManagementModal = ({
|
||||
{adminShipments
|
||||
.filter((shipment: ShipmentMethod) => shipment._id != null)
|
||||
.map((shipment: ShipmentMethod) => (
|
||||
<div key={shipment._id} className="flex items-center justify-between p-4 bg-gray-50 rounded-xl">
|
||||
<div>
|
||||
<h4 className="font-medium">{shipment.name}</h4>
|
||||
<p className="text-sm text-gray-600">{shipment.description}</p>
|
||||
<p className="text-xs text-gray-500 mt-1">
|
||||
نوع ارسال: {getDeliveryTypeText(shipment.deliveryType)} |
|
||||
زمان ارسال: {shipment.deliveryTime} روز
|
||||
</p>
|
||||
<div key={shipment._id} className="flex items-center justify-between p-4 bg-gray-50 rounded-xl">
|
||||
<div>
|
||||
<h4 className="font-medium">{shipment.name}</h4>
|
||||
<p className="text-sm text-gray-600">{shipment.description}</p>
|
||||
<p className="text-xs text-gray-500 mt-1">
|
||||
نوع ارسال: {getDeliveryTypeText(shipment.deliveryType)} |
|
||||
زمان ارسال: {shipment.deliveryTime} روز
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
variant="danger"
|
||||
size="sm"
|
||||
label="حذف"
|
||||
onClick={() => handleRemoveShipmentFromAdmin(String(shipment._id))}
|
||||
isLoading={removeShipmentFromAdminMutation.isPending}
|
||||
className="w-fit"
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
variant="danger"
|
||||
size="sm"
|
||||
label="حذف"
|
||||
onClick={() => handleRemoveShipmentFromAdmin(String(shipment._id))}
|
||||
isLoading={removeShipmentFromAdminMutation.isPending}
|
||||
className="w-fit"
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user