All files / pages HomePageLoggedIn.jsx

100% Statements 159/159
100% Branches 18/18
100% Functions 13/13
100% Lines 159/159

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 2091x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 31x   31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x   31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x   31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x     31x 2x 2x   31x 2x 2x   31x 4x 4x 4x 4x 4x 4x 4x 4x   31x 2x 2x 2x 2x 2x 2x   31x 31x 31x 31x     31x 31x 31x 31x     31x 4x 4x   31x 2x 2x   31x 17x 17x 2x   17x   31x 17x 17x 2x   17x   31x   31x 1x 1x 1x 1x 1x 1x 1x 1x   31x 1x 1x 1x   31x 31x 31x 31x 31x     31x 1x 1x   31x   31x 31x 31x 31x 31x 31x 31x 31x 31x   31x 31x 31x 31x 31x     31x 31x 31x       31x 31x 31x 31x 31x 31x             31x 31x 13x 13x 13x 13x 13x 13x       31x   31x 31x 31x 31x 31x 31x 31x 31x             31x  
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";
 
export default function HomePageLoggedIn() {
  const currentUser = useCurrentUser();
 
  const {
    data: courses,
    error: _error,
    status: _status,
  } = useBackend(
    // Stryker disable next-line all : don't test internal caching of React Query
    ["/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 {
    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 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 cellToAxiosParamsStaff = (cell) => ({
    url: `/api/coursestaff/joinCourse`,
    method: "PUT",
    params: {
      courseStaffId: cell.row.original.staffId,
    },
  });
 
  const studentJoinMutation = useBackendMutation(
    cellToAxiosParamsStudent,
    { onSuccess: onJoinSuccess, onError: onJoinFail },
    [`/api/courses/list`],
  );
 
  const staffJoinMutation = useBackendMutation(
    cellToAxiosParamsStaff,
    { onSuccess: onJoinSuccess, onError: onJoinFail },
    [`/api/courses/staffCourses`],
  );
 
  const joinStudentCourseCallback = async (cell) => {
    studentJoinMutation.mutate(cell);
  };
 
  const joinStaffCourseCallback = async (cell) => {
    staffJoinMutation.mutate(cell);
  };
 
  const isStudentJoining = (cell) => {
    return (
      studentJoinMutation.isPending &&
      studentJoinMutation.variables.row.index === cell.row.index
    );
  };
 
  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>
        {courses.length > 0 ? (
          <>
            <CoursesTable
              courses={courses}
              testId={"CoursesTable"}
              joinCallback={joinStudentCourseCallback}
              isLoading={isStudentJoining}
            />
          </>
        ) : (
          <p>You are not enrolled in any student courses yet.</p>
        )}
        {staffCourses.length > 0 && (
          <>
            <h1>Your Staff Courses</h1>
            <CoursesTable
              courses={staffCourses}
              testId={"StaffCoursesTable"}
              joinCallback={joinStaffCourseCallback}
              isLoading={isStaffJoining}
            />
          </>
        )}
      </div>
    </BasicLayout>
  );
}