From d3eab9ed75a09d20aa8bbc4c40c6e9f474423b0b Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 25 Jan 2026 11:43:57 +0330 Subject: [PATCH] create attibute value --- src/config/Paths.tsx | 3 + .../product/attribute/AttributeValues.tsx | 26 +++++ src/pages/product/attribute/List.tsx | 13 +++ src/pages/product/components/CreateValue.tsx | 99 +++++++++++++++++++ src/pages/product/hooks/useProductData.ts | 21 ++++ src/pages/product/service/ProductService.ts | 12 ++- src/pages/product/types/Types.ts | 14 +++ src/router/MainRouter.tsx | 2 + 8 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 src/pages/product/attribute/AttributeValues.tsx create mode 100644 src/pages/product/components/CreateValue.tsx diff --git a/src/config/Paths.tsx b/src/config/Paths.tsx index 579d020..05e58ad 100644 --- a/src/config/Paths.tsx +++ b/src/config/Paths.tsx @@ -15,6 +15,9 @@ export const Paths = { list: '/product/attribute/', create: '/product/attribute/create/', update: '/product/attribute/update/' + }, + attributeValue: { + list: '/product/attribute-value/' } }, order: { diff --git a/src/pages/product/attribute/AttributeValues.tsx b/src/pages/product/attribute/AttributeValues.tsx new file mode 100644 index 0000000..efd2ff1 --- /dev/null +++ b/src/pages/product/attribute/AttributeValues.tsx @@ -0,0 +1,26 @@ +import { type FC } from 'react' +import { useParams } from 'react-router-dom' +import { useGetAttributeValues } from '../hooks/useProductData' +import CreateValue from '../components/CreateValue' + +const AttributeValues: FC = () => { + + const { id } = useParams() + const { data, refetch } = useGetAttributeValues(Number(id)) + + return ( +
+
+

مقادیر

+ + +
+ +
+ ) +} + +export default AttributeValues \ No newline at end of file diff --git a/src/pages/product/attribute/List.tsx b/src/pages/product/attribute/List.tsx index b4e7c8e..cd2137f 100644 --- a/src/pages/product/attribute/List.tsx +++ b/src/pages/product/attribute/List.tsx @@ -66,6 +66,19 @@ const AttributeList: FC = () => { ) } }, + { + key: 'value', + title: 'مقادیر', + render: (item) => { + return ( + + + + ) + } + }, { key: 'actions', title: '', diff --git a/src/pages/product/components/CreateValue.tsx b/src/pages/product/components/CreateValue.tsx new file mode 100644 index 0000000..0787401 --- /dev/null +++ b/src/pages/product/components/CreateValue.tsx @@ -0,0 +1,99 @@ +import { useState, type FC } from 'react' +import Button from '@/components/Button' +import { AddSquare, 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 { useCreateAttributeValue } from '../hooks/useProductData' +import { extractErrorMessage } from '@/config/func' +import { toast } from 'react-toastify' + +type Props = { + id: number, + refetch: () => void, +} + +const CreateValue: FC = ({ id, refetch }) => { + + + const { mutate: createValue } = useCreateAttributeValue() + const [showModal, setShowModal] = useState(false) + + const formik = useFormik({ + initialValues: { + value: '', + order: 1 + }, + validationSchema: Yup.object({ + value: Yup.string().required('این فیلد اجباری است') + }), + onSubmit: (values) => { + createValue({ id: id, params: values }, { + onSuccess: () => { + refetch() + setShowModal(false) + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + } + + }) + } + }) + + return ( +
+ + + setShowModal(false)} + isHeader + title_header='ساخت مقدار جدید' + > +
+ +
+ +
+ +
+ +
+ +
+ +
+
+ ) +} + +export default CreateValue \ No newline at end of file diff --git a/src/pages/product/hooks/useProductData.ts b/src/pages/product/hooks/useProductData.ts index b7502d0..87a3f7f 100644 --- a/src/pages/product/hooks/useProductData.ts +++ b/src/pages/product/hooks/useProductData.ts @@ -7,6 +7,7 @@ import { import * as api from "../service/ProductService"; import type { CreateAttributeType, + CreateAttributeValueType, CreateCategoryType, CreateProductType, } from "../types/Types"; @@ -139,3 +140,23 @@ export const useDeleteAttribute = () => { mutationFn: (id: number) => api.deleteAttributes(id), }); }; + +export const useGetAttributeValues = (id: number) => { + return useQuery({ + queryKey: ["attrbute-values", id], + queryFn: () => api.getAttributeValues(id), + enabled: !!id, + }); +}; + +export const useCreateAttributeValue = () => { + return useMutation({ + mutationFn: ({ + id, + params, + }: { + id: number; + params: CreateAttributeValueType; + }) => api.createAttributeValues(id, params), + }); +}; diff --git a/src/pages/product/service/ProductService.ts b/src/pages/product/service/ProductService.ts index db6259a..ece3353 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 CategoriesResponse, type CategoryResponse, type CreateAttributeType, type CreateCategoryType, type CreateProductType, type ProductDetailResponeType, type ProductResponeType } from "../types/Types"; +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"; export const getCategory = async () => { const { data } = await axios.get("/admin/category"); @@ -74,4 +74,14 @@ export const updateAttribute = async (id: number, params:CreateAttributeType) => export const deleteAttributes = async (id: number) => { const { data } = await axios.delete(`/admin/products/attributes/${id}`); return data; +}; + +export const getAttributeValues = async (id: number) => { + const { data } = await axios.get(`/admin/products/attributes/${id}/values`); + return data; +}; + +export const createAttributeValues = async (id: number, params:CreateAttributeValueType) => { + const { data } = await axios.post(`/admin/product/attribute/${id}/value`, params); + 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 c51ac01..58c093d 100644 --- a/src/pages/product/types/Types.ts +++ b/src/pages/product/types/Types.ts @@ -71,3 +71,17 @@ export type AttributeType = { export type AttributeResponseType = BaseResponse; export type AttributeDetailResponseType = BaseResponse; + +export type CreateAttributeValueType = { + value: string, + order: number, +} + +export type AttributeValueType = { + createdAt: string, + id: number, + order: number, + value: string, +} + +export type AttributeValuesResponseType = BaseResponse; diff --git a/src/router/MainRouter.tsx b/src/router/MainRouter.tsx index d0f33e7..6e0246f 100644 --- a/src/router/MainRouter.tsx +++ b/src/router/MainRouter.tsx @@ -32,6 +32,7 @@ import UpdateProduct from '@/pages/product/Update' import CreateAttribute from '@/pages/product/attribute/Create' import AttributeList from '@/pages/product/attribute/List' import UpdateAttribute from '@/pages/product/attribute/Update' +import AttributeValues from '@/pages/product/attribute/AttributeValues' const MainRouter: FC = () => { return ( @@ -55,6 +56,7 @@ const MainRouter: FC = () => { } /> } /> } /> + } /> } /> } /> } />