All files / components/Users UsersTable.js

100% Statements 30/30
100% Branches 3/3
100% Functions 13/13
100% Lines 30/30

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              3x 72x                                       72x                                   36x 36x 36x       1x                   36x               36x 2x   2x     36x   1x 1x         1x                   36x               36x 2x   2x     36x   1x 1x     36x                                       90x         90x         90x       36x         36x                   1x             1x              
import React, {useState} from "react";
import Modal from "react-modal";
import OurTable, { ButtonColumn } from "main/components/OurTable"
import { formatTime } from "main/utils/dateUtils";
import { useBackendMutation } from "main/utils/useBackend";
 
// Stryker disable all
const ConfirmationModal = ({ isOpen, onClose, onConfirm, message }) => {
    const customStyles = {
        content: {
        top: "50%",
        left: "50%",
        right: "auto",
        bottom: "auto",
        marginRight: "-50%",
        transform: "translate(-50%, -50%)",
        width: "50%", // Adjust the width as needed
        maxWidth: "400px", // Limit the maximum width
        padding: "20px",
        borderRadius: "8px",
        boxShadow: "0 4px 8px rgba(0, 0, 0, 0.2)",
        background: "#fff",
        },
        overlay: {
        background: "rgba(0, 0, 0, 0.5)",
        },
    };
    
    return (
        <Modal
        isOpen={isOpen}
        onRequestClose={onClose}
        style={customStyles}
        contentLabel="Confirmation Modal"
        >
        <div>{message}</div>
        <button onClick={onConfirm}>Yes</button>
        <button onClick={onClose}>No</button>
        </Modal>
    );
};
// Stryker restore all 
 
export default function UsersTable({ users, showToggleButtons = false }) {
 
  // Stryker disable next-line all : TA told me to
  const [InstructorConfirmationModalOpen, setInstructorConfirmationModalOpen] = useState(false);
  const [AdminConfirmationModalOpen, setAdminConfirmationModalOpen] = useState(false);
  const [selectedCell, setSelectedCell] = useState(null);
 
  // toggleAdmin
  function cellToAxiosParamsToggleAdmin(cell) {
      return {
          url: "/api/admin/users/toggleAdmin",
          method: "POST",
          params: {
              githubId: cell.row.values.githubId
          }
      }
  }
 
  // Stryker disable all : hard to test for query caching
  const toggleAdminMutation = useBackendMutation(
    cellToAxiosParamsToggleAdmin,
    {},
    ["/api/admin/users"]
  );
  // Stryker restore all
 
  // Stryker disable next-line all : TODO try to make a good test for this
  const toggleAdminCallback = async (cell) => {
      setSelectedCell(cell);
      // Stryker disable next-line all : TA told me to
      setAdminConfirmationModalOpen(true);
    };
 
    const handleToggleAdminConfirmed = () => {
      // Stryker disable next-line all : TA told me to
      setAdminConfirmationModalOpen(false);
      toggleAdminMutation.mutate(selectedCell);
  };
 
  // toggleInstructor
  function cellToAxiosParamsToggleInstructor(cell) {
    return {
      url: "/api/admin/users/toggleInstructor",
      method: "POST",
      params: {
        githubId: cell.row.values.githubId,
      },
    };
  }
 
  // Stryker disable all : hard to test for query caching
  const toggleInstructorMutation = useBackendMutation(
    cellToAxiosParamsToggleInstructor,
    {},
    ["/api/admin/users"]
  );
  // Stryker restore all
 
  // Stryker disable next-line all : TODO try to make a good test for this
  const toggleInstructorCallback = async (cell) => {
      setSelectedCell(cell);
      // Stryker disable next-line all : TA told me to
      setInstructorConfirmationModalOpen(true);
    };
 
    const handleToggleInstructorConfirmed = () => {
      // Stryker disable next-line all : TA told me to
      setInstructorConfirmationModalOpen(false);
      toggleInstructorMutation.mutate(selectedCell);
  };
 
  const columns = [
    {
      Header: "githubId",
      accessor: "githubId", // accessor is the "key" in the data
    },
    {
      Header: "githubLogin",
      accessor: "githubLogin", // accessor is the "key" in the data
    },
    {
      Header: "fullName",
      accessor: "fullName",
    },
    {
      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: "Instructor",
      id: "instructor",
      accessor: (row, _rowIndex) => String(row.instructor), // hack needed for boolean values to show up
    },
  ];
 
  const buttonColumn = [
      ...columns,
      ButtonColumn("toggle-admin", "primary", toggleAdminCallback, "UsersTable"),
      ButtonColumn("toggle-instructor", "primary", toggleInstructorCallback, "UsersTable")
  ]
  return (
      <>
        <OurTable
          data={users}
          columns={showToggleButtons ? buttonColumn : columns}
          testid={"UsersTable"}
        />
        <ConfirmationModal
          isOpen={AdminConfirmationModalOpen}
          // Stryker disable next-line all : TA told me to
          onClose={() => setAdminConfirmationModalOpen(false)}
          onConfirm={handleToggleAdminConfirmed}
          message="Are you sure you want to toggle this user's admin role?"
        />
        <ConfirmationModal
          isOpen={InstructorConfirmationModalOpen}
          // Stryker disable next-line all : TA told me to
          onClose={() => setInstructorConfirmationModalOpen(false)}
          onConfirm={handleToggleInstructorConfirmed}
          message="Are you sure you want to toggle this user's instructor role?"
        />
      </>
    );
};