From 2f6a339ceec9043432205f1a47fdbf58de52213c Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Tue, 25 Nov 2025 09:24:03 +0330 Subject: [PATCH] update shipment --- src/pages/shipmentMethod/Update.tsx | 153 ++++++++++++++++++ .../components/ShipmentMethodTableColumns.tsx | 2 +- .../hooks/useShipmentMethodData.ts | 31 +++- .../service/ShipmentMethodService.ts | 25 ++- src/pages/shipmentMethod/types/Types.ts | 5 + src/router/Main.tsx | 2 + 6 files changed, 213 insertions(+), 5 deletions(-) create mode 100644 src/pages/shipmentMethod/Update.tsx diff --git a/src/pages/shipmentMethod/Update.tsx b/src/pages/shipmentMethod/Update.tsx new file mode 100644 index 0000000..0a231d5 --- /dev/null +++ b/src/pages/shipmentMethod/Update.tsx @@ -0,0 +1,153 @@ +import { type FC, useMemo } from 'react' +import { useFormik } from 'formik' +import * as Yup from 'yup' +import { TickCircle } from 'iconsax-react' +import { toast } from 'react-toastify' +import { useNavigate, useParams } from 'react-router-dom' +import Button from '@/components/Button' +import Input from '@/components/Input' +import Select from '@/components/Select' +import Switch from '@/components/Switch' +import type { CreateShipmentMethodType, ShipmentMethod } from './types/Types' +import { useUpdateRestaurantShipmentMethod, useGetRestaurantShipmentMethod, useGetShipmentMethods } from './hooks/useShipmentMethodData' +import { Pages } from '@/config/Pages' +import type { ErrorType } from '@/helpers/types' +import { extractErrorMessage } from '@/config/func' + +const UpdateShipmentMethod: FC = () => { + const navigate = useNavigate() + const { id } = useParams<{ id: string }>() + const { mutate: updateRestaurantShipmentMethod, isPending } = useUpdateRestaurantShipmentMethod() + const { data: shipmentMethodData, isLoading } = useGetRestaurantShipmentMethod(id || '') + const { data: shipmentMethodsData } = useGetShipmentMethods() + + const shipmentMethods = shipmentMethodsData?.data?.map((method: ShipmentMethod) => ({ + label: method.title, + value: method.id + })) || [] + + const initialValues = useMemo(() => { + if (shipmentMethodData?.data) { + const data = shipmentMethodData.data + return { + shipmentMethodId: data.shipmentMethod.id, + price: data.price, + minOrderPrice: data.minOrderPrice, + isActive: data.isActive, + } + } + return { + shipmentMethodId: '', + price: null, + minOrderPrice: null, + isActive: true, + } + }, [shipmentMethodData]) + + const formik = useFormik({ + initialValues, + enableReinitialize: true, + validationSchema: Yup.object().shape({ + shipmentMethodId: Yup.string().required('روش ارسال الزامی است'), + price: Yup.number().nullable().required('قیمت الزامی است').min(0, 'قیمت باید مثبت باشد'), + minOrderPrice: Yup.number().nullable().required('حداقل قیمت سفارش الزامی است').min(0, 'حداقل قیمت سفارش باید مثبت باشد'), + isActive: Yup.boolean(), + }), + onSubmit: (values) => { + if (!id) return + updateRestaurantShipmentMethod( + { id, params: values }, + { + onSuccess: () => { + toast.success('روش ارسال با موفقیت ویرایش شد') + navigate(Pages.shipment_methods.list) + }, + onError: (error: ErrorType) => { + toast.error(extractErrorMessage(error)) + }, + } + ) + }, + }) + + if (isLoading) { + return ( +
+
در حال بارگذاری...
+
+ ) + } + + return ( +
+
+
+ ویرایش روش ارسال +
+
+ +
+
+ +
+
+
+ { + const value = e.target.value === '' ? null : Number(e.target.value) + formik.setFieldValue('price', value) + }} + error_text={formik.touched.price && formik.errors.price ? formik.errors.price : ''} + /> +
+
+ { + const value = e.target.value === '' ? null : Number(e.target.value) + formik.setFieldValue('minOrderPrice', value) + }} + error_text={formik.touched.minOrderPrice && formik.errors.minOrderPrice ? formik.errors.minOrderPrice : ''} + /> +
+
+ formik.setFieldValue('isActive', value)} + label='فعال' + /> +
+
+
+
+ ) +} + +export default UpdateShipmentMethod \ No newline at end of file diff --git a/src/pages/shipmentMethod/components/ShipmentMethodTableColumns.tsx b/src/pages/shipmentMethod/components/ShipmentMethodTableColumns.tsx index 07df42f..88daebe 100644 --- a/src/pages/shipmentMethod/components/ShipmentMethodTableColumns.tsx +++ b/src/pages/shipmentMethod/components/ShipmentMethodTableColumns.tsx @@ -31,7 +31,7 @@ export const getShipmentMethodTableColumns = ({ onDelete, isDeleting }: GetShipm key: 'price', title: 'قیمت', render: (item: RestaurantShipmentMethod) => { - return item.price ? `${item.price.toLocaleString()} تومان` : '-' + return item.price ? `${item.price.toLocaleString()} تومان` : 'رایگان' } }, { diff --git a/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts b/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts index db417b8..4a32707 100644 --- a/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts +++ b/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts @@ -1,6 +1,9 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import * as api from "../service/ShipmentMethodService"; -import type { GetRestaurantShipmentMethodsParams } from "../types/Types"; +import type { + CreateShipmentMethodType, + GetRestaurantShipmentMethodsParams, +} from "../types/Types"; export const useGetRestaurantShipmentMethods = ( params?: GetRestaurantShipmentMethodsParams @@ -41,3 +44,29 @@ export const useDeleteRestaurantShipmentMethod = () => { }, }); }; + +export const useGetRestaurantShipmentMethod = (id: string) => { + return useQuery({ + queryKey: ["restaurant-shipment-method", id], + queryFn: () => api.getRestaurantShipmentMethod(id), + enabled: !!id, + }); +}; + +export const useUpdateRestaurantShipmentMethod = () => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: ({ + id, + params, + }: { + id: string; + params: CreateShipmentMethodType; + }) => api.updateRestaurantShipmentMethod(id, params), + onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: ["restaurant-shipment-methods"], + }); + }, + }); +}; diff --git a/src/pages/shipmentMethod/service/ShipmentMethodService.ts b/src/pages/shipmentMethod/service/ShipmentMethodService.ts index c8f71ae..4934e89 100644 --- a/src/pages/shipmentMethod/service/ShipmentMethodService.ts +++ b/src/pages/shipmentMethod/service/ShipmentMethodService.ts @@ -3,6 +3,7 @@ import type { CreateShipmentMethodType, GetShipmentMethodsResponseType, GetRestaurantShipmentMethodsResponseType, + GetRestaurantShipmentMethodResponseType, GetRestaurantShipmentMethodsParams, } from "../types/Types"; @@ -16,7 +17,7 @@ export const getShipmentMethods = async (): Promise< export const getRestaurantShipmentMethods = async ( params?: GetRestaurantShipmentMethodsParams ): Promise => { - const { data } = await axios.get("/admin/restaurant-shipment-methods", { + const { data } = await axios.get("/admin/shipment-methods/restaurant", { params, }); return data; @@ -26,7 +27,7 @@ export const createRestaurantShipmentMethod = async ( params: CreateShipmentMethodType ) => { const { data } = await axios.post( - "/admin/restaurant-shipment-methods", + "/admin/shipment-methods/restaurant", params ); return data; @@ -34,7 +35,25 @@ export const createRestaurantShipmentMethod = async ( export const deleteRestaurantShipmentMethod = async (id: string) => { const { data } = await axios.delete( - `/admin/restaurant-shipment-methods/${id}` + `/admin/shipment-methods/restaurant/${id}` + ); + return data; +}; + +export const getRestaurantShipmentMethod = async ( + id: string +): Promise => { + const { data } = await axios.get(`/admin/shipment-methods/restaurant/${id}`); + return data; +}; + +export const updateRestaurantShipmentMethod = async ( + id: string, + params: CreateShipmentMethodType +) => { + const { data } = await axios.patch( + `/admin/shipment-methods/restaurant/${id}`, + params ); return data; }; diff --git a/src/pages/shipmentMethod/types/Types.ts b/src/pages/shipmentMethod/types/Types.ts index 90e495a..ac726e4 100644 --- a/src/pages/shipmentMethod/types/Types.ts +++ b/src/pages/shipmentMethod/types/Types.ts @@ -22,6 +22,7 @@ export type CreateShipmentMethodType = { export type RestaurantShipmentMethod = { id: string; + restaurant: string; createdAt: string; updatedAt: string; deletedAt: string | null; @@ -35,6 +36,10 @@ export type GetRestaurantShipmentMethodsResponseType = IResponse< RestaurantShipmentMethod[] >; +export type GetRestaurantShipmentMethodResponseType = IResponse< + RestaurantShipmentMethod +>; + export type GetRestaurantShipmentMethodsParams = { page?: number; limit?: number; diff --git a/src/router/Main.tsx b/src/router/Main.tsx index 6ff6efb..e403c68 100644 --- a/src/router/Main.tsx +++ b/src/router/Main.tsx @@ -32,6 +32,7 @@ import CreateAdmin from '@/pages/admin/Create' import UpdateAdmin from '@/pages/admin/Update' import ShipmentMethodList from '@/pages/shipmentMethod/List' import CreateShipmentMethod from '@/pages/shipmentMethod/Create' +import UpdateShipmentMethod from '@/pages/shipmentMethod/Update' const MainRouter: FC = () => { const { hasSubMenu } = useSharedStore() @@ -78,6 +79,7 @@ const MainRouter: FC = () => { } /> } /> + } />