diff --git a/src/pages/product/Update.tsx b/src/pages/product/Update.tsx new file mode 100644 index 0000000..73ab5bb --- /dev/null +++ b/src/pages/product/Update.tsx @@ -0,0 +1,261 @@ +import Button from '@/components/Button' +import Input from '@/components/Input'; +import { clx } from '@/helpers/utils'; +import { useFormik } from 'formik'; +import { AddSquare } from 'iconsax-react' +import { useEffect, 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 { useGetProductDetail, useUpdateProduct } from './hooks/useProductData'; +import { toast } from 'react-toastify'; +import type { ErrorType } from '@/helpers/types'; +import { extractErrorMessage } from '@/config/func'; +import { useNavigate, useParams } from 'react-router-dom'; + +const UpdateProduct: FC = () => { + + const navigate = useNavigate() + const { id } = useParams() + const [files, setFiles] = useState([]) + const { data: product, refetch } = useGetProductDetail(Number(id)) + const { mutate: upload, isPending: isUploading } = useMultiUpload() + const { mutate: updateProduct, isPending } = useUpdateProduct() + + const handleSave = (values: CreateProductType) => { + values.categoryId = Number(values.categoryId) + updateProduct({ id: Number(id), params: values }, { + onSuccess: () => { + toast.success('عملیات با موفقیت انجام شد') + refetch() + 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[] = [...values.images] + 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) + } + + useEffect(() => { + + if (product) { + formik.setValues({ + title: product?.data?.title, + desc: product?.data?.title, + images: product?.data?.images, + isActive: product?.data?.isActive, + linkUrl: product?.data?.linkUrl, + order: product?.data?.order, + quantities: product?.data?.quantities, + categoryId: product?.data?.category?.id + }) + } + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [product]) + + + return ( +
+
+ +

ویرایش جدید

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