From 375ea9d669581c36ff3e219b7077f7ed5f65e048 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 25 Jan 2026 12:02:23 +0330 Subject: [PATCH] update value --- .../product/attribute/AttributeValues.tsx | 10 +- src/pages/product/components/UpdateValue.tsx | 110 ++++++++++++++++++ src/pages/product/hooks/useProductData.ts | 20 ++++ src/pages/product/service/ProductService.ts | 12 +- src/pages/product/types/Types.ts | 1 + 5 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/pages/product/components/UpdateValue.tsx diff --git a/src/pages/product/attribute/AttributeValues.tsx b/src/pages/product/attribute/AttributeValues.tsx index 680b640..6eb0949 100644 --- a/src/pages/product/attribute/AttributeValues.tsx +++ b/src/pages/product/attribute/AttributeValues.tsx @@ -3,7 +3,7 @@ import { useParams } from 'react-router-dom' import { useGetAttributeValues } from '../hooks/useProductData' import CreateValue from '../components/CreateValue' import Table from '@/components/Table' -import { Edit } from 'iconsax-react' +import UpdateValue from '../components/UpdateValue' const AttributeValues: FC = () => { @@ -35,12 +35,12 @@ const AttributeValues: FC = () => { { key: 'actions', title: '', - render: () => { + render: (item) => { return (
-
) diff --git a/src/pages/product/components/UpdateValue.tsx b/src/pages/product/components/UpdateValue.tsx new file mode 100644 index 0000000..e534ccd --- /dev/null +++ b/src/pages/product/components/UpdateValue.tsx @@ -0,0 +1,110 @@ +import { useEffect, useState, type FC } from 'react' +import Button from '@/components/Button' +import { Edit, TickCircle } from 'iconsax-react' +import DefaulModal from '@/components/DefaulModal' +import { useFormik } from 'formik' +import type { CreateAttributeValueType } from '../types/Types' +import * as Yup from 'yup' +import Input from '@/components/Input' +import { useGetAttributeValueDetail, useUpdateAttributeValue } from '../hooks/useProductData' +import { extractErrorMessage } from '@/config/func' +import { toast } from 'react-toastify' + +type Props = { + id: number, + refetch: () => void, +} + +const UpdateValue: FC = ({ id, refetch }) => { + + + const [showModal, setShowModal] = useState(false) + const { mutate: updateValue } = useUpdateAttributeValue() + const { data, refetch: refetchDeetail } = useGetAttributeValueDetail(id, showModal) + + const formik = useFormik({ + initialValues: { + value: '', + order: 1 + }, + validationSchema: Yup.object({ + value: Yup.string().required('این فیلد اجباری است') + }), + onSubmit: (values) => { + updateValue({ id: id, params: values }, { + onSuccess: () => { + refetch() + refetchDeetail() + setShowModal(false) + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + } + + }) + } + }) + + useEffect(() => { + + if (data) { + formik.setValues({ + value: data?.data.value, + order: data?.data.order + }) + } + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [data]) + + + return ( +
+ setShowModal(true)} + size={20} + color='#0037FF' + /> + + setShowModal(false)} + isHeader + title_header='ویرایش مقدار' + > +
+ +
+ +
+ +
+ +
+ +
+ +
+
+ ) +} + +export default UpdateValue \ No newline at end of file diff --git a/src/pages/product/hooks/useProductData.ts b/src/pages/product/hooks/useProductData.ts index 87a3f7f..ff45794 100644 --- a/src/pages/product/hooks/useProductData.ts +++ b/src/pages/product/hooks/useProductData.ts @@ -160,3 +160,23 @@ export const useCreateAttributeValue = () => { }) => api.createAttributeValues(id, params), }); }; + +export const useUpdateAttributeValue = () => { + return useMutation({ + mutationFn: ({ + id, + params, + }: { + id: number; + params: CreateAttributeValueType; + }) => api.updateAttributeValues(id, params), + }); +}; + +export const useGetAttributeValueDetail = (id: number, enabled: boolean) => { + return useQuery({ + queryKey: ["attrbute-values-detail", id], + queryFn: () => api.getAttributeValueDetail(id), + enabled: !!id && enabled, + }); +}; diff --git a/src/pages/product/service/ProductService.ts b/src/pages/product/service/ProductService.ts index ece3353..ef629a6 100644 --- a/src/pages/product/service/ProductService.ts +++ b/src/pages/product/service/ProductService.ts @@ -1,5 +1,5 @@ import axios from "@/config/axios"; -import { type AttributeDetailResponseType, type AttributeResponseType, type AttributeValuesResponseType, type CategoriesResponse, type CategoryResponse, type CreateAttributeType, type CreateAttributeValueType, type CreateCategoryType, type CreateProductType, type ProductDetailResponeType, type ProductResponeType } from "../types/Types"; +import { type AttributeDetailResponseType, type AttributeResponseType, type AttributeValueDetailResponseType, type AttributeValuesResponseType, type CategoriesResponse, type CategoryResponse, type CreateAttributeType, type CreateAttributeValueType, type CreateCategoryType, type CreateProductType, type ProductDetailResponeType, type ProductResponeType } from "../types/Types"; export const getCategory = async () => { const { data } = await axios.get("/admin/category"); @@ -84,4 +84,14 @@ export const getAttributeValues = async (id: number) => { export const createAttributeValues = async (id: number, params:CreateAttributeValueType) => { const { data } = await axios.post(`/admin/product/attribute/${id}/value`, params); return data; +}; + +export const updateAttributeValues = async (id: number, params:CreateAttributeValueType) => { + const { data } = await axios.patch(`/admin/products/attributes/value/${id}`, params); + return data; +}; + +export const getAttributeValueDetail = async (id: number) => { + const { data } = await axios.get(`/admin/products/attributes/value/${id}`); + return data; }; \ No newline at end of file diff --git a/src/pages/product/types/Types.ts b/src/pages/product/types/Types.ts index 58c093d..21d03a3 100644 --- a/src/pages/product/types/Types.ts +++ b/src/pages/product/types/Types.ts @@ -85,3 +85,4 @@ export type AttributeValueType = { } export type AttributeValuesResponseType = BaseResponse; +export type AttributeValueDetailResponseType = BaseResponse;