All files / pages HomePageLoggedIn.jsx

100% Statements 121/121
100% Branches 13/13
100% Functions 10/10
100% Lines 121/121

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 1591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x   27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x     27x 4x 4x 4x 4x 4x 4x   27x 1x 1x   27x 2x 2x   27x 27x 27x 27x     27x 4x 4x   27x 12x 12x 2x   12x   27x   27x 1x 1x 1x 1x 1x 1x 1x 1x   27x 1x 1x 1x   27x 27x 27x 27x 27x     27x 1x 1x   27x   27x 27x 27x 27x 27x 27x 27x 27x 27x   27x 27x 27x 27x 27x     27x 27x 27x       27x 27x 27x 27x 27x 27x             27x 27x 27x 27x 27x 27x 27x 27x 27x 27x             27x  
import CoursesTable from "main/components/Courses/CoursesTable";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import { useCurrentUser } from "main/utils/currentUser";
import InstructorCoursesTable from "main/components/Courses/InstructorCoursesTable";
import CourseModal from "main/components/Courses/CourseModal";
import Button from "react-bootstrap/Button";
import React from "react";
import { hasRole } from "main/utils/currentUser";
import { StudentCoursesTable } from "main/components/Courses/StudentCoursesTable";
 
export default function HomePageLoggedIn() {
  const currentUser = useCurrentUser();
  const {
    data: staffCourses,
    error: _staffError,
    status: _staffStatus,
  } = useBackend(
    // Stryker disable next-line all : don't test internal caching of React Query
    ["/api/courses/staffCourses"],
    // Stryker disable next-line StringLiteral : The default value for an empty ("") method is GET.
    { method: "GET", url: "/api/courses/staffCourses" },
    // Stryker disable next-line all : don't test default value of empty list
    [],
  );
  const {
    data: instructorCourses,
    error: _instructorError,
    status: _instructorStatus,
  } = useBackend(
    // Stryker disable next-line all : don't test internal caching of React Query
    ["/api/courses/allForInstructors"],
    // 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/allForInstructors" },
    // Stryker disable next-line all : don't test default value of empty list
    [],
    false,
    {
      enabled: Boolean(hasRole(currentUser, "ROLE_INSTRUCTOR")),
    },
  );
 
  const cellToAxiosParamsStaff = (cell) => ({
    url: `/api/coursestaff/joinCourse`,
    method: "PUT",
    params: {
      courseStaffId: cell.row.original.staffId,
    },
  });
 
  const onJoinSuccess = (message) => {
    toast(message);
  };
 
  const onJoinFail = (result) => {
    toast(result.response.data ? result.response.data : result.message);
  };
 
  const staffJoinMutation = useBackendMutation(
    cellToAxiosParamsStaff,
    { onSuccess: onJoinSuccess, onError: onJoinFail },
    [`/api/courses/staffCourses`],
  );
 
  const joinStaffCourseCallback = async (cell) => {
    staffJoinMutation.mutate(cell);
  };
 
  const isStaffJoining = (cell) => {
    return (
      staffJoinMutation.isPending &&
      staffJoinMutation.variables.row.index === cell.row.index
    );
  };
 
  const [viewModal, setViewModal] = React.useState(false);
 
  const objectToAxiosParams = (course) => ({
    url: "/api/courses/post",
    method: "POST",
    params: {
      courseName: course.courseName,
      term: course.term,
      school: course.school,
    },
  });
 
  const onSuccess = (course) => {
    toast(`Course ${course.courseName} created`);
    setViewModal(false);
  };
 
  const mutation = useBackendMutation(
    objectToAxiosParams,
    { onSuccess },
    // Stryker disable next-line all : don't test internal caching of React Query
    ["/api/courses/allForInstructors"],
  );
 
  const onSubmit = async (data) => {
    mutation.mutate(data);
  };
 
  const createCourse = () => setViewModal(true);
 
  return (
    <BasicLayout>
      <div className="pt-2">
        {hasRole(currentUser, "ROLE_INSTRUCTOR") && (
          <>
            <CourseModal
              showModal={viewModal}
              toggleShowModal={setViewModal}
              onSubmitAction={onSubmit}
            />
            <Button
              onClick={createCourse}
              style={{ float: "right", marginBottom: 10 }}
              variant="primary"
            >
              Create Course
            </Button>
            <h1>Your Instructor Courses</h1>
            {instructorCourses.length === 0 && (
              <p>
                No instructor courses yet. Click the button above to create one.
              </p>
            )}
            {instructorCourses.length > 0 && (
              <>
                <>
                  <InstructorCoursesTable
                    courses={instructorCourses}
                    currentUser={currentUser}
                  />
                </>
              </>
            )}
          </>
        )}
        <h1>Your Student Courses</h1>
        <StudentCoursesTable testid={"CoursesTable"} />
        {staffCourses.length > 0 && (
          <>
            <h1>Your Staff Courses</h1>
            <CoursesTable
              courses={staffCourses}
              testId={"StaffCoursesTable"}
              joinCallback={joinStaffCourseCallback}
              isLoading={isStaffJoining}
            />
          </>
        )}
      </div>
    </BasicLayout>
  );
}