summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/components/topologies/RoomTable.js
blob: 8a5c401ffb200b6af9e952c10e39f2ef114c519d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Button } from '@patternfly/react-core'
import PropTypes from 'prop-types'
import React from 'react'
import { useDispatch } from 'react-redux'
import { useTopology } from '../../data/topology'
import { Table, TableBody, TableHeader } from '@patternfly/react-table'
import { goToRoom } from '../../redux/actions/interaction-level'
import { deleteRoom } from '../../redux/actions/topology/room'
import TableEmptyState from '../util/TableEmptyState'

function RoomTable({ topologyId }) {
    const dispatch = useDispatch()
    const { status, data: topology } = useTopology(topologyId)

    const onClick = (room) => dispatch(goToRoom(room._id))
    const onDelete = (room) => dispatch(deleteRoom(room._id))

    const columns = ['Name', 'Tiles', 'Racks']
    const rows =
        topology?.rooms.length > 0
            ? topology.rooms.map((room) => {
                  const tileCount = room.tiles.length
                  const rackCount = room.tiles.filter((tile) => tile.rack).length
                  return [
                      {
                          title: (
                              <Button variant="link" isInline onClick={() => onClick(room)}>
                                  {room.name}
                              </Button>
                          ),
                      },
                      tileCount === 1 ? '1 tile' : `${tileCount} tiles`,
                      rackCount === 1 ? '1 rack' : `${rackCount} racks`,
                  ]
              })
            : [
                  {
                      heightAuto: true,
                      cells: [
                          {
                              props: { colSpan: 3 },
                              title: <TableEmptyState status={status} loadingTitle="Loading Rooms" />,
                          },
                      ],
                  },
              ]

    const actions =
        topology?.rooms.length > 0
            ? [
                  {
                      title: 'Delete room',
                      onClick: (_, rowId) => onDelete(topology.rooms[rowId]),
                  },
              ]
            : []

    return (
        <Table aria-label="Room list" variant="compact" cells={columns} rows={rows} actions={actions}>
            <TableHeader />
            <TableBody />
        </Table>
    )
}

RoomTable.propTypes = {
    topologyId: PropTypes.string,
}

export default RoomTable