import { Box, Button, Card, FileInput, Group, Stack, Text, TextInput, Textarea, } from '@mantine/core' import { notifications } from '@mantine/notifications' import { IconDownload, IconFileTypeDocx, IconFileTypePdf, IconFileTypeXls, IconPhoto, IconSearch, IconTrash, } from '@tabler/icons-react' import { useState } from 'react' import classes from './FileUploadForm.module.css' // type TFileProfile = { // label: string // type: string // value: string // children?: TFileProfile[] // } interface FileData { id: number name: string url: string type: string description?: string created_at: string } type FileUploadFormProps = { data: FileData[]; handleSubmit: (e: React.FormEvent, fileName: string, description: string, currentUser: string) => Promise; handleFileChange: (file: File | null) => void; removeFile: (id: number) => Promise; isLoading: boolean; currentUser: string; }; const FileUploadForm = ({ data, handleSubmit, handleFileChange, removeFile, isLoading, currentUser, }: FileUploadFormProps) => { const [selectedFile, setSelectedFile] = useState(null) const [fileName, setFileName] = useState('') const [description, setDescription] = useState('') const [isUploading, setIsUploading] = useState(false) const [searchTerm, setSearchTerm] = useState('') const handleFileSelect = (file: File | null) => { setSelectedFile(file) handleFileChange(file) if (file) { // Set default name as file name without extension setFileName(file.name.split('.')[0]) } } const handleFormSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsUploading(true) try { await handleSubmit(e, fileName, description, currentUser) notifications.show({ title: 'Thành công', message: 'Tải file lên thành công', color: 'green', }) setFileName('') setDescription('') setSelectedFile(null) } catch (error) { console.error('Error submitting form:', error) notifications.show({ title: 'Lỗi', message: 'Không thể tải file lên', color: 'red', }) } finally { setIsUploading(false) } } const getFileIcon = (type: string) => { switch (type) { case 'document': return case 'image': return case 'spreadsheet': return default: return } } const filteredFiles = data.filter( (file) => file.name.toLowerCase().includes(searchTerm.toLowerCase()) || (file.description && file.description.toLowerCase().includes(searchTerm.toLowerCase())), ) return ( <> {isLoading && (
)}
Tài liệu setFileName(e.target.value)} className={classes.fileNameInput} required />