All files / pages/Home HomePageLoggedIn.jsx

100% Statements 16/16
100% Branches 10/10
100% Functions 5/5
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 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                          17x         17x                     17x                       17x   17x                   17x 1x 1x     17x       17x 1x 1x     17x   17x                                                                                              
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 InstructorAdminCoursesTable from "main/components/Courses/InstructorAdminCoursesTable";
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(
    ["/api/courses/list/staff"],
    // Stryker disable next-line StringLiteral : The default value for an empty ("") method is GET.
    { method: "GET", url: "/api/courses/list/staff" },
    // Stryker disable next-line all : don't test default value of empty list
    [],
  );
  const {
    data: instructorCourses,
    error: _instructorError,
    status: _instructorStatus,
  } = useBackend(
    ["/api/courses/list/instructors"],
    // 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/instructors" },
    // Stryker disable next-line all : don't test default value of empty list
    [],
    false,
    {
      enabled: Boolean(hasRole(currentUser, "ROLE_INSTRUCTOR")),
    },
  );
 
  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 }, [
    "/api/courses/list/instructors",
  ]);
 
  const onSubmit = async (data) => {
    data.school = data.school.key;
    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 && (
              <>
                <InstructorAdminCoursesTable
                  courses={instructorCourses}
                  currentUser={currentUser}
                />
              </>
            )}
          </>
        )}
        <h1>Your Student Courses</h1>
        <StudentCoursesTable testid={"CoursesTable"} />
        <h1>Your Staff Courses</h1>
        {staffCourses.length === 0 && <p>No staff courses yet.</p>}
        {staffCourses.length > 0 && (
          <InstructorAdminCoursesTable
            courses={staffCourses}
            currentUser={currentUser}
          />
        )}
      </div>
    </BasicLayout>
  );
}