All files / components/Courses CoursesTable.jsx

100% Statements 127/127
100% Branches 24/24
100% Functions 6/6
100% Lines 127/127

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 1621x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 38x 38x 38x 38x 38x 38x 2x 2x 2x 2x   38x 42x   42x 42x 6x 6x 6x 42x 6x 6x 6x 42x 6x 6x 6x 42x 6x 6x 6x 42x 12x 12x 12x 42x 6x 6x 42x 42x 42x 42x     42x   38x 38x 38x 38x 38x 38x 232x 232x 37x 37x 37x 37x   37x     232x 47x 47x 47x 47x 47x   47x 47x 47x 47x 47x 47x   47x 3x 3x 3x 3x 3x 3x 3x       47x           195x 37x 37x 37x 37x   37x 37x 37x 37x 37x 37x           148x 37x 37x 37x     111x 37x 37x 37x     37x 37x 37x 37x 37x   37x     37x 38x 38x 38x 38x   38x  
import OurTable from "main/components/OurTable";
import { Tooltip, OverlayTrigger, Button, Spinner } from "react-bootstrap";
 
const columns = [
  {
    header: "id",
    accessorKey: "id", // accessor is the "key" in the data
  },
  {
    header: "Course Name",
    accessorKey: "courseName",
  },
  {
    header: "Term",
    accessorKey: "term",
  },
  {
    header: "School",
    accessorKey: "school",
  },
];
 
export default function CoursesTable({
  courses,
  testId,
  joinCallback,
  isLoading,
}) {
  const viewInviteCallback = (cell) => {
    const organizationName = cell.row.original.orgName;
    const gitInvite = `https://github.com/orgs/${organizationName}/invitation`;
    window.open(gitInvite, "_blank");
  };
 
  const renderTooltip = (studentStatus) => (props) => {
    let set_message;
 
    switch (studentStatus) {
      case "PENDING":
        set_message =
          "This course has not been completely set up by your instructor yet.";
        break;
      case "JOINCOURSE":
        set_message =
          "Clicking this button will generate an invitation to the GitHub organization associated with this course.";
        break;
      case "INVITED":
        set_message =
          "You have been invited to the GitHub organization associated with this course, but you still need to accept or decline the invitation. Please accept it if you plan to stay enrolled, and decline only if you plan to withdraw from the course.";
        break;
      case "OWNER":
        set_message =
          "You are an owner of the GitHub organization associated with this course.";
        break;
      case "MEMBER":
        set_message =
          "You are a member of the GitHub organization associated with this course.";
        break;
      default:
        set_message = "Tooltip for illegal status that will never occur";
        break;
    }
    return (
      <Tooltip id={`${studentStatus.toLowerCase()}-tooltip`} {...props}>
        {set_message}
      </Tooltip>
    );
  };
 
  const columnsWithStatus = [
    ...columns,
    {
      header: "Status",
      accessorKey: "studentStatus",
      cell: ({ cell }) => {
        const status = cell.row.original.studentStatus;
        if (status === "PENDING") {
          return (
            <OverlayTrigger
              placement="right"
              overlay={renderTooltip("PENDING")}
            >
              <span className="text-warning">Pending</span>
            </OverlayTrigger>
          );
        } else if (status === "JOINCOURSE") {
          const cellIsLoading = isLoading(cell);
          return (
            <OverlayTrigger
              placement="right"
              overlay={renderTooltip("JOINCOURSE")}
            >
              <span>
                <Button
                  variant={"primary"}
                  onClick={() => joinCallback(cell)}
                  data-testid={`${testId}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
                  disabled={cellIsLoading}
                >
                  {cellIsLoading ? (
                    <>
                      <Spinner
                        as="span"
                        animation="grow"
                        size="sm"
                        role="status"
                      />
                      Joining...
                    </>
                  ) : (
                    <>Join Course</>
                  )}
                </Button>
              </span>
            </OverlayTrigger>
          );
        } else if (status === "INVITED") {
          return (
            <OverlayTrigger
              placement="right"
              overlay={renderTooltip("INVITED")}
            >
              <span>
                <Button
                  variant={"primary"}
                  onClick={() => viewInviteCallback(cell)}
                  data-testid={`${testId}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
                >
                  View Invite
                </Button>
              </span>
            </OverlayTrigger>
          );
        } else if (status === "OWNER") {
          return (
            <OverlayTrigger placement="right" overlay={renderTooltip("OWNER")}>
              <span className="text-info">Owner</span>
            </OverlayTrigger>
          );
        } else if (status === "MEMBER") {
          return (
            <OverlayTrigger placement="right" overlay={renderTooltip("MEMBER")}>
              <span className="text-primary">Member</span>
            </OverlayTrigger>
          );
        }
        return (
          <OverlayTrigger
            placement="right"
            overlay={renderTooltip(cell.row.original.studentStatus)}
          >
            <span>{status}</span>
          </OverlayTrigger>
        );
      },
    },
  ];
  return (
    <OurTable data={courses} columns={columnsWithStatus} testid={testId} />
  );
}