All files / components/Courses StudentCoursesTable.jsx

100% Statements 13/13
100% Branches 6/6
100% Functions 6/6
100% Lines 13/13

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            53x               53x 1x     53x 2x     53x 4x                 53x           53x 4x     53x 31x           53x                                  
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 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 (
    <>
      {courses.length > 0 ? (
        <>
          <CoursesTable
            courses={courses}
            testId={testid}
            joinCallback={joinStudentCourseCallback}
            isLoading={isStudentJoining}
          />
        </>
      ) : (
        <p>You are not enrolled in any student courses yet.</p>
      )}
    </>
  );
}