All files / components/RecommendationRequest RecommendationRequestTable.js

100% Statements 27/27
100% Branches 13/13
100% Functions 8/8
100% Lines 27/27

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                                        35x 35x 35x 1x           35x       35x 2x               35x 2x     35x 1x     1x   1x         35x 53x   53x 1x 1x     53x 6x                       47x               35x                                                                                                         35x 29x                   35x       26x                   35x                
import React from "react";
import OurTable, { ButtonColumn } from "main/components/OurTable";
 
import { useBackendMutation } from "main/utils/useBackend";
import {
  cellToAxiosParamsDelete,
  onDeleteSuccess,
  cellToAxiosParamsUpdateStatus,
  onUpdateStatusSuccess,
} from "main/utils/RecommendationRequestUtils";
import { useNavigate } from "react-router-dom";
import { hasRole } from "main/utils/currentUser";
import { Dropdown, DropdownButton } from "react-bootstrap";
import { useQueryClient } from "react-query";
 
export default function RecommendationRequestTable({
  requests,
  currentUser,
  onPendingRequests = false,
}) {
  const navigate = useNavigate();
  const queryClient = useQueryClient();
  const editCallback = (cell) => {
    navigate(`/requests/edit/${cell.row.values.id}`);
  };
 
  // Stryker disable all : hard to test for query caching
 
  // when delete success, invalidate the correct query key (depending on user role)
  const apiEndpoint = hasRole(currentUser, "ROLE_PROFESSOR")
    ? "/api/recommendationrequest/professor/all"
    : "/api/recommendationrequest/requester/all";
 
  const deleteMutation = useBackendMutation(
    (cell) => cellToAxiosParamsDelete(cell, hasRole(currentUser, "ROLE_ADMIN")),
    { onSuccess: onDeleteSuccess },
    [apiEndpoint],
  );
 
  // Stryker restore all
 
  // Stryker disable next-line all : TODO try to make a good test for this
  const deleteCallback = async (cell) => {
    deleteMutation.mutate(cell);
  };
 
  const updateStatusMutation = useBackendMutation(
    (params) => cellToAxiosParamsUpdateStatus(params.cell, params.newStatus),
    {
      onSuccess: (message) => {
        onUpdateStatusSuccess(message);
        // Refreshes the page immediately when status is changed by requiring a new GET call
        queryClient.invalidateQueries(apiEndpoint);
      },
    },
  );
 
  const StatusCell = ({ cell }) => {
    const [status, setStatus] = React.useState(cell.row.values.status);
 
    const handleClick = (eventKey) => {
      setStatus(eventKey);
      updateStatusMutation.mutate({ cell, newStatus: eventKey });
    };
 
    if (onPendingRequests && hasRole(currentUser, "ROLE_PROFESSOR")) {
      return (
        <DropdownButton
          title={status}
          data-testid={`status-dropdown-${cell.row.values.id}`}
          onSelect={handleClick}
        >
          <Dropdown.Item eventKey="PENDING">PENDING</Dropdown.Item>
          <Dropdown.Item eventKey="COMPLETED">COMPLETED</Dropdown.Item>
          <Dropdown.Item eventKey="DENIED">DENIED</Dropdown.Item>
        </DropdownButton>
      );
    } else {
      return (
        <span data-testid={`status-span-${cell.row.values.id}`}>
          {cell.row.values.status}
        </span>
      );
    }
  };
 
  const columns = [
    {
      Header: "id",
      accessor: "id", // accessor is the "key" in the data
    },
    {
      Header: "Professor Name",
      accessor: "professor.fullName",
    },
    {
      Header: "Professor Email",
      accessor: "professor.email",
    },
    {
      Header: "Requester Name",
      accessor: "requester.fullName",
    },
    {
      Header: "Requester Email",
      accessor: "requester.email",
    },
    {
      Header: "Recommendation Type",
      accessor: "recommendationType",
    },
    {
      Header: "Details",
      accessor: "details",
    },
    {
      Header: "Status",
      accessor: "status",
      Cell: StatusCell,
    },
    {
      Header: "Submission Date",
      accessor: "submissionDate",
    },
    {
      Header: "Last Modified Date",
      accessor: "lastModifiedDate",
    },
    {
      Header: "Completion Date",
      accessor: "completionDate",
    },
    {
      Header: "Due Date",
      accessor: "dueDate",
    },
  ];
 
  //since all admins have the role of a user, we can just check if the current user has the role ROLE_USER
  if (hasRole(currentUser, "ROLE_USER")) {
    columns.push(
      ButtonColumn(
        "Delete",
        "danger",
        deleteCallback,
        "RecommendationRequestTable",
      ),
    );
  }
 
  if (
    hasRole(currentUser, "ROLE_USER") &&
    !hasRole(currentUser, "ROLE_ADMIN")
  ) {
    columns.push(
      ButtonColumn(
        "Edit",
        "primary",
        editCallback,
        "RecommendationRequestTable",
      ),
    );
  }
 
  return (
    <OurTable
      data={requests}
      columns={columns}
      testid={"RecommendationRequestTable"}
    />
  );
}