import * as React from "react"; import { styled } from "@mui/material/styles"; import Table from "@mui/material/Table"; import TableBody from "@mui/material/TableBody"; import TableCell, { tableCellClasses } from "@mui/material/TableCell"; import TableContainer from "@mui/material/TableContainer"; import TableHead from "@mui/material/TableHead"; import TableRow from "@mui/material/TableRow"; import Paper, { paperClasses } from "@mui/material/Paper"; import { adminList, deleteAdmin } from "../../redux/slices/adminSlice.ts"; import { vehicleList, deleteVehicle } from "../../redux/slices/VehicleSlice.ts"; import { deleteManager, managerList } from "../../redux/slices/managerSlice.ts"; import { useDispatch, useSelector } from "react-redux"; import { Box, Button, InputAdornment, Menu, IconButton, Pagination, TextField, Typography, } from "@mui/material"; import MoreHorizRoundedIcon from "@mui/icons-material/MoreHorizRounded"; import DeleteModal from "../Modals/DeleteModal/index.tsx"; import { AppDispatch, RootState } from "../../redux/store/store.ts"; import ViewModal from "../Modals/ViewModal/index.tsx"; import VehicleViewModal from "../Modals/VehicleViewModal/index.tsx"; import SearchIcon from "@mui/icons-material/Search"; import TuneIcon from "@mui/icons-material/Tune"; import { CustomIconButton } from "../AddUserModal/styled.css"; import ManagerViewModal from "../Modals/ViewManagerModal/index.tsx"; import UserViewModal from "../Modals/UserViewModal/index.tsx"; import { deleteUser, userList } from "../../redux/slices/userSlice.ts"; import { deleteStation, stationList } from "../../redux/slices/stationSlice.ts"; import StationViewModal from "../Modals/StationViewModal/index.tsx"; import { deleteSlot, fetchAvailableSlots, } from "../../redux/slices/slotSlice.ts"; import { bookingList, deleteBooking } from "../../redux/slices/bookSlice.ts"; // Styled components for customization const StyledTableCell = styled(TableCell)(({ theme }) => ({ [`&.${tableCellClasses.head}`]: { backgroundColor: "#454545", // Changed to #272727 for the header color: theme.palette.common.white, borderBottom: "none", // Remove any border at the bottom of the header }, [`&.${tableCellClasses.body}`]: { fontSize: 14, borderBottom: "1px solid #454545", // Adding border to body cells }, })); const StyledTableRow = styled(TableRow)(({ theme }) => ({ "&:nth-of-type(odd)": { backgroundColor: theme.palette.action.hover, }, "& td, th": { borderColor: "#454545", // Applying border color to both td and th borderWidth: "1px", // Set border width to ensure it appears }, })); export interface Column { id: string; label: string; align?: "left" | "center" | "right"; } interface Row { [key: string]: any; } interface CustomTableProps { columns: Column[]; rows: Row[]; setDeleteModal: Function; setRowData: Function; setModalOpen: Function; viewModal: boolean; setViewModal: Function; deleteModal: boolean; handleStatusToggle?: (id: string, currentStatus: number) => void; tableType: string; // Adding tableType prop to change header text dynamically handleClickOpen?: () => void; } const CustomTable: React.FC = ({ columns, rows, setDeleteModal, deleteModal, viewModal, setRowData, setViewModal, setModalOpen, handleStatusToggle, tableType, handleClickOpen, }) => { const dispatch = useDispatch(); const [anchorEl, setAnchorEl] = React.useState(null); const [selectedRow, setSelectedRow] = React.useState(null); const [searchQuery, setSearchQuery] = React.useState(""); const [currentPage, setCurrentPage] = React.useState(1); const usersPerPage = 10; const { user } = useSelector((state: RootState) => state?.profileReducer); const open = Boolean(anchorEl); const handleClick = (event: React.MouseEvent, row: Row) => { setAnchorEl(event.currentTarget); setSelectedRow(row); setRowData(row); }; const handleClose = () => { setAnchorEl(null); }; const isImage = (value: any) => { if (typeof value === "string") { return value.startsWith("http") || value.startsWith("data:image"); // Check for URL or base64 image } return false; }; const handleDeleteButton = (id: string | undefined) => { if (!id) { console.error("ID not found", id); return; } switch (tableType) { case "admin": dispatch(deleteAdmin(id || "")); break; case "vehicle": dispatch(deleteVehicle(id || "")); break; case "manager": dispatch(deleteManager(id || "")); break; case "user": dispatch(deleteUser(id || "")); break; case "station": dispatch(deleteStation(id || "")); break; case "slots": dispatch(deleteSlot(id || "")); break; case "booking": dispatch(deleteBooking(id || "")); break; default: console.error("Unknown table type:", tableType); return; } setDeleteModal(false); // Close the modal only after deletion handleClose(); }; const handleViewButton = (id: string | undefined) => { if (!id) console.error("ID not found", id); switch (tableType) { case "admin": dispatch(adminList()); break; case "vehicle": dispatch(vehicleList()); break; case "manager": dispatch(managerList()); break; case "user": dispatch(userList()); break; case "booking": dispatch(bookingList()); break; case "slots": dispatch(fetchAvailableSlots()); break; case "station": dispatch(stationList()); break; default: console.error("Unknown table type:", tableType); return; } setViewModal(false); }; const handleToggleStatus = () => { if (selectedRow) { // Toggle the opposite of current status const newStatus = selectedRow.statusValue === 1 ? 0 : 1; handleStatusToggle?.(selectedRow.id, newStatus); } handleClose(); }; const filteredRows = rows.filter( (row) => (row.name && row.name.toLowerCase().includes(searchQuery.toLowerCase())) || row.registeredAddress.toLowerCase().includes(searchQuery.toLowerCase()) || false ); const indexOfLastRow = currentPage * usersPerPage; const indexOfFirstRow = indexOfLastRow - usersPerPage; const currentRows = filteredRows.slice(indexOfFirstRow, indexOfLastRow); const handlePageChange = ( _event: React.ChangeEvent, value: number ) => { setCurrentPage(value); }; return ( {/* Dynamic title based on the page type */} {(() => { switch (tableType) { case "admin": return "Admin"; case "role": return "Roles"; case "user": return "Users"; case "manager": return "Managers"; case "vehicle": return "Vehicles"; case "station": return "Charging Station"; case "external-station": return "Charging Station"; case "booking": return "Booking"; case "slots": return "Slot"; default: return "List"; } })()} {/* Search & Buttons Section */} ), }, }} value={searchQuery} onChange={(e) => setSearchQuery(e.target.value)} /> {!( user?.userType === "user" && (tableType === "slots" )) && ( )} {/* Table Section */} {" "} {columns.map((column) => ( {column.label} ))} {currentRows.map((row, rowIndex) => ( {columns.map((column) => ( {isImage(row[column.id]) ? ( Row ) : column.id !== "action" ? ( row[column.id] ) : ( { handleClick(e, row); setRowData(row); // Store the selected row }} sx={{ padding: 0, minWidth: 0, width: "auto", height: "auto", color: "#FFFFFF", }} > )} ))} ))}
{/* Pagination */} Page Number : {/* Menu Actions */} {open && (
{viewModal && tableType === "admin" && ( handleViewButton(selectedRow?.id) } open={viewModal} setViewModal={setViewModal} id={selectedRow?.id} /> )} {viewModal && tableType === "manager" && ( handleViewButton(selectedRow?.id) } open={viewModal} setViewModal={setViewModal} id={selectedRow?.id} /> )} {viewModal && tableType === "vehicle" && ( handleViewButton(selectedRow?.id) } open={viewModal} setViewModal={setViewModal} id={selectedRow?.id} /> )} {viewModal && tableType === "station" && ( handleViewButton(selectedRow?.id) } open={viewModal} setViewModal={setViewModal} id={selectedRow?.id} /> )} {viewModal && tableType === "user" && ( handleViewButton(selectedRow?.id) } open={viewModal} setViewModal={setViewModal} id={selectedRow?.id} /> )} {/* Edit Button */} {tableType === "role" && ( )} {tableType === "station" && ( )} {/* Delete Button */}
)} {/* Modals */} {deleteModal && ( handleDeleteButton(selectedRow?.id)} open={deleteModal} setDeleteModal={setDeleteModal} id={selectedRow?.id} /> )}
); }; export default CustomTable;