All files / components OurTable.jsx

100% Statements 250/250
100% Branches 40/40
100% Functions 17/17
100% Lines 250/250

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 3261x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 394x 394x 394x 394x 394x 394x 394x   394x 394x 394x 394x 394x 394x 186x 186x 394x 394x     394x 394x 394x 394x   394x 394x 394x 394x 394x 394x 394x 394x 394x   394x 394x 372x 372x 372x 372x 372x 372x 2725x 2725x   2725x 2725x 2725x 2725x 2725x 2725x 2725x 2725x   2725x 2725x 2725x   2725x 190x 188x 2x 2535x       2725x     372x     394x 394x 394x 394x 977x 977x 977x 977x 977x 977x 977x 977x 977x   977x 7256x 7256x 7256x 7256x 7256x 7256x 7256x 7256x 7256x 7256x 7256x   7256x     7256x     977x     394x 394x 394x 394x 394x 394x 394x   394x 394x 394x 394x   394x     394x 394x   394x 394x 394x 394x   394x     394x 394x 394x 394x   394x     394x 394x 394x 394x   394x     394x 394x 394x 394x     394x   394x 394x 394x 394x   394x     394x 394x 394x 394x   394x     394x 394x 394x 394x   394x     394x 394x   394x 394x 394x 3x   394x   394x     394x 394x 394x 394x 394x 394x 22x               394x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 181x 181x 181x 181x 181x 181x 181x 181x   181x     181x 181x 181x   1x 75x 75x 75x 75x 75x 75x 75x 75x   75x     75x 75x 75x   1x 43x 43x 43x 43x 43x 43x 43x   1x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 975x 975x 975x   975x 975x 85x 85x 85x  
import React from "react";
import { useTable, useSortBy } from "react-table";
import { Table, Button, Pagination } from "react-bootstrap";
import Plaintext from "main/components/Utils/Plaintext";
 
// Stryker disable all
var tableStyle = {
  background: "white",
  display: "block",
  maxWidth: "-moz-fit-content",
  margin: "0 auto",
  overflowX: "auto",
  whiteSpace: "nowrap",
};
var paginationStyle = {
  display: "flex",
  justifyContent: "right",
};
// Stryker restore all
export default function OurTable({
  columns,
  data,
  testid = "testid",
  ...rest
}) {
  const [pageIndex, setPageIndex] = React.useState(0);
  const pageSize = rest.pageSize || 10;
 
  const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
    useTable(
      {
        columns,
        data,
        ...(rest.initialState && {
          initialState: rest.initialState,
        }),
      },
      useSortBy,
    );
 
  const tableProps = getTableProps();
  const { key: tableKey, ...tableRest } = tableProps || {};
  const bodyProps = getTableBodyProps();
  const { key: bodyKey, ...bodyRest } = bodyProps || {};
 
  return (
    <div {...tableRest} key={tableKey}>
      <Table
        style={tableStyle}
        {...tableRest}
        key={tableKey}
        striped
        bordered
        hover
      >
        <thead>
          {headerGroups.map((headerGroup, hgIndex) => {
            const hgProps = headerGroup.getHeaderGroupProps();
            const { key: hgKey, ...hgRest } = hgProps || {};
            const safeHgKey = hgKey ?? `headergroup-${hgIndex}`;
            return (
              <tr {...hgRest} key={safeHgKey} data-fallback-key={safeHgKey}>
                {headerGroup.headers.map((column, colIndex) => {
                  const colProps = column.getHeaderProps(
                    column.getSortByToggleProps(),
                  );
                  const { key: colKey, ...colRest } = colProps || {};
                  const safeColKey = colKey ?? `col-${column.id ?? colIndex}`;
                  return (
                    <th
                      {...colRest}
                      key={safeColKey}
                      data-fallback-key={safeColKey}
                      data-testid={`${testid}-header-${column.id}`}
                    >
                      {column.render("Header")}
                      <span
                        data-testid={`${testid}-header-${column.id}-sort-carets`}
                      >
                        {column.isSorted
                          ? column.isSortedDesc
                            ? " 🔽"
                            : " 🔼"
                          : ""}
                      </span>
                    </th>
                  );
                })}
              </tr>
            );
          })}
          {/* headerGroups already rendered above with safe keys */}
        </thead>
        <tbody {...bodyRest} key={bodyKey}>
          {rows
            .slice(pageIndex * pageSize, (pageIndex + 1) * pageSize)
            .map((row, rowIndex) => {
              prepareRow(row);
              const rowProps = row.getRowProps();
              const { key: rowKey, ...rowRest } = rowProps || {};
              const safeRowKey = rowKey ?? `row-${rowIndex}`;
              return (
                <tr
                  {...rowRest}
                  key={safeRowKey}
                  data-fallback-key={safeRowKey}
                >
                  {row.cells.map((cell, cellIndex) => {
                    const cellProps = cell.getCellProps();
                    const { key: cellKey, ...cellRest } = cellProps || {};
                    const safeCellKey =
                      cellKey ??
                      `cell-${cell.row.index}-${cell.column.id ?? cellIndex}`;
                    return (
                      <td
                        {...cellRest}
                        key={safeCellKey}
                        data-fallback-key={safeCellKey}
                        data-testid={`${testid}-cell-row-${cell.row.index}-col-${cell.column.id}`}
                      >
                        {cell.render("Cell")}
                      </td>
                    );
                  })}
                </tr>
              );
            })}
        </tbody>
      </Table>
      {rows.length > pageSize && (
        <div style={paginationStyle}>
          <Pagination {...tableRest}>
            <Pagination.Prev
              onClick={() => setPageIndex(pageIndex - 1)}
              data-testid={`${testid}-prev-page-button`}
              disabled={pageIndex === 0}
            />
            {pageIndex > 3 && (
              <Pagination.Item
                onClick={() => setPageIndex(0)}
                data-testid={`${testid}-first-page-button`}
              >
                {1}
              </Pagination.Item>
            )}
            {pageIndex > 4 && (
              <Pagination.Ellipsis data-testid={`${testid}-left-ellipsis`} />
            )}
            {pageIndex === 4 && (
              <Pagination.Item
                onClick={() => setPageIndex(pageIndex - 3)}
                data-testid={`${testid}-back-three-page-button`}
              >
                {pageIndex - 2}
              </Pagination.Item>
            )}
            {pageIndex > 1 && (
              <Pagination.Item
                onClick={() => setPageIndex(pageIndex - 2)}
                data-testid={`${testid}-back-two-page-button`}
              >
                {pageIndex - 1}
              </Pagination.Item>
            )}
            {pageIndex > 0 && (
              <Pagination.Item
                onClick={() => setPageIndex(pageIndex - 1)}
                data-testid={`${testid}-back-one-page-button`}
              >
                {pageIndex}
              </Pagination.Item>
            )}
            <Pagination.Item
              data-testid={`${testid}-current-page-button`}
              // Stryker disable all
              active={true}
              // Stryker restore all
            >
              {pageIndex + 1}
            </Pagination.Item>
            {pageIndex + 1 <= Math.ceil(rows.length / pageSize - 1) && (
              <Pagination.Item
                onClick={() => setPageIndex(pageIndex + 1)}
                data-testid={`${testid}-forward-one-page-button`}
              >
                {pageIndex + 2}
              </Pagination.Item>
            )}
            {pageIndex + 2 <= Math.ceil(rows.length / pageSize - 1) && (
              <Pagination.Item
                onClick={() => setPageIndex(pageIndex + 2)}
                data-testid={`${testid}-forward-two-page-button`}
              >
                {pageIndex + 3}
              </Pagination.Item>
            )}
            {pageIndex + 4 === Math.ceil(rows.length / pageSize - 1) && (
              <Pagination.Item
                onClick={() => setPageIndex(pageIndex + 3)}
                data-testid={`${testid}-forward-three-page-button`}
              >
                {pageIndex + 4}
              </Pagination.Item>
            )}
            {pageIndex + 4 < Math.ceil(rows.length / pageSize - 1) && (
              <Pagination.Ellipsis data-testid={`${testid}-right-ellipsis`} />
            )}
            {pageIndex + 4 <= Math.ceil(rows.length / pageSize - 1) && (
              <Pagination.Item
                onClick={() =>
                  setPageIndex(Math.ceil(rows.length / pageSize - 1))
                }
                data-testid={`${testid}-last-page-button`}
              >
                {Math.ceil(rows.length / pageSize)}
              </Pagination.Item>
            )}
            <Pagination.Next
              {...bodyRest}
              onClick={() => setPageIndex(pageIndex + 1)}
              data-testid={`${testid}-next-page-button`}
              disabled={
                pageIndex === Math.ceil(rows.length / pageSize - 1) ||
                rows.length === 0
              }
            />
          </Pagination>
        </div>
      )}
    </div>
  );
}
 
// The callback function for ButtonColumn should have the form
// (cell) => { doSomethingWith(cell); }
// The fields in cell are:
//   ["column","row","value","getCellProps","render"]
// Documented here: https://react-table.tanstack.com/docs/api/useTable#cell-properties
// Typically, you want cell.row.values, which is where you can get the individual
//   fields of the object representing the row in the table.
// Example:
//   const deleteCallback = (cell) =>
//      toast(`Delete Callback called on id: ${cell.row.values.id} name: ${cell.row.values.name}`);
 
// Add it to table like this:
// const columns = [
//   {
//       Header: 'id',
//       accessor: 'id', // accessor is the "key" in the data
//   },
//   {
//       Header: 'Name',
//       accessor: 'name',
//   },
//   ButtonColumn("Edit", "primary", editCallback),
//   ButtonColumn("Delete", "danger", deleteCallback)
// ];
 
export function ButtonColumn(label, variant, callback, testid) {
  const column = {
    Header: label,
    id: label,
    Cell: ({ cell }) => (
      <Button
        variant={variant}
        onClick={() => callback(cell)}
        data-testid={`${testid}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
      >
        {label}
      </Button>
    ),
  };
  return column;
}
 
export function HrefButtonColumn(label, variant, href, testid) {
  const column = {
    Header: label,
    id: label,
    Cell: ({ cell }) => (
      <Button
        variant={variant}
        href={`${href}${cell.row.values["commons.id"]}`}
        data-testid={`${testid}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
      >
        {label}
      </Button>
    ),
  };
  return column;
}
 
export function PlaintextColumn(label, getText) {
  const column = {
    Header: label,
    id: label,
    Cell: ({ cell }) => <Plaintext text={getText(cell)} />,
  };
  return column;
}
 
export function DateColumn(label, getDate) {
  const options = {
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
    hour12: false,
    timeZone: "America/Los_Angeles",
  };
  const column = {
    Header: label,
    id: label,
    Cell: ({ cell }) => {
      const date = new Date(getDate(cell));
      const formattedDate = new Intl.DateTimeFormat("en-US", options).format(
        date,
      );
      return <>{formattedDate}</>;
    },
  };
  return column;
}