diff --git a/src/pages/shipment/Update.tsx b/src/pages/shipment/Update.tsx new file mode 100644 index 0000000..78a915f --- /dev/null +++ b/src/pages/shipment/Update.tsx @@ -0,0 +1,264 @@ +import { type FC } from 'react' +import { useFormik } from 'formik' +import { type CreateShipper, type Cost } from './types/Types' +import * as Yup from 'yup' +import { useGetShipmentById, useUpdateShipment } from './hooks/useShipmentData' +import { useNavigate, useParams } from 'react-router-dom' +import Input from '../../components/Input' +import Textarea from '../../components/Textarea' +import Button from '../../components/Button' +import Select from '../../components/Select' +import { TickCircle, Add, Trash } from 'iconsax-react' +import { toast } from 'react-toastify' +import { extractErrorMessage } from '../../helpers/utils' +import { Pages } from '../../config/Pages' + +const UpdateShipment: FC = () => { + const { id } = useParams<{ id: string }>() + const navigate = useNavigate() + const updateShipmentMutation = useUpdateShipment() + const { data: shipmentDetail, isLoading: shipmentDetailLoading, error: shipmentDetailError } = useGetShipmentById(id!) + + const formik = useFormik({ + initialValues: { + name: shipmentDetail?.name || '', + description: shipmentDetail?.description || '', + costs: shipmentDetail?.costs || [], + deliveryTime: shipmentDetail?.deliveryTime || 0, + deliveryType: shipmentDetail?.deliveryType || 'Standard', + }, + enableReinitialize: true, + validationSchema: Yup.object({ + name: Yup.string().required('عنوان روش ارسال الزامی است'), + description: Yup.string().required('توضیحات روش ارسال الزامی است'), + costs: Yup.array().min(1, 'حداقل یک محدوده وزنی الزامی است'), + deliveryTime: Yup.number().min(0, 'زمان ارسال باید بزرگتر از صفر باشد').required('زمان ارسال الزامی است'), + deliveryType: Yup.string().oneOf(['Standard', 'SameDay', 'Express'], 'نوع ارسال نامعتبر است'), + }), + onSubmit: async (values) => { + values.deliveryTime = Number(values.deliveryTime) + updateShipmentMutation.mutate({ id: id!, params: values }, { + onSuccess: () => { + toast.success('روش ارسال با موفقیت ویرایش شد') + navigate(Pages.shipment.list) + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + } + }) + }, + }) + + const addCostRange = () => { + const newCost: Cost = { + weightRange: { min: 0, max: 0 }, + cost_first: 0, + cost_rest: 0 + } + formik.setFieldValue('costs', [...formik.values.costs, newCost]) + } + + const removeCostRange = (index: number) => { + const newCosts = formik.values.costs.filter((_, i) => i !== index) + formik.setFieldValue('costs', newCosts) + } + + const updateCostRange = (index: number, field: keyof Cost, value: number | { min?: number; max?: number }) => { + const newCosts = [...formik.values.costs] + if (field === 'weightRange') { + newCosts[index].weightRange = { ...newCosts[index].weightRange, ...(value as { min?: number; max?: number }) } + } else { + newCosts[index][field] = value as number + } + formik.setFieldValue('costs', newCosts) + } + + if (shipmentDetailLoading) { + return ( +
+
+
+
+

در حال بارگذاری...

+
+
+
+ ) + } + + if (shipmentDetailError) { + return ( +
+
+

خطا در بارگذاری اطلاعات روش ارسال

+
+
+ ) + } + + if (!shipmentDetail) { + return ( +
+
+

روش ارسال یافت نشد

+
+
+ ) + } + + return ( +
+
+
+

ویرایش روش ارسال

+
+
+ +
+
+ +
+ {/* اطلاعات اصلی */} +
+

اطلاعات اصلی

+ +
+
+ + +
+ +