All files / components/Users UsersTable.jsx

100% Statements 38/38
100% Branches 13/13
100% Functions 11/11
100% Lines 38/38

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            3x 23x 1x     22x 22x 22x 22x 22x 22x 22x 22x   22x 19x     3x 2x     1x     3x                                       71x         71x                         71x 23x 23x 22x   1x   48x           42x 1x                 1x           42x 1x                 1x           42x           1x               42x           1x                                         3x 3x 3x  
import OurTable from "main/components/OurTable";
import { Button } from "react-bootstrap";
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import PropTypes from "prop-types";
 
const formatApprovalDate = (dateApproved) => {
  if (typeof dateApproved !== "string") {
    return null;
  }
 
  const dateParts = dateApproved.split("-");
  const yearString = dateParts[0];
  const monthString = dateParts[1];
  const dayString = dateParts[2];
  const year = Number(yearString);
  const month = Number(monthString);
  const day = Number(dayString);
  const date = new Date(Date.UTC(year, month - 1, day));
 
  if (Number.isNaN(date.getTime())) {
    return null;
  }
 
  if (date.toISOString().slice(0, 10) !== dateApproved) {
    return null;
  }
 
  return `${month}/${day}/${year}`;
};
 
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: "Admin",
    id: "admin",
    accessor: (row, _rowIndex) => String(row.admin), // hack needed for boolean values to show up
  },
  {
    Header: "Moderator",
    id: "moderator",
    accessor: (row, _rowIndex) => String(row.moderator),
  },
  {
    Header: "Alias",
    accessor: "alias",
  },
  {
    Header: "Proposed Alias",
    accessor: "proposedAlias",
  },
  {
    Header: "Status",
    accessor: (row) => {
      if (row.status === "APPROVED") {
        const formattedDate = formatApprovalDate(row.dateApproved);
        if (!formattedDate) {
          return row.status;
        }
        return `Approved on ${formattedDate}`;
      }
      return row.status;
    },
  },
];
 
export default function UsersTable({ users, showToggleButtons = false }) {
  const toggleAdminMutation = useBackendMutation(
    (cell) => ({
      url: "/api/admin/toggleAdmin",
      method: "PUT",
      params: {
        id: cell.row.values.id,
      },
    }),
    {
      onSuccess: () => {
        toast("Admin status toggled");
      },
    },
    ["/api/admin/users"],
  );
 
  const toggleModeratorMutation = useBackendMutation(
    (cell) => ({
      url: "/api/admin/toggleModerator",
      method: "PUT",
      params: {
        id: cell.row.values.id,
      },
    }),
    {
      onSuccess: () => {
        toast("Moderator status toggled");
      },
    },
    ["/api/admin/users"],
  );
 
  const toggleAdminColumn = {
    Header: "Toggle Admin",
    id: "toggle-admin",
    Cell: (cell) => (
      <Button
        variant="primary"
        onClick={() => toggleAdminMutation.mutate(cell)}
        data-testid={`UsersTable-cell-row-${cell.row.index}-col-toggle-admin-button`}
      >
        Toggle Admin
      </Button>
    ),
  };
 
  const toggleModeratorColumn = {
    Header: "Toggle Moderator",
    id: "toggle-moderator",
    Cell: (cell) => (
      <Button
        variant="primary"
        onClick={() => toggleModeratorMutation.mutate(cell)}
        data-testid={`UsersTable-cell-row-${cell.row.index}-col-toggle-moderator-button`}
      >
        Toggle Moderator
      </Button>
    ),
  };
 
  return (
    <OurTable
      data={users}
      columns={
        showToggleButtons
          ? [...columns, toggleAdminColumn, toggleModeratorColumn]
          : columns
      }
      testid={"UsersTable"}
    />
  );
}
 
UsersTable.propTypes = Object.create(null);
UsersTable.propTypes.users = PropTypes.array.isRequired;
UsersTable.propTypes.showToggleButtons = PropTypes.bool;