"use client";
import { useParams } from "next/navigation";
import { useState } from "react";
import { Button } from "@nextui-org/button";
import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter } from "@nextui-org/modal";
import { Input } from "@nextui-org/input";
import { Timestamp } from "firebase/firestore";
import { Table, TableHeader, TableColumn, TableBody, TableRow, TableCell } from "@nextui-org/table";
import { Select, SelectItem } from "@nextui-org/react";
import { useBuilding, WasteDataPoint } from "@/lib/useBuildingData";
import { trashItems } from "@/components/trashcanMode";
export default function TrashPage() {
const { buildingid } = useParams();
const { data: building, isLoading, error, updateBuilding } = useBuilding(buildingid as string);
const [isModalOpen, setIsModalOpen] = useState(false);
const [newEntry, setNewEntry] = useState({
timestamp: new Date().toISOString().slice(0, 16),
type: "",
trashcanID: "",
wasteCategory: "",
emissions: 0,
});
const [sortConfig, setSortConfig] = useState<{ key: keyof WasteDataPoint; direction: 'ascending' | 'descending' } | null>(null);
if (isLoading) return
Loading...
;
if (error) return Error: {error.message}
;
if (!building) return Building not found
;
const handleInputChange = (e: React.ChangeEvent) => {
const { name, value } = e.target;
if (name === 'emissions') {
const inputValue = parseFloat(value);
const scaledValue = isNaN(inputValue) ? 0 : inputValue / 1e+3;
setNewEntry(prev => ({ ...prev, [name]: scaledValue }));
} else {
setNewEntry(prev => ({ ...prev, [name]: value }));
}
};
const handleSubmit = () => {
const updatedWasteGeneration = [
...building!.wasteGeneration,
{ ...newEntry, timestamp: Timestamp.fromDate(new Date(newEntry.timestamp)), emissions: Number(newEntry.emissions) }
];
updateBuilding({ wasteGeneration: updatedWasteGeneration as WasteDataPoint[] });
setIsModalOpen(false);
};
const handleSort = (key: keyof WasteDataPoint) => {
let direction: 'ascending' | 'descending' = 'ascending';
if (sortConfig && sortConfig.key === key && sortConfig.direction === 'ascending') {
direction = 'descending';
}
setSortConfig({ key, direction });
};
const sortedWasteGeneration = [...building.wasteGeneration].sort((a, b) => {
if (!sortConfig) return 0;
const { key, direction } = sortConfig;
if (a[key] < b[key]) return direction === 'ascending' ? -1 : 1;
if (a[key] > b[key]) return direction === 'ascending' ? 1 : -1;
return 0;
});
const handleDelete = (index: number) => {
updateBuilding({ operation: 'deleteWasteEntry', index });
};
return (
);
}