import { Gate } from "models/Gate"; import { Box, Card, IconButton, List, ListItem, ListItemText, Theme, Typography } from "@mui/material"; import React, { useCallback, useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { useMobile } from "hooks"; //import MaterialTable from "material-table"; //import { getTableIcons } from "common/ResponsiveTable"; import { Terminal } from "models/Terminal"; import { makeStyles } from "@mui/styles"; import ResponsiveTable, { Column } from "common/ResponsiveTable"; import AddGateFab from "./AddGateFab"; import GateSettings from "./GateSettings"; import { useGateAPI, useGlobalState } from "providers"; import { Settings } from "@mui/icons-material"; import { cloneDeep } from "lodash"; interface Props { //gates: Gate[]; terminals: Terminal[]; currentTerminal?: string; //the key of the terminal for the currently selected tab useMobile: boolean; //duplicateGate: (gate: Gate) => void; } const useStyles = makeStyles((theme: Theme) => ({ gridListTile: { minHeight: "184px", height: "auto !important", width: "184px", padding: 2 }, hidden: { visibility: "hidden" }, gateCard: { marginBottom: 10, paddingLeft: 15 } })); export default function GateList(props: Props) { const {terminals, currentTerminal } = props; const [{as}] = useGlobalState(); // const history = useHistory(); const navigate = useNavigate(); const isMobile = useMobile(); const classes = useStyles(); const [gateDialog, setGateDialog] = useState(false); const [tablePage, setTablePage] = useState(0) const [pageSize, setPageSize] = useState(10) const [total, setTotal] = useState(0) const [terminalMap, setTerminalMap] = useState>(new Map()); const gateAPI = useGateAPI(); const [gates, setGates] = useState([]) const [selectedGate, setSelectedGate] = useState() const [search, setSearch] = useState("") const goToGate = (gate: Gate) => { let path = "/terminals/" + gate.key; navigate(path, { state: gate }) }; useEffect(() => { let map = new Map(); terminals.forEach(t => { map.set(t.key, t.name); }); setTerminalMap(map); }, [terminals]); const loadGates = useCallback(() => { let keys = undefined let types = undefined if(currentTerminal) { keys = [currentTerminal] types = ["terminal"] } gateAPI.listGates(pageSize, pageSize * tablePage, "asc", "name", search, undefined, keys, types, undefined, undefined, as).then(resp => { setGates(resp.data.gates.map(gate => Gate.create(gate))) setTotal(resp.data.total) }) },[currentTerminal, search, pageSize, tablePage, as]) useEffect(() => { loadGates() },[loadGates]) const handleChange = (event: any) => { setPageSize(event.target.value); }; const desktopCols = (): Column[] => { return [ { title: "Gate", render: gate => ( {gate.name} ) }, { title: "Terminal", render: gate => ( {terminalMap.get(gate.terminal())} ) }, { title: "Duct Type", render: gate => ( {gate.settings.ductName} ) }, { title: "Duct Size(mm)", render: gate => ( {gate.ductDiameter()} ) }, { title: "Duct Length(m)", render: gate => ( {gate.settings.ductLength} ) }, { title: "PCA Unit", render: gate => ( {gate.settings.pcaType} ) }, ] } const mobileCols = (): Column[] => { return [{ title: "", render: gate => { return ( {gate.name} { event.stopPropagation() setSelectedGate(gate) setGateDialog(true) }}> ) } }] } const gutter = (gate: Gate) => { return( Terminal: {terminalMap.get(gate.terminal()) ?? "None"} Duct Type: {gate.ductName() !== "" ? gate.ductName() : "None"} Duct Size(mm): {gate.ductDiameter()} DuctLength(m): {gate.ductLength()} PCA Unit: {gate.settings.pcaType !== "" ? gate.settings.pcaType : "None"} // // ) } const gateTable = () => { return ( page={tablePage} pageSize={pageSize} columns={isMobile || props.useMobile ? mobileCols() : desktopCols()} handleRowsPerPageChange={handleChange} renderGutter={isMobile || props.useMobile ? gutter : undefined} gutterPadding={0} rows={gates} setPage={(page) => {setTablePage(page)}} total={total} onRowClick={(gate) => {goToGate(gate)}} hideKeys={isMobile || props.useMobile} setSearchText={(search) => setSearch(search)} loadMore={()=>{ let current = cloneDeep(gates) let keys = undefined let types = undefined if(currentTerminal) { keys = [currentTerminal] types = ["terminal"] } gateAPI.listGates(pageSize, current.length , "asc", "name", search, undefined, keys, types, undefined, undefined, as).then(resp => { let newGates = resp.data.gates.map(gate => Gate.create(gate)) setGates(current.concat(newGates)) }) }} /> ) }; return { if (newGate) { gates.push(newGate) } setGateDialog(false); }} terminals={terminals} /> { setGateDialog(true); }} pulse={gates.length < 1} /> {gateTable()} }