From 45122f4d7a0e9901d89a3fc881f9b1b6080b6ec9 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 23 Nov 2025 15:56:09 +0330 Subject: [PATCH] create shipment method --- src/pages/shipmentMethod/Create.tsx | 125 ++++++++++++++++++ .../hooks/useShipmentMethodData.ts | 15 ++- .../service/ShipmentMethodService.ts | 26 +++- src/pages/shipmentMethod/types/Types.ts | 36 +++++ src/router/Main.tsx | 2 + 5 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 src/pages/shipmentMethod/Create.tsx create mode 100644 src/pages/shipmentMethod/types/Types.ts diff --git a/src/pages/shipmentMethod/Create.tsx b/src/pages/shipmentMethod/Create.tsx new file mode 100644 index 0000000..db68ba2 --- /dev/null +++ b/src/pages/shipmentMethod/Create.tsx @@ -0,0 +1,125 @@ +import { type FC } from 'react' +import { useFormik } from 'formik' +import * as Yup from 'yup' +import { TickCircle } from 'iconsax-react' +import { toast } from 'react-toastify' +import { useNavigate } 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 { useCreateRestaurantShipmentMethod, useGetShipmentMethods } from './hooks/useShipmentMethodData' +import { Pages } from '@/config/Pages' +import type { ErrorType } from '@/helpers/types' +import { extractErrorMessage } from '@/config/func' + +const CreateShipmentMethod: FC = () => { + const navigate = useNavigate() + const { mutate: createRestaurantShipmentMethod, isPending } = useCreateRestaurantShipmentMethod() + const { data: shipmentMethodsData } = useGetShipmentMethods() + + const shipmentMethods = shipmentMethodsData?.data?.map((method: ShipmentMethod) => ({ + label: method.title, + value: method.id + })) || [] + + const formik = useFormik({ + initialValues: { + shipmentMethodId: '', + price: null, + minOrderPrice: null, + isActive: 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) => { + createRestaurantShipmentMethod(values, { + onSuccess: () => { + toast.success('روش ارسال با موفقیت ایجاد شد') + navigate(Pages.shipment_methods.list) + }, + onError: (error: ErrorType) => { + toast.error(extractErrorMessage(error)) + }, + }) + }, + }) + + 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 CreateShipmentMethod diff --git a/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts b/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts index 19855a3..c9a4373 100644 --- a/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts +++ b/src/pages/shipmentMethod/hooks/useShipmentMethodData.ts @@ -1,4 +1,4 @@ -import { useQuery } from "@tanstack/react-query"; +import { useMutation, useQuery } from "@tanstack/react-query"; import * as api from "../service/ShipmentMethodService"; export const useGetRestaurantShipmentMethods = () => { @@ -7,3 +7,16 @@ export const useGetRestaurantShipmentMethods = () => { queryFn: api.getRestaurantShipmentMethods, }); }; + +export const useGetShipmentMethods = () => { + return useQuery({ + queryKey: ["shipment-methods"], + queryFn: api.getShipmentMethods, + }); +}; + +export const useCreateRestaurantShipmentMethod = () => { + return useMutation({ + mutationFn: api.createRestaurantShipmentMethod, + }); +}; diff --git a/src/pages/shipmentMethod/service/ShipmentMethodService.ts b/src/pages/shipmentMethod/service/ShipmentMethodService.ts index 312ab70..9407326 100644 --- a/src/pages/shipmentMethod/service/ShipmentMethodService.ts +++ b/src/pages/shipmentMethod/service/ShipmentMethodService.ts @@ -1,6 +1,30 @@ import axios from "@/config/axios"; +import type { + CreateShipmentMethodType, + GetShipmentMethodsResponseType, + GetRestaurantShipmentMethodsResponseType, +} from "../types/Types"; -export const getRestaurantShipmentMethods = async () => { +export const getShipmentMethods = async (): Promise< + GetShipmentMethodsResponseType +> => { + const { data } = await axios.get(`/admin/shipment-methods`); + return data; +}; + +export const getRestaurantShipmentMethods = async (): Promise< + GetRestaurantShipmentMethodsResponseType +> => { const { data } = await axios.get("/admin/restaurant-shipment-methods"); return data; }; + +export const createRestaurantShipmentMethod = async ( + params: CreateShipmentMethodType +) => { + const { data } = await axios.post( + "/admin/restaurant-shipment-methods", + params + ); + return data; +}; diff --git a/src/pages/shipmentMethod/types/Types.ts b/src/pages/shipmentMethod/types/Types.ts new file mode 100644 index 0000000..c7cc4f8 --- /dev/null +++ b/src/pages/shipmentMethod/types/Types.ts @@ -0,0 +1,36 @@ +import type { IResponse } from "@/types/response.types"; + +export type ShipmentMethod = { + id: string; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + name: string; + title: string; + description: string; + isActive: boolean; +}; + +export type GetShipmentMethodsResponseType = IResponse; + +export type CreateShipmentMethodType = { + shipmentMethodId: string; + price: number | null; + minOrderPrice: number | null; + isActive: boolean; +}; + +export type RestaurantShipmentMethod = { + id: string; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + shipmentMethod: ShipmentMethod; + price: number | null; + minOrderPrice: number | null; + isActive: boolean; +}; + +export type GetRestaurantShipmentMethodsResponseType = IResponse< + RestaurantShipmentMethod[] +>; diff --git a/src/router/Main.tsx b/src/router/Main.tsx index cf82d11..6ff6efb 100644 --- a/src/router/Main.tsx +++ b/src/router/Main.tsx @@ -31,6 +31,7 @@ import AdminList from '@/pages/admin/List' import CreateAdmin from '@/pages/admin/Create' import UpdateAdmin from '@/pages/admin/Update' import ShipmentMethodList from '@/pages/shipmentMethod/List' +import CreateShipmentMethod from '@/pages/shipmentMethod/Create' const MainRouter: FC = () => { const { hasSubMenu } = useSharedStore() @@ -76,6 +77,7 @@ const MainRouter: FC = () => { } /> } /> + } />