diff --git a/src/components/ui/FileUploader.tsx b/src/components/ui/FileUploader.tsx index 6912961..45befcb 100644 --- a/src/components/ui/FileUploader.tsx +++ b/src/components/ui/FileUploader.tsx @@ -1,4 +1,4 @@ -import React, { useState, useRef, useCallback } from 'react'; +import React, { useState, useRef, useCallback, useEffect } from 'react'; import { Upload, X, Image, File, AlertCircle, CheckCircle } from 'lucide-react'; import { Button } from './Button'; @@ -18,7 +18,7 @@ interface FileUploaderProps { onUpload: (file: File) => Promise<{ id: string; url: string }>; onRemove?: (fileId: string) => void; acceptedTypes?: string[]; - maxFileSize?: number; // در بایت + maxFileSize?: number; maxFiles?: number; label?: string; description?: string; @@ -28,13 +28,14 @@ interface FileUploaderProps { mode?: 'single' | 'multi'; onUploadStart?: () => void; onUploadComplete?: () => void; + initialFiles?: Array & { id: string; url?: string }>; } export const FileUploader: React.FC = ({ onUpload, onRemove, acceptedTypes = ['image/*', 'video/*'], - maxFileSize = 10 * 1024 * 1024, // 10MB + maxFileSize = 10 * 1024 * 1024, maxFiles = 10, label = "فایل‌ها", description = "تصاویر و ویدیوها را اینجا بکشید یا کلیک کنید", @@ -44,6 +45,7 @@ export const FileUploader: React.FC = ({ mode = 'multi', onUploadStart, onUploadComplete, + initialFiles = [], }) => { const [files, setFiles] = useState([]); const [isDragOver, setIsDragOver] = useState(false); @@ -59,6 +61,22 @@ export const FileUploader: React.FC = ({ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; + useEffect(() => { + if (initialFiles && initialFiles.length > 0) { + const normalized: UploadedFile[] = initialFiles.map((f) => ({ + id: f.id, + name: f.name || (f.url ? f.url.split('/').pop() || 'file' : 'file'), + size: typeof f.size === 'number' ? f.size : 0, + type: f.type || 'image/*', + url: f.url, + preview: f.preview, + progress: 100, + status: 'completed', + })); + setFiles(mode === 'single' ? [normalized[0]] : normalized); + } + }, [initialFiles, mode]); + const validateFile = (file: File) => { if (maxFileSize && file.size > maxFileSize) { return `حجم فایل نباید بیشتر از ${formatFileSize(maxFileSize)} باشد`; @@ -202,7 +220,6 @@ export const FileUploader: React.FC = ({ )} - {/* Upload Area - only show in multi mode or single mode without uploaded files */} {showUploadArea && (
= ({

)} - {/* Files List */} {files.length > 0 && (

@@ -259,11 +275,10 @@ export const FileUploader: React.FC = ({ key={file.id} className="flex items-center gap-3 p-3 bg-gray-50 dark:bg-gray-700 rounded-lg" > - {/* File Icon/Preview */}
- {file.preview ? ( + {(file.preview || file.url) ? ( {file.name} @@ -278,7 +293,6 @@ export const FileUploader: React.FC = ({ )}
- {/* File Info */}

{file.name} @@ -287,7 +301,6 @@ export const FileUploader: React.FC = ({ {formatFileSize(file.size)}

- {/* Progress Bar */} {file.status === 'uploading' && (
@@ -300,7 +313,6 @@ export const FileUploader: React.FC = ({
)} - {/* Error Message */} {file.status === 'error' && file.error && (

@@ -309,7 +321,6 @@ export const FileUploader: React.FC = ({ )}

- {/* Status & Actions */}
{file.status === 'completed' && ( diff --git a/src/pages/categories/category-form/CategoryFormPage.tsx b/src/pages/categories/category-form/CategoryFormPage.tsx index 5623e6d..f9bfaf8 100644 --- a/src/pages/categories/category-form/CategoryFormPage.tsx +++ b/src/pages/categories/category-form/CategoryFormPage.tsx @@ -38,19 +38,22 @@ const CategoryFormPage = () => { useEffect(() => { if (category && isEdit) { + const fileId = (category as any).file?.id ?? category.file_id; + const fileUrl = (category as any).file?.url || ''; + setFormData({ name: category.name || '', description: category.description || '', - parent_id: category.parent_id || null, - file_id: category.file_id || undefined, + parent_id: (category as any).parent_id || null, + file_id: fileId || undefined, }); - // Set uploaded image if exists - if (category.file_id) { - setUploadedImage({ - id: category.file_id.toString(), - url: '' // We don't have URL from category, just ID - }); + if (fileId && fileUrl) { + setUploadedImage({ id: String(fileId), url: fileUrl }); + } else if (fileId) { + setUploadedImage({ id: String(fileId), url: '' }); + } else { + setUploadedImage(null); } } }, [category, isEdit]); @@ -138,7 +141,6 @@ const CategoryFormPage = () => { backButton={backButton} /> - {/* Form */}
@@ -170,17 +172,17 @@ const CategoryFormPage = () => { onUpload={handleFileUpload} onRemove={handleFileRemove} acceptedTypes={['image/*']} - maxFileSize={5 * 1024 * 1024} // 5MB + maxFileSize={5 * 1024 * 1024} maxFiles={1} mode="single" label="تصویر دسته‌بندی" description="تصویر دسته‌بندی را انتخاب کنید (حداکثر 5MB)" onUploadStart={() => setIsUploading(true)} onUploadComplete={() => setIsUploading(false)} + initialFiles={uploadedImage ? [{ id: uploadedImage.id, url: uploadedImage.url }] : []} />
- {/* Actions */}