All files / components/Users UsersTable.js

100% Statements 28/28
100% Branches 0/0
100% Functions 12/12
100% Lines 26/26

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              27x 27x 27x 27x 27x   27x 4x 4x     27x 4x 4x     27x 1x 1x     27x 1x 1x     27x                                       75x         75x         75x             27x               1x                     27x               1x                   27x                
import React, { useState } from "react";
import OurTable, { ButtonColumn } from "main/components/OurTable";
import { formatTime } from "main/utils/dateUtils";
import { useRestoreUser, useSuspendUser } from "main/utils/users";
import { Button, Modal } from "react-bootstrap";
 
export default function UsersTable({ users }) {
  const { mutate: suspendUser } = useSuspendUser();
  const { mutate: restoreUser } = useRestoreUser();
  const [showSuspendModal, setShowSuspendModal] = useState(false);
  const [showRestoreModal, setShowRestoreModal] = useState(false);
  const [selectedCell, setSelectedCell] = useState(null);
 
  const suspendCallback = async (cell) => {
    setSelectedCell(cell);
    setShowSuspendModal(true);
  };
 
  const restoreCallback = async (cell) => {
    setSelectedCell(cell);
    setShowRestoreModal(true);
  };
 
  const confirmSuspend = async () => {
    suspendUser(selectedCell);
    setShowSuspendModal(false);
  };
 
  const confirmRestore = async () => {
    restoreUser(selectedCell);
    setShowRestoreModal(false);
  };
 
  const columns = [
    {
      Header: "id",
      accessor: "id", // accessor is the "key" in the data
    },
    {
      Header: "First Name",
      accessor: "givenName",
    },
    {
      Header: "Last Name",
      accessor: "familyName",
    },
    {
      Header: "Email",
      accessor: "email",
    },
    {
      Header: "Last Online",
      id: "lastOnline",
      accessor: (row) => formatTime(row.lastOnline),
    },
    {
      Header: "Admin",
      id: "admin",
      accessor: (row, _rowIndex) => String(row.admin), // hack needed for boolean values to show up
    },
    {
      Header: "Suspended",
      id: "suspended",
      accessor: (row, _rowIndex) => String(row.suspended), // hack needed for boolean values to show up
    },
    ButtonColumn("Suspend", "danger", suspendCallback, "UsersTable"),
    ButtonColumn("Restore", "primary", restoreCallback, "UsersTable"),
  ];
 
  const suspendModal = (
    <Modal show={showSuspendModal} onHide={() => setShowSuspendModal(false)}>
      <Modal.Header closeButton>
        <Modal.Title>Suspend User</Modal.Title>
      </Modal.Header>
      <Modal.Body>
        Are you sure you want to suspend {selectedCell?.row.values.email}?
      </Modal.Body>
      <Modal.Footer>
        <Button variant="secondary" onClick={() => setShowSuspendModal(false)}>
          Cancel
        </Button>
        <Button variant="danger" onClick={confirmSuspend}>
          Confirm Suspend
        </Button>
      </Modal.Footer>
    </Modal>
  );
 
  const restoreModal = (
    <Modal show={showRestoreModal} onHide={() => setShowRestoreModal(false)}>
      <Modal.Header closeButton>
        <Modal.Title>Restore User</Modal.Title>
      </Modal.Header>
      <Modal.Body>
        Are you sure you want to restore {selectedCell?.row.values.email}?
      </Modal.Body>
      <Modal.Footer>
        <Button variant="secondary" onClick={() => setShowRestoreModal(false)}>
          Cancel
        </Button>
        <Button variant="primary" onClick={confirmRestore}>
          Confirm Restore
        </Button>
      </Modal.Footer>
    </Modal>
  );
 
  return (
    <>
      <OurTable data={users} columns={columns} testid={"UsersTable"} />
      {suspendModal}
      {restoreModal}
    </>
  );
}