All files / components/Reports ReportTable.jsx

100% Statements 47/47
100% Branches 6/6
100% Functions 17/17
100% Lines 47/47

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                          3x 3x 3x               45x   45x   45x 45x 45x 45x   45x           45x           45x           45x 2x 2x 1x   1x       45x 3x 3x     45x 3x 3x     45x 1x 1x     45x 1x 1x     45x                 169x                             169x             169x         45x 36x     36x     156x                     3x               36x     156x                   3x                   44x   44x                                   1x                         1x                               1x                           1x                                
import React, { useState } from "react";
import { Button, Modal, OverlayTrigger, Tooltip } from "react-bootstrap";
import OurTable, { ButtonColumn } from "main/components/OurTable";
import PageSizeSelector from "main/components/Utils/PageSizeSelector";
import usePageSize from "main/utils/usePageSize";
import { useNavigate } from "react-router";
import { useBackendMutation } from "main/utils/useBackend";
import {
  cellToAxiosParamsDeleteReport,
  cellToAxiosParamsPurgeReports,
  onDeleteSuccess,
} from "main/utils/reportUtils";
 
const PAGE_SIZE_OPTIONS = [20, 50, 100, 200, 500];
const DEFAULT_PAGE_SIZE = 20;
const PAGE_SIZE_STORAGE_KEY = "reports-page-size";
 
// should take in a players list from a game
export default function ReportTable({
  reports,
  storybook = false,
  buttons = true,
}) {
  const testid = "ReportTable";
 
  const navigate = useNavigate();
 
  const [showDeleteModal, setShowDeleteModal] = useState(false);
  const [showPurgeModal, setShowPurgeModal] = useState(false);
  const [cellToDelete, setCellToDelete] = useState(null);
  const [cellToPurge, setCellToPurge] = useState(null);
 
  const [pageSize, handlePageSizeChange] = usePageSize({
    storageKey: PAGE_SIZE_STORAGE_KEY,
    options: PAGE_SIZE_OPTIONS,
    defaultPageSize: DEFAULT_PAGE_SIZE,
  });
 
  const deleteMutation = useBackendMutation(
    cellToAxiosParamsDeleteReport,
    { onSuccess: onDeleteSuccess },
    ["/api/reports"],
  );
 
  const purgeMutation = useBackendMutation(
    cellToAxiosParamsPurgeReports,
    { onSuccess: onDeleteSuccess },
    ["/api/reports"],
  );
 
  const reportCallback = (cell) => {
    const route = `/admin/report/${cell.row.values["id"]}`;
    if (storybook) {
      window.alert(`would navigate to ${route}`);
    } else {
      navigate(route);
    }
  };
 
  const deleteCallback = (cell) => {
    setCellToDelete(cell);
    setShowDeleteModal(true);
  };
 
  const purgeCallback = (cell) => {
    setCellToPurge(cell);
    setShowPurgeModal(true);
  };
 
  const confirmDelete = () => {
    deleteMutation.mutate(cellToDelete);
    setShowDeleteModal(false);
  };
 
  const confirmPurge = () => {
    purgeMutation.mutate(cellToPurge);
    setShowPurgeModal(false);
  };
 
  const columns = [
    {
      Header: "id",
      accessor: "id",
    },
    {
      Header: "gameId",
      accessor: "gameId",
      Cell: (props) => {
        return <div style={{ textAlign: "right" }}>{props.value}</div>;
      },
    },
    {
      Header: "Name",
      accessor: "name",
    },
    {
      Header: "Create Date",
      accessor: "createDate",
    },
    {
      Header: "Num Users",
      accessor: "numUsers",
      Cell: (props) => {
        return <div style={{ textAlign: "right" }}>{props.value}</div>;
      },
    },
    {
      Header: "Num Cows",
      accessor: "numCows",
      Cell: (props) => {
        return <div style={{ textAlign: "right" }}>{props.value}</div>;
      },
    },
  ];
 
  if (buttons) {
    columns.push(
      ButtonColumn("View Report", "secondary", reportCallback, testid),
    );
    columns.push({
      Header: "Delete Report",
      Cell: ({ cell }) => (
        <OverlayTrigger
          placement="top"
          overlay={
            <Tooltip>
              Deletes the current report (along with all detail rows pertaining
              to it)
            </Tooltip>
          }
        >
          <Button
            variant="danger"
            onClick={() => deleteCallback(cell)}
            data-testid={`${testid}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
          >
            Delete Report
          </Button>
        </OverlayTrigger>
      ),
    });
    columns.push({
      Header: "Purge Older Reports",
      Cell: ({ cell }) => (
        <OverlayTrigger
          placement="top"
          overlay={
            <Tooltip>
              Keeps this report, but purges all reports older than this one
            </Tooltip>
          }
        >
          <Button
            variant="warning"
            onClick={() => purgeCallback(cell)}
            data-testid={`${testid}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
          >
            Purge Older Reports
          </Button>
        </OverlayTrigger>
      ),
    });
  }
 
  const sortBy = [{ id: "createDate", desc: true }];
 
  return (
    <>
      <PageSizeSelector
        value={pageSize}
        onChange={handlePageSizeChange}
        options={PAGE_SIZE_OPTIONS}
        testid={`${testid}-page-size-selector`}
      />
      <OurTable
        data={reports}
        columns={columns}
        testid={testid}
        pageSize={pageSize}
        initialState={{ sortBy }}
      />
      <Modal
        data-testid={`${testid}-DeleteModal`}
        show={showDeleteModal}
        onHide={() => setShowDeleteModal(false)}
      >
        <Modal.Header closeButton>
          <Modal.Title>Confirm Deletion</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          Are you sure you want to delete this report, along with all detail
          rows pertaining to it? This cannot be undone.
        </Modal.Body>
        <Modal.Footer>
          <Button
            variant="secondary"
            data-testid={`${testid}-DeleteModal-Cancel`}
            onClick={() => setShowDeleteModal(false)}
          >
            Keep this Report
          </Button>
          <Button
            variant="danger"
            data-testid={`${testid}-DeleteModal-Delete`}
            onClick={confirmDelete}
          >
            Permanently Delete
          </Button>
        </Modal.Footer>
      </Modal>
      <Modal
        data-testid={`${testid}-PurgeModal`}
        show={showPurgeModal}
        onHide={() => setShowPurgeModal(false)}
      >
        <Modal.Header closeButton>
          <Modal.Title>Confirm Purge</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          Are you sure you want to purge all reports older than this one? This
          keeps this report, but permanently deletes all older reports. This
          cannot be undone.
        </Modal.Body>
        <Modal.Footer>
          <Button
            variant="secondary"
            data-testid={`${testid}-PurgeModal-Cancel`}
            onClick={() => setShowPurgeModal(false)}
          >
            Cancel
          </Button>
          <Button
            variant="warning"
            data-testid={`${testid}-PurgeModal-Purge`}
            onClick={confirmPurge}
          >
            Purge Older Reports
          </Button>
        </Modal.Footer>
      </Modal>
    </>
  );
}