diff --git a/.env b/.env index 7fb068d..ec31907 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -VITE_API_BASE_URL = 'http://10.241.118.88:4000' +VITE_API_BASE_URL = 'http://172.27.69.89: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 1f30045..f573876 100644 --- a/src/config/Paths.tsx +++ b/src/config/Paths.tsx @@ -9,6 +9,11 @@ export const Paths = { create: '/product/category/create', list: '/product/category/list', update: '/product/category/update/' + }, + attribute: { + list: '/attribute/list', + create: '/attribute/create', + update: '/attribute/update' } }, order: { diff --git a/src/pages/product/Create.tsx b/src/pages/product/Create.tsx index d379d78..693159d 100644 --- a/src/pages/product/Create.tsx +++ b/src/pages/product/Create.tsx @@ -1,13 +1,87 @@ import Button from '@/components/Button' -import GridWrapper from '@/components/GridWrapper'; import Input from '@/components/Input'; -import { Checkbox } from '@/components/ui/checkbox'; -import UploadBox from '@/components/UploadBox'; import { clx } from '@/helpers/utils'; +import { useFormik } from 'formik'; import { AddSquare } from 'iconsax-react' -import { type FC } from 'react' +import { useState, type FC } from 'react' +import type { CreateProductType } from './types/Types'; +import * as Yup from 'yup' +import Textarea from '@/components/Textarea'; +import CategoriesSelect from './components/CategoriesSelect'; +import SwitchComponent from '@/components/Switch'; +import UploadBoxDraggble from '@/components/UploadBoxDraggble'; +import { useMultiUpload } from '../uploader/hooks/useUploader'; +import { useCreateProduct } from './hooks/useProductData'; +import { toast } from 'react-toastify'; +import type { ErrorType } from '@/helpers/types'; +import { extractErrorMessage } from '@/config/func'; +import { useNavigate } from 'react-router-dom'; const CreateProduct: FC = () => { + + const navigate = useNavigate() + const [files, setFiles] = useState([]) + const { mutate: upload, isPending: isUploading } = useMultiUpload() + const { mutate: createProduct, isPending } = useCreateProduct() + + const handleSave = (values: CreateProductType) => { + values.categoryId = Number(values.categoryId) + createProduct(values, { + onSuccess: () => { + toast.success('عملیات با موفقیت انجام شد') + formik.resetForm() + navigate(-1) + }, + onError: (error: ErrorType) => { + toast.error(extractErrorMessage(error)) + } + }) + } + + const formik = useFormik({ + initialValues: { + categoryId: undefined, + desc: '', + images: [], + isActive: true, + linkUrl: '', + order: 1, + quantities: [], + title: '' + }, + validationSchema: Yup.object({ + categoryId: Yup.string().required('این فیلد اجباری می باشد.'), + title: Yup.string().required('این فیلد اجباری می باشد.'), + }), + onSubmit: (values) => { + if (files.length > 0) { + upload(files, { + onSuccess: (data) => { + const imagesUrl: string[] = [] + data.data?.map((item) => { + imagesUrl.push(item.url) + }) + values.images = imagesUrl + handleSave(values) + } + }) + } else { + handleSave(values) + } + } + }) + + const handleQuantities = (value: number) => { + const array = [...formik.values.quantities] + const index: number = array.findIndex(o => o === value) + if (index > -1) { + array.splice(index, 1) + } else { + array.push(value) + } + formik.setFieldValue('quantities', array) + } + return (
@@ -15,6 +89,8 @@ const CreateProduct: FC = () => {

محصول جدید

-
-
- اطلاعات محصول +
+
+
+ اطلاعات محصول +
+ +
+ + +
+ +
+ + + +
+ +
+