From dd63abd212609a634df344123ff4c27fe65f7f21 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 14 Jun 2026 09:34:32 +0330 Subject: [PATCH] upload single icon --- src/pages/product/components/CategoryForm.tsx | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/pages/product/components/CategoryForm.tsx b/src/pages/product/components/CategoryForm.tsx index 63d49ed..cb516c2 100644 --- a/src/pages/product/components/CategoryForm.tsx +++ b/src/pages/product/components/CategoryForm.tsx @@ -1,5 +1,6 @@ -import { type FC, useState } from 'react' -import { Add, Image } from 'iconsax-react' +import { type ChangeEvent, type FC, useRef, useState } from 'react' +import { Add, DocumentUpload, Image } from 'iconsax-react' +import { toast } from 'react-toastify' import Input from '@/components/Input' import SwitchComponent from '@/components/Switch' import Button from '@/components/Button' @@ -7,6 +8,9 @@ import type { FormikProps } from 'formik' import { useGetCategories, useGetIcons } from '../hooks/useProductData' import IconSelectModal from './IconSelectModal' import Select from '@/components/Select' +import { useSingleUpload } from '../../uploader/hooks/useUploaderData' +import { extractErrorMessage } from '@/config/func' +import type { ErrorType } from '@/helpers/types' type CategoryFormValues = { title: string @@ -41,6 +45,8 @@ const CategoryForm: FC = ({ const { data: categories } = useGetCategories() const icons = iconsData?.data || [] const [isIconModalOpen, setIsIconModalOpen] = useState(false) + const fileInputRef = useRef(null) + const { mutate: singleUpload, isPending: isUploadingIcon } = useSingleUpload() const handleIconSelect = (url: string) => { if (onIconUrlChange) { @@ -49,6 +55,30 @@ const CategoryForm: FC = ({ setIsIconModalOpen(false) } + const handleUploadClick = () => { + fileInputRef.current?.click() + } + + const handleFileChange = (e: ChangeEvent) => { + const file = e.target.files?.[0] + if (!file) return + + singleUpload(file, { + onSuccess: (response) => { + const url = response?.data?.url || '' + if (url && onIconUrlChange) { + onIconUrlChange(url) + toast.success('آیکون با موفقیت آپلود شد') + } + }, + onError: (error: ErrorType) => { + toast.error(extractErrorMessage(error, 'خطا در آپلود آیکون')) + } + }) + + e.target.value = '' + } + return (

{isEditing ? 'ویرایش دسته بندی' : 'اضافه کردن دسته بندی'}

@@ -105,6 +135,22 @@ const CategoryForm: FC = ({ انتخاب آیکون + + {selectedIconUrl && (
@@ -154,7 +200,7 @@ const CategoryForm: FC = ({