import { getFiles, uploadFiles } from '@/api/Admin' import { get } from '@/rtk/helpers/apiService' import { getAccessToken } from '@/rtk/localStorage' import { Box, Button, Card, Collapse, Group, Modal, Stack, Text, TextInput, Title, } from '@mantine/core' import { notifications } from '@mantine/notifications' import { IconChevronDown, IconDownload, IconFileTypeDocx, IconFileTypePdf, IconFolder, IconListCheck, IconPhoto, IconSearch, IconTrash, } from '@tabler/icons-react' import axios from 'axios' import { useEffect, useState } from 'react' import FileUploadForm from '../Profile/components/FileUploadForm' import classes from './AllProfiles.module.css' interface FileData { id: number name: string url: string type: string description?: string created_at: string } interface GroupedFiles { [key: string]: FileData[] } const AllProfiles = () => { const [groupedFiles, setGroupedFiles] = useState({}) const [currentUser, setCurrentUser] = useState('') const [openedProfile, setOpenedProfile] = useState(false) const [selectedFile, setSelectedFile] = useState(null) const [isLoading, setIsLoading] = useState(false) const [expandedFolders, setExpandedFolders] = useState<{ [key: string]: boolean }>({}) const [searchTerms, setSearchTerms] = useState<{ [key: string]: string }>({}) const toggleFolder = (userName: string) => { setExpandedFolders((prev) => ({ ...prev, [userName]: !prev[userName], })) } const getFileIcon = (type: string) => { switch (type) { case 'document': return case 'image': return default: return } } const handleSubmit = async ( e: React.FormEvent, fileName: string, description: string, currentUser: string ) => { e.preventDefault() setIsLoading(true) const formData = new FormData() if (selectedFile) { formData.append('file', selectedFile) formData.append('name', fileName) formData.append('description', description) formData.append('user_name', currentUser) try { const token = await getAccessToken() const response = await axios.post(uploadFiles, formData, { headers: { 'Content-Type': 'multipart/form-data', Authorization: `Bearer ${token}`, }, }) if (response.status === 200) { setSelectedFile(null) await getAllFiles() return true } return false } catch (error) { console.error('Error uploading file:', error) throw error } finally { setIsLoading(false) } } return false } const getAllFiles = async () => { try { const res = await get(getFiles) if (res.status === true) { setGroupedFiles(res.data) } } catch (error) { console.log(error) } } const removeFile = async (id: number) => { try { const token = await getAccessToken(); const response = await axios.delete(`${import.meta.env.VITE_BACKEND_URL}api/v1/admin/profile/files/${id}`, { headers: { Authorization: `Bearer ${token}`, }, }); if (response.status === 200) { notifications.show({ title: 'Thành công', message: 'Xóa file thành công', color: 'green', }); await getAllFiles(); } } catch (error) { console.log(error); notifications.show({ title: 'Lỗi', message: 'Không thể xóa file', color: 'red', }); } } useEffect(() => { getAllFiles() }, []) const filterFiles = (files: FileData[], searchTerm: string) => { return files.filter( (file) => file.name.toLowerCase().includes(searchTerm.toLowerCase()) || (file.description && file.description.toLowerCase().includes(searchTerm.toLowerCase())), ) } return (

Admin/ Files Management

{Object.entries(groupedFiles).map(([userName, files]) => ( toggleFolder(userName)} style={{ cursor: 'pointer' }} > {userName} } value={searchTerms[userName] || ''} onChange={(e) => setSearchTerms((prev) => ({ ...prev, [userName]: e.target.value, })) } onClick={(e) => e.stopPropagation()} /> {filterFiles(files, searchTerms[userName] || '') .sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) .map((file: FileData) => ( {getFileIcon(file.type)} {file.name} {file.description && ( {file.description} )} Uploaded:{' '} {new Date(file.created_at).toLocaleDateString()} ), )} ))} { setOpenedProfile(false) setCurrentUser('') setSelectedFile(null) }} > file && setSelectedFile(file)} removeFile={removeFile} isLoading={isLoading} currentUser={currentUser} />
) } export default AllProfiles