From d5cbee4b82e1e639fcac165cc9bbc2a9de0475f4 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Mon, 29 Sep 2025 10:05:03 +0330 Subject: [PATCH] update warranty --- src/pages/warranty/List.tsx | 18 ++- src/pages/warranty/Update.tsx | 146 ++++++++++++++++++ src/pages/warranty/hooks/useWarrantyData.ts | 20 ++- src/pages/warranty/service/WarrantyService.ts | 16 ++ src/pages/warranty/types/Types.ts | 8 + src/router/Main.tsx | 2 + 6 files changed, 203 insertions(+), 7 deletions(-) create mode 100644 src/pages/warranty/Update.tsx diff --git a/src/pages/warranty/List.tsx b/src/pages/warranty/List.tsx index 4bb5f7c..eed6d16 100644 --- a/src/pages/warranty/List.tsx +++ b/src/pages/warranty/List.tsx @@ -7,8 +7,9 @@ import PaginationUi from '@/components/PaginationUi' import Td from '@/components/Td' import TrashWithConfrim from '@/components/TrashWithConfrim' import Button from '@/components/Button' -import { useNavigate } from 'react-router-dom' +import { Link, useNavigate } from 'react-router-dom' import { Pages } from '@/config/Pages' +import { Edit } from 'iconsax-react' const WarrantyList: FC = () => { @@ -90,11 +91,16 @@ const WarrantyList: FC = () => { -
- handleDeleteWarranty(warranty._id.toString())} - isLoading={deleteWarrantyMutation.isPending} - /> +
+ + + +
+ handleDeleteWarranty(warranty._id.toString())} + isLoading={deleteWarrantyMutation.isPending} + /> +
diff --git a/src/pages/warranty/Update.tsx b/src/pages/warranty/Update.tsx new file mode 100644 index 0000000..3217398 --- /dev/null +++ b/src/pages/warranty/Update.tsx @@ -0,0 +1,146 @@ +import { type FC, useState, useEffect } from 'react' +import { useParams, useNavigate } from 'react-router-dom' +import { useFormik } from 'formik' +import { type CreateWarrantyType } from './types/Types' +import * as Yup from 'yup' +import { useGetWarrantyById, useUpdateWarranty } from './hooks/useWarrantyData' +import Input from '../../components/Input' +import Button from '../../components/Button' +import { TickCircle } from 'iconsax-react' +import { useUploadSingle } from '../uploader/hooks/useUploaderData' +import UploadBoxDraggble from '@/components/UploadBoxDraggble' +import { toast } from 'react-toastify' +import { extractErrorMessage } from '@/helpers/utils' +import { Pages } from '@/config/Pages' + +const UpdateWarranty: FC = () => { + const navigate = useNavigate() + const { id } = useParams<{ id: string }>() + const uploadSingle = useUploadSingle() + const { data: warrantyDetail, isLoading: warrantyDetailLoading, error: warrantyDetailError } = useGetWarrantyById(id!) + const warranty = warrantyDetail?.results.warranty + const updateWarrantyMutation = useUpdateWarranty() + + const [logoFile, setLogoFile] = useState([]) + + const formik = useFormik({ + initialValues: { + name: '', + duration: '', + logoUrl: '', + }, + validationSchema: Yup.object({ + name: Yup.string().required('عنوان گارانتی الزامی است'), + duration: Yup.string().required('مدت زمان گارانتی الزامی است'), + }), + onSubmit: async (values) => { + let logoUrl = values.logoUrl + + if (logoFile.length > 0) { + const logoResponse = await uploadSingle.mutateAsync(logoFile[0]) + logoUrl = logoResponse.results?.url?.url || '' + } + + const submitData = { + ...values, + logoUrl + } + + updateWarrantyMutation.mutate({ id: id!, params: submitData }, { + onSuccess: () => { + toast.success('گارانتی با موفقیت بروزرسانی شد') + navigate(Pages.products.warranty.list) + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + } + }) + }, + }) + + useEffect(() => { + if (warranty) { + formik.setValues({ + name: warranty.name, + duration: warranty.duration, + logoUrl: warranty.logoUrl, + }) + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [warranty]) + + if (warrantyDetailLoading) { + return
در حال بارگذاری...
+ } + + if (warrantyDetailError) { + return
خطا در بارگذاری داده‌ها
+ } + + return ( +
+
+
+

ویرایش گارانتی

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

اطلاعات اصلی

+ +
+
+ + +
+
+
+ + {/* فایل‌ها */} +
+
+

فایل‌ها

+ +
+
+ setLogoFile(files)} + preview={warranty?.logoUrl ? [warranty.logoUrl] : []} + /> +
+
+
+
+
+
+ ) +} + +export default UpdateWarranty \ No newline at end of file diff --git a/src/pages/warranty/hooks/useWarrantyData.ts b/src/pages/warranty/hooks/useWarrantyData.ts index 5912b35..08fd38f 100644 --- a/src/pages/warranty/hooks/useWarrantyData.ts +++ b/src/pages/warranty/hooks/useWarrantyData.ts @@ -1,6 +1,10 @@ import { useMutation, useQuery } from "@tanstack/react-query"; import * as api from "../service/WarrantyService"; -import type { WarrantyResponse } from "../types/Types"; +import type { + CreateWarrantyType, + WarrantyResponse, + GetWarrantyResponse, +} from "../types/Types"; export const useGetWarranties = (page: number = 1, limit: number = 10) => { return useQuery({ @@ -20,3 +24,17 @@ export const useCreateWarranty = () => { mutationFn: api.createWarranty, }); }; + +export const useGetWarrantyById = (id: string) => { + return useQuery({ + queryKey: ["warranty", id], + queryFn: () => api.getWarrantyById(id), + }); +}; + +export const useUpdateWarranty = () => { + return useMutation({ + mutationFn: ({ id, params }: { id: string; params: CreateWarrantyType }) => + api.updateWarranty(id, params), + }); +}; diff --git a/src/pages/warranty/service/WarrantyService.ts b/src/pages/warranty/service/WarrantyService.ts index 2f6390f..a906317 100644 --- a/src/pages/warranty/service/WarrantyService.ts +++ b/src/pages/warranty/service/WarrantyService.ts @@ -2,6 +2,7 @@ import axios from "../../../config/axios"; import type { GetWarrantiesParams, WarrantyResponse, + GetWarrantyResponse, CreateWarrantyType, } from "../types/Types"; @@ -21,3 +22,18 @@ export const createWarranty = async (params: CreateWarrantyType) => { const { data } = await axios.post(`/admin/warranty`, params); return data; }; + +export const getWarrantyById = async ( + id: string +): Promise => { + const { data } = await axios.get(`/admin/warranty/${id}`); + return data; +}; + +export const updateWarranty = async ( + id: string, + params: CreateWarrantyType +) => { + const { data } = await axios.patch(`/admin/warranty/${id}`, params); + return data; +}; diff --git a/src/pages/warranty/types/Types.ts b/src/pages/warranty/types/Types.ts index e6f481c..3cb90ec 100644 --- a/src/pages/warranty/types/Types.ts +++ b/src/pages/warranty/types/Types.ts @@ -29,6 +29,14 @@ export interface WarrantyResponse { results: WarrantyResults; } +export interface GetWarrantyResponse { + status: number; + success: boolean; + results: { + warranty: Warranty; + }; +} + export interface GetWarrantiesParams { page?: number; limit?: number; diff --git a/src/router/Main.tsx b/src/router/Main.tsx index 69739a8..4c2b4a1 100644 --- a/src/router/Main.tsx +++ b/src/router/Main.tsx @@ -36,6 +36,7 @@ import UsersList from '@/pages/user/List' import AdminList from '@/pages/admin/List' import AdminCreate from '@/pages/admin/Create' import UpdateBrand from '@/pages/brand/Update' +import UpdateWarranty from '@/pages/warranty/Update' const MainRouter: FC = () => { @@ -68,6 +69,7 @@ const MainRouter: FC = () => { } /> } /> } /> + } /> } /> } />