diff --git a/src/app/admin/adminVendors.js b/src/app/admin/adminVendors.js index a4dda00..baad1f7 100644 --- a/src/app/admin/adminVendors.js +++ b/src/app/admin/adminVendors.js @@ -66,10 +66,29 @@ async function deleteVendor(id) { return await response.json(); } +// Function to bulk delete vendors +async function bulkDeleteVendors(ids) { + try { + const deleteRequests = ids.map((id) => + fetch(`${process.env.NEXT_PUBLIC_BASE_PATH}/api/vendors`, { + method: 'DELETE', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ id }), + }) + ); + + await Promise.all(deleteRequests); + } catch (error) { + console.error('Error during bulk deletion:', error); + throw new Error('Failed to delete vendors.'); + } +} + export default function VendorsAdmin() { const [data, setData] = useState([]); const [searchQuery, setSearchQuery] = useState(""); const [filteredData, setFilteredData] = useState([]); + const [selectedVendors, setSelectedVendors] = useState([]); const [editingVendor, setEditingVendor] = useState(null); const editFormRef = useRef(null); const [csvFile, setCsvFile] = useState(null); // For storing the selected CSV file @@ -89,6 +108,7 @@ export default function VendorsAdmin() { async function getData() { const fetchedData = await fetchVendors(); setData(fetchedData); + setFilteredData(fetchedData); } getData(); }, []); @@ -101,6 +121,7 @@ export default function VendorsAdmin() { const updatedData = await fetchVendors(); setData(updatedData); setFilteredData(updatedData); + setSelectedVendors([]); }; @@ -245,6 +266,60 @@ export default function VendorsAdmin() { } }; + // Bulk delete vendors + const handleBulkDelete = async () => { + if (selectedVendors.length === 0) { + alert("You have not selected any vendors to delete."); + return; + } + + const confirmed = window.confirm( + "Are you sure you want to delete the selected vendors? This action cannot be undone." + ); + + if (!confirmed) return; + + console.log("Deleting vendors with IDs:", selectedVendors); // Debug log + + try { + const result = await bulkDeleteVendors(selectedVendors); + + // Handle result if it contains a valid response + console.log("Delete result:", result); + + setData((prevData) => + prevData.filter((vendor) => !selectedVendors.includes(vendor.id)) + ); + setFilteredData((prevFilteredData) => + prevFilteredData.filter((vendor) => !selectedVendors.includes(vendor.id)) + ); + setSelectedVendors([]); // Clear selection after deletion + + alert("Selected vendors have been deleted successfully!"); + } catch (error) { + console.error("Error deleting vendors:", error); + alert("Failed to delete vendors. Please try again."); + } + }; + + + const handleCheckboxChange = (id) => { + setSelectedVendors((prevSelected) => + prevSelected.includes(id) + ? prevSelected.filter((vendorId) => vendorId !== id) + : [...prevSelected, id] + ); + }; + + const handleSelectAll = (e) => { + if (e.target.checked) { + const allVendorIds = filteredData.map((vendor) => vendor.id); + setSelectedVendors(allVendorIds); + } else { + setSelectedVendors([]); + } + }; + // Trigger editing mode for a selected vendor const handleEditClick = (vendor) => { setEditingVendor(vendor); @@ -286,14 +361,14 @@ export default function VendorsAdmin() { Upload CSV - +
- +
{/* Add New Vendor Form */} @@ -404,6 +479,19 @@ export default function VendorsAdmin() { /> + + {/* Bulk Delete Selected Vendors Button */} + {selectedVendors.length > 0 && ( +
+ +
+ )} + {/* Table layout on large screens, Card layout on small screens */}
{/* Table layout */} @@ -411,6 +499,19 @@ export default function VendorsAdmin() { + @@ -425,6 +526,13 @@ export default function VendorsAdmin() { {filteredData.length > 0 ? ( filteredData.map((vendor) => ( +
+ { + if (selectedVendors.length === data.length) { + setSelectedVendors([]); + } else { + setSelectedVendors(data.map((vendor) => vendor.id)); + } + }} + checked={selectedVendors.length === data.length} + /> + Building Floor Room
+ handleCheckboxChange(vendor.id)} + checked={selectedVendors.includes(vendor.id)} + /> + {vendor.building} {vendor.floor} {vendor.room}