diff --git a/.env b/.env index ec31907..86a5253 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -VITE_API_BASE_URL = 'http://172.27.69.89:4000' +VITE_API_BASE_URL = 'http://192.168.99.235:4000' VITE_TOKEN_NAME = 'negareh_t' VITE_REFRESH_TOKEN_NAME = 'negareh_rt' \ No newline at end of file diff --git a/src/config/Paths.tsx b/src/config/Paths.tsx index 8613d45..579d020 100644 --- a/src/config/Paths.tsx +++ b/src/config/Paths.tsx @@ -12,9 +12,9 @@ export const Paths = { update: '/product/category/update/' }, attribute: { - list: '/attribute/list', - create: '/attribute/create', - update: '/attribute/update' + list: '/product/attribute/', + create: '/product/attribute/create/', + update: '/product/attribute/update/' } }, order: { diff --git a/src/pages/product/List.tsx b/src/pages/product/List.tsx index 472c05d..e512b96 100644 --- a/src/pages/product/List.tsx +++ b/src/pages/product/List.tsx @@ -73,12 +73,12 @@ const ProductList: FC = () => { { key: 'attribute', title: 'ویژگی ها', - render: () => { + render: (item) => { return ( -
+
ویژگی ها
-
+ ) } }, diff --git a/src/pages/product/attribute/Create.tsx b/src/pages/product/attribute/Create.tsx index c0dd89a..82c3328 100644 --- a/src/pages/product/attribute/Create.tsx +++ b/src/pages/product/attribute/Create.tsx @@ -1,8 +1,126 @@ -import React from 'react' +import { useFormik } from 'formik' +import { type FC } from 'react' +import type { CreateAttributeType } from '../types/Types' +import { FieldTypeEnum } from '../enum/Enum' +import * as Yup from 'yup' +import Button from '@/components/Button' +import { AddSquare } from 'iconsax-react' +import Input from '@/components/Input' +import Select from '@/components/Select' +import SwitchComponent from '@/components/Switch' +import { useCreateAttribute } from '../hooks/useProductData' +import { useNavigate, useParams } from 'react-router-dom' +import { toast } from 'react-toastify' +import { extractErrorMessage } from '@/config/func' +import { Paths } from '@/config/Paths' + +const CreateAttribute: FC = () => { + + const { id } = useParams() + const navigate = useNavigate() + const { mutate: createAttribute, isPending } = useCreateAttribute() + + const formik = useFormik({ + initialValues: { + name: '', + isRequired: true, + order: 1, + type: FieldTypeEnum.text, + }, + validationSchema: Yup.object({ + name: Yup.string().required('این فیلد اجباری می باشد') + }), + onSubmit: (values) => { + createAttribute({ id: Number(id), params: values }, { + onSuccess: () => { + toast.success('با موفقیت ذخیره شد') + navigate(Paths.product.attribute.list + id) + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + } + }) + } + }) -const CreateAttribute = () => { return ( -
CreateAttribute
+
+
+ +

ساخت ویژگی جدید

+ +
+ +
+
+ + + +
+ +
+ formik.setFieldValue('isRequired', value)} + label='اجباری' + /> +
+
+ +
) } diff --git a/src/pages/product/enum/Enum.ts b/src/pages/product/enum/Enum.ts new file mode 100644 index 0000000..d2330c1 --- /dev/null +++ b/src/pages/product/enum/Enum.ts @@ -0,0 +1,9 @@ +export const enum FieldTypeEnum { + text = "text", + textarea = "textarea", + number = "number", + select = "select", + radio = "radio", + checkbox = "checkbox", + date = "date", +} diff --git a/src/pages/product/hooks/useProductData.ts b/src/pages/product/hooks/useProductData.ts index d645611..32c3896 100644 --- a/src/pages/product/hooks/useProductData.ts +++ b/src/pages/product/hooks/useProductData.ts @@ -5,7 +5,11 @@ import { useQueryClient, } from "@tanstack/react-query"; import * as api from "../service/ProductService"; -import type { CreateCategoryType, CreateProductType } from "../types/Types"; +import type { + CreateAttributeType, + CreateCategoryType, + CreateProductType, +} from "../types/Types"; export const useGetCategory = () => { return useQuery({ @@ -86,3 +90,10 @@ export const useDeleteProduct = () => { mutationFn: (id: number) => api.deleteProduct(id), }); }; + +export const useCreateAttribute = () => { + return useMutation({ + mutationFn: ({ id, params }: { id: number; params: CreateAttributeType }) => + api.createAttribute(id, params), + }); +}; diff --git a/src/pages/product/service/ProductService.ts b/src/pages/product/service/ProductService.ts index 77f65ef..9510a8c 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 CategoriesResponse, type CategoryResponse, type CreateCategoryType, type CreateProductType, type ProductDetailResponeType, type ProductResponeType } from "../types/Types"; +import { type CategoriesResponse, type CategoryResponse, type CreateAttributeType, type CreateCategoryType, type CreateProductType, type ProductDetailResponeType, type ProductResponeType } from "../types/Types"; export const getCategory = async () => { const { data } = await axios.get("/admin/category"); @@ -49,4 +49,9 @@ export const updateProduct = async (id: number, params:CreateProductType) => { export const deleteProduct = async (id: number) => { const { data } = await axios.delete(`/admin/products/${id}`); return data; +}; + +export const createAttribute = async (id: number, params:CreateAttributeType) => { + const { data } = await axios.post(`/admin/product/${id}/attribute`, 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 3866941..08cb71d 100644 --- a/src/pages/product/types/Types.ts +++ b/src/pages/product/types/Types.ts @@ -1,5 +1,6 @@ import type { RowDataType } from "@/components/types/TableTypes"; import { type BaseResponse } from "@/shared/types/Types"; +import type { FieldTypeEnum } from "../enum/Enum"; export interface CategoryType extends RowDataType { avatarUrl: string | null; @@ -50,3 +51,10 @@ export type ProductType = { export type ProductResponeType = BaseResponse; export type ProductDetailResponeType = BaseResponse; + +export type CreateAttributeType = { + name: string, + isRequired: boolean, + type: FieldTypeEnum, + order: number +} \ No newline at end of file diff --git a/src/router/MainRouter.tsx b/src/router/MainRouter.tsx index 2d624c1..40b6d4a 100644 --- a/src/router/MainRouter.tsx +++ b/src/router/MainRouter.tsx @@ -29,6 +29,7 @@ import CategoryList from '@/pages/product/category/List' import CreateCategory from '@/pages/product/category/Create' import UpdateCategory from '@/pages/product/category/Update' import UpdateProduct from '@/pages/product/Update' +import CreateAttribute from '@/pages/product/attribute/Create' const MainRouter: FC = () => { return ( @@ -49,6 +50,7 @@ const MainRouter: FC = () => { } /> } /> } /> + } /> } /> } /> } />