+
+
+ {/* Tabs */}
+
+ {tabs.map(({ id, name, icon: Icon }) => (
+
+ ))}
- {/* Tab Navigation */}
-
-
-
+ {error && (
+
+ {error}
+
+ )}
- {/* Content Area */}
-
- {error && (
-
- {error}
+
+ {activeTab === 'documents' && (
+
+
+
+
+
+ | Title |
+ Status |
+ Uploaded By |
+ Shared With |
+ Created At |
+
+
+
+ {documents.map((doc) => (
+
+ | {doc.title} |
+
+
+ {doc.status}
+
+ |
+
+ {doc.uploadedBy?.name} ({doc.uploadedBy?.email})
+ |
+
+ {doc.sharedWith && doc.sharedWith.length > 0
+ ? doc.sharedWith.map(user => (
+
+ {user.name} ({user.email})
+
+ ))
+ : 'None'}
+ |
+
+ {new Date(doc.createdAt).toLocaleString()}
+ |
+
+ ))}
+
+
+
)}
- {loading ? (
-
- ) : (
+ {activeTab === 'users' && (
<>
- {activeTab === 'documents' && (
-
-
-
-
- | Title |
- Status |
- Shared With |
- Created At |
+
+
+
+
+
+
+ | Name |
+ Email |
+ Role |
+
+
+
+ {users.map((user) => (
+
+ | {user.name} |
+ {user.email} |
+
+
+ {user.isAdmin ? 'Admin' : 'User'}
+
+ |
-
-
- {documents.map((doc) => (
-
- | {doc.title} |
-
-
- {doc.status}
-
- |
- {/*
- {doc.sharedWith?.map((user) => user.name).join(', ') || 'None'}
- | */}
-
- {doc.sharedWith ? `${doc.sharedWith.name} (${doc.sharedWith.email})` : 'None'}
- |
-
- {new Date(doc.createdAt).toLocaleDateString()}
- |
-
- ))}
-
-
-
- )}
-
- {activeTab === 'users' && (
- <>
-
-
-
-
-
- | Name |
- Email |
- Admin Status |
- Actions |
-
-
-
- {users.map((user) => (
-
- | {user.name} |
- {user.email} |
-
-
- {user.isAdmin ? 'Admin' : 'User'}
-
- |
-
- {/* Add actions if needed */}
- |
-
- ))}
-
-
- >
- )}
-
- {activeTab === 'upload' && }
+ ))}
+
+
+
>
)}
+
+ {activeTab === 'upload' && (
+
+
+
+ )}
);
}
-export default AdminPanel;
-// import { useState, useEffect } from 'react';
-// import { useAuth } from '../../hooks/useAuth';
-// import { format } from 'date-fns';
-// import { FiUpload, FiUsers, FiFile, FiTrash2, FiShare2, FiLoader, FiSearch, FiChevronRight } from 'react-icons/fi';
-// import api from '../../services/api';
-
-// function AdminPanel() {
-// const [documents, setDocuments] = useState([]);
-// const [users, setUsers] = useState([]);
-// const [loading, setLoading] = useState(true);
-// const [error, setError] = useState('');
-// const [selectedFile, setSelectedFile] = useState(null);
-// const [uploadTitle, setUploadTitle] = useState('');
-// const [selectedUser, setSelectedUser] = useState('');
-// const [searchTerm, setSearchTerm] = useState('');
-// const [expandedUsers, setExpandedUsers] = useState({});
-
-// useEffect(() => {
-// fetchData();
-// }, []);
-
-// const fetchData = async () => {
-// try {
-// setLoading(true);
-// const [documentsRes, usersRes] = await Promise.all([
-// api.get('/admin/documents'),
-// api.get('/admin/users')
-// ]);
-
-// console.log('Documents response:', documentsRes.data);
-// console.log('Users response:', usersRes.data);
-
-// // Group documents by user
-// const groupedDocs = groupDocumentsByUser(documentsRes.data || []);
-// setDocuments(groupedDocs);
-// setUsers(usersRes.data || []);
-// } catch (err) {
-// setError('Failed to fetch data');
-// console.error('Error:', err);
-// } finally {
-// setLoading(false);
-// }
-// };
-// const groupDocumentsByUser = (docs) => {
-// return docs.reduce((acc, doc) => {
-// const userName = doc.sharedWith?.name || 'Unassigned';
-// if (!acc[userName]) {
-// acc[userName] = [];
-// }
-// acc[userName].push(doc);
-// return acc;
-// }, {});
-// };
-
-// const toggleUser = (userName) => {
-// setExpandedUsers(prev => ({
-// ...prev,
-// [userName]: !prev[userName]
-// }));
-// };
-
-// const handleFileUpload = async (e) => {
-// e.preventDefault();
-// if (!selectedFile || !uploadTitle || !selectedUser) {
-// alert('Please fill in all fields');
-// return;
-// }
-
-// const formData = new FormData();
-// formData.append('file', selectedFile);
-// formData.append('title', uploadTitle);
-// formData.append('sharedWith', selectedUser);
-
-// try {
-// await api.post('/admin/documents', formData, {
-// headers: {
-// 'Content-Type': 'multipart/form-data',
-// },
-// });
-
-// setSelectedFile(null);
-// setUploadTitle('');
-// setSelectedUser('');
-// fetchData();
-// alert('Document uploaded successfully');
-// } catch (err) {
-// console.error('Upload error:', err);
-// alert('Failed to upload document');
-// }
-// };
-
-// const handleDelete = async (documentId) => {
-// if (!window.confirm('Are you sure you want to delete this document?')) {
-// return;
-// }
-
-// try {
-// await api.delete(`/admin/documents/${documentId}`);
-// fetchData();
-// alert('Document deleted successfully');
-// } catch (err) {
-// console.error('Delete error:', err);
-// alert('Failed to delete document');
-// }
-// };
-
-// const handleShare = async (documentId) => {
-// const userId = prompt('Enter user ID to share with:');
-// if (!userId) return;
-
-// try {
-// await api.post(`/admin/documents/${documentId}/share`, { userId });
-// fetchData();
-// alert('Document shared successfully');
-// } catch (err) {
-// console.error('Share error:', err);
-// alert('Failed to share document');
-// }
-// };
-
-// if (loading) {
-// return (
-//
-//
-//
-// Loading admin panel...
-//
-//
-// );
-// }
-
-// if (error) {
-// return (
-//
-// );
-// }
-
-// return (
-//
-//
-//
-
-// {/* Upload Section - remains the same */}
-//
-// {/* ... upload form content remains the same ... */}
-//
-
-// {/* Search Bar */}
-//
-//
-// setSearchTerm(e.target.value)}
-// className="w-full pl-10 pr-4 py-3 bg-white/5 border border-gray-700 rounded-lg focus:outline-none focus:border-blue-500 text-white placeholder-gray-400"
-// />
-//
-
-// {/* Documents List - Grouped by User */}
-//
-// {Object.entries(documents).map(([userName, userDocs]) => {
-// const filteredDocs = userDocs.filter(doc =>
-// doc.title.toLowerCase().includes(searchTerm.toLowerCase())
-// );
-
-// if (filteredDocs.length === 0) return null;
-
-// return (
-//
-//
-
-// {expandedUsers[userName] && (
-//
-// {filteredDocs.map(doc => (
-//
-//
-//
-//
-//
{doc.title}
-//
-// Added: {format(new Date(doc.createdAt), 'MMM dd, yyyy')}
-//
-//
-//
-
-//
-//
-//
-//
-//
-// ))}
-//
-// )}
-//
-// );
-// })}
-
-// {Object.keys(documents).length === 0 && (
-//
-// No documents available
-//
-// )}
-//
-//
-//
-// )
-
-// }
-
-// export default AdminPanel;
-// import { useState, useEffect } from 'react';
-// import { useAuth } from '../../hooks/useAuth';
-// import { format } from 'date-fns';
-// import {
-// FiUpload,
-// FiDownload,
-// FiUsers,
-// FiFile,
-// FiTrash2,
-// FiShare2,
-// FiLoader,
-// FiSearch,
-// FiChevronRight,
-// FiUserPlus,
-// FiEdit2,
-// FiKey
-// } from 'react-icons/fi';
-// import api from '../../services/api';
-
-// function AdminPanel() {
-// const [documents, setDocuments] = useState({});
-// const [users, setUsers] = useState([]);
-// const [expandedUsers, setExpandedUsers] = useState({});
-// const [loading, setLoading] = useState(true);
-// const [error, setError] = useState('');
-// const [selectedFile, setSelectedFile] = useState(null);
-// const [uploadTitle, setUploadTitle] = useState('');
-// const [selectedUser, setSelectedUser] = useState('');
-// const [searchTerm, setSearchTerm] = useState('');
-// const [activeTab, setActiveTab] = useState('documents'); // 'documents' or 'users'
-// const [newUser, setNewUser] = useState({ username: '', password: '', name: '', isAdmin: false });
-
-// useEffect(() => {
-// fetchData();
-// }, []);
-
-// const fetchData = async () => {
-// try {
-// setLoading(true);
-// const [documentsRes, usersRes] = await Promise.all([
-// api.get('/admin/documents'),
-// api.get('/admin/users')
-// ]);
-
-// const groupedDocs = groupDocumentsByUser(documentsRes.data || []);
-// const initialExpandedState = Object.keys(groupedDocs).reduce((acc, userName) => {
-// acc[userName] = false;
-// return acc;
-// }, {});
-
-// setDocuments(groupedDocs);
-// setUsers(usersRes.data || []);
-// setExpandedUsers(initialExpandedState);
-// } catch (err) {
-// setError('Failed to fetch data');
-// console.error('Error:', err);
-// } finally {
-// setLoading(false);
-// }
-// };
-
-// const groupDocumentsByUser = (docs) => {
-// return docs.reduce((acc, doc) => {
-// const userName = doc.sharedWith?.name || 'Unassigned';
-// if (!acc[userName]) {
-// acc[userName] = [];
-// }
-// acc[userName].push(doc);
-// return acc;
-// }, {});
-// };
-
-// const handleAddUser = async (e) => {
-// e.preventDefault();
-// try {
-// await api.post('/admin/users', newUser);
-// setNewUser({ username: '', password: '', name: '', isAdmin: false });
-// fetchData();
-// alert('User added successfully');
-// } catch (err) {
-// console.error('Error adding user:', err);
-// alert('Failed to add user');
-// }
-// };
-
-// const handleDeleteUser = async (userId) => {
-// if (!window.confirm('Are you sure you want to delete this user?')) return;
-// try {
-// await api.delete(`/admin/users/${userId}`);
-// fetchData();
-// alert('User deleted successfully');
-// } catch (err) {
-// console.error('Error deleting user:', err);
-// alert('Failed to delete user');
-// }
-// };
-
-// const handleResetPassword = async (userId) => {
-// const newPassword = prompt('Enter new password:');
-// if (!newPassword) return;
-
-// try {
-// await api.post(`/admin/users/${userId}/reset-password`, { password: newPassword });
-// alert('Password reset successfully');
-// } catch (err) {
-// console.error('Error resetting password:', err);
-// alert('Failed to reset password');
-// }
-// };
-
-// const handleFileUpload = async (e) => {
-// e.preventDefault();
-// if (!selectedFile || !uploadTitle || !selectedUser) {
-// alert('Please fill in all fields');
-// return;
-// }
-
-// const formData = new FormData();
-// formData.append('file', selectedFile);
-// formData.append('title', uploadTitle);
-// formData.append('sharedWith', selectedUser);
-
-// try {
-// await api.post('/admin/documents', formData, {
-// headers: {
-// 'Content-Type': 'multipart/form-data',
-// },
-// });
-
-// setSelectedFile(null);
-// setUploadTitle('');
-// setSelectedUser('');
-// fetchData();
-// alert('Document uploaded successfully');
-// } catch (err) {
-// console.error('Upload error:', err);
-// alert('Failed to upload document');
-// }
-// };
-// const toggleUser = (userName) => {
-// console.log('Toggling user:', userName); // Debug log
-// setExpandedUsers(prev => ({
-// ...prev,
-// [userName]: !prev[userName]
-// }));
-// };
-
-
-
-// const handleDownload = async (s3Key, fileName) => {
-// try {
-// const token = localStorage.getItem('token');
-// // Remove 'documents/' from the s3Key as it's already in the path
-// const cleanS3Key = s3Key.replace('documents/', '');
-
-// const response = await fetch(`http://localhost:3000/admin/documents/download/${cleanS3Key}`, {
-// headers: {
-// 'Authorization': `Bearer ${token}`
-// }
-// });
-
-// if (!response.ok) {
-// throw new Error('Download failed');
-// }
-
-// const blob = await response.blob();
-// const url = window.URL.createObjectURL(blob);
-// const link = document.createElement('a');
-// link.href = url;
-// link.download = fileName;
-// document.body.appendChild(link);
-// link.click();
-// document.body.removeChild(link);
-// window.URL.revokeObjectURL(url);
-// } catch (err) {
-// console.error('Error downloading document:', err);
-// alert('Failed to download document. Please try again.');
-// }
-// };
-
-
-// if (loading) {
-// return (
-//
-//
-//
-// Loading admin panel...
-//
-//
-// );
-// }
-
-// return (
-//
-//
-//
-
-// {/* Tab Navigation */}
-//
-//
-//
-//
-
-// {activeTab === 'documents' ? (
-// <>
-// {/* Upload Section */}
-//
-//
Upload Document
-//
-//
-
-// {/* Documents List */}
-//
-// {Object.entries(documents).map(([userName, userDocs]) => (
-//
-//
-// {expandedUsers[userName] && (
-//
-// {userDocs.map(doc => (
-//
-//
-//
-//
-//
{doc.title}
-//
-// Added: {format(new Date(doc.createdAt), 'MMM dd, yyyy')}
-//
-//
-//
-
-//
-//
-//
-//
-//
-//
-// ))}
-//
-// )}
-//
-// ))}
-//
-// >
-// ) : (
-// // Users Management Section
-//
-// {/* Add New User Form */}
-//
-
-// {/* Users List */}
-//
-//
-//
Users
-//
-//
-// {Array.isArray(users) && users.map(user => (
-//
-//
-//
-//
-//
{user.name}
-//
@{user.username}
-//
-// {user.isAdmin && (
-//
-// Admin
-//
-// )}
-//
-
-//
-//
-//
-//
-//
-// ))}
-//
-//
-//
-// )}
-//
-//
-// );
-// }
-
-// export default AdminPanel;
\ No newline at end of file
+export default AdminPanel;
\ No newline at end of file