All files / components/Courses StudentCoursesTable.jsx

100% Statements 14/14
100% Branches 6/6
100% Functions 7/7
100% Lines 14/14

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                      63x                                   54x               54x 1x     54x 2x     54x 4x                 54x           54x 4x     54x 31x           54x                  
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import CoursesTable from "main/components/Courses/CoursesTable";
import React from "react";
 
export function StudentCoursesTableView({
  courses,
  testid,
  joinCallback,
  isLoading,
}) {
  return (
    <>
      {courses.length > 0 ? (
        <CoursesTable
          courses={courses}
          testId={testid}
          joinCallback={joinCallback}
          isLoading={isLoading}
          courseNameLinkPrefix="/student/courses"
        />
      ) : (
        <p>You are not enrolled in any student courses yet.</p>
      )}
    </>
  );
}
 
export function StudentCoursesTable({ testid }) {
  const { data: courses } = useBackend(
    ["/api/courses/list"],
    // Stryker disable next-line StringLiteral : The default value for an empty ("") method is GET. Therefore, there is no way to kill a mutation that transforms "GET" to ""
    { method: "GET", url: "/api/courses/list" },
    // Stryker disable next-line all : don't test default value of empty list
    [],
  );
 
  const onJoinSuccess = (message) => {
    toast(message);
  };
 
  const onJoinFail = (result) => {
    toast(result.response.data ? result.response.data : result.message);
  };
 
  const cellToAxiosParamsStudent = (cell) => {
    return {
      url: `/api/rosterstudents/joinCourse`,
      method: "PUT",
      params: {
        rosterStudentId: cell.row.original.rosterStudentId,
      },
    };
  };
 
  const studentJoinMutation = useBackendMutation(
    cellToAxiosParamsStudent,
    { onSuccess: onJoinSuccess, onError: onJoinFail },
    [`/api/courses/list`],
  );
 
  const joinStudentCourseCallback = async (cell) => {
    studentJoinMutation.mutate(cell);
  };
 
  const isStudentJoining = (cell) => {
    return (
      studentJoinMutation.isPending &&
      studentJoinMutation.variables.row.index === cell.row.index
    );
  };
 
  return (
    <StudentCoursesTableView
      courses={courses}
      testid={testid}
      joinCallback={joinStudentCourseCallback}
      isLoading={isStudentJoining}
    />
  );
}