All files / components/Sections SectionsTable.js

100% Statements 46/46
100% Branches 4/4
100% Functions 28/28
100% Lines 46/46

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257                                          800x         154x 14436x     154x   153x 153x     153x     104x 10931x       104x       50x     3x 1x                   3x   1x       1x     3x   1x 1x       1x 1x     3x 1x                 26x   26x             26x     228x         150x           168x               150x         228x           228x         150x         228x         150x       228x         150x       228x         150x       228x         150x       228x         150x                                                                                                                     26x   26x   26x                
import SectionsTableBase from "main/components/SectionsTableBase";
import AddToScheduleModal from "main/components/PersonalSchedules/AddToScheduleModal";
 
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import { useCurrentUser } from "main/utils/currentUser.js";
 
import { yyyyqToQyy } from "main/utils/quarterUtilities.js";
import {
  convertToFraction,
  formatDays,
  formatInstructors,
  formatLocation,
  formatTime,
  isSection,
  formatStatus,
  formatInfoLink,
  renderInfoLink,
} from "main/utils/sectionUtils.js";
 
function getFirstVal(values) {
  return values[0];
}
 
export function isLectureWithNoSections(enrollCode, sections) {
  // Find the section with the given enrollCode
  const section = sections.find(
    (section) => section.section.enrollCode === enrollCode,
  );
 
  if (section) {
    // Extract the courseId and section number from the found section
    const courseId = section.courseInfo.courseId;
    const sectionNumber = section.section.section;
 
    // Check if the section number is '0100', indicating a lecture
    if (sectionNumber === "0100") {
      // Filter all sections with the same courseId
      // Stryker disable all
      const courseSections = sections.filter(
        (section) => section.courseInfo.courseId === courseId,
      );
      // Stryker restore all
      // Check if there is only one section for the course
      return courseSections.length === 1;
    }
  }
 
  return false;
}
 
export const objectToAxiosParams = (data) => {
  return {
    url: "/api/courses/post",
    method: "POST",
    params: {
      enrollCd: data.enrollCd.toString(),
      psId: data.psId.toString(),
    },
  };
};
 
export const handleAddToSchedule = (section, schedule, mutation) => {
  // Execute the mutation with the provided data
  const dataFinal = {
    enrollCd: section.section.enrollCode,
    psId: schedule,
  };
  mutation.mutate(dataFinal);
};
 
export const handleLectureAddToSchedule = (section, schedule, mutation) => {
  // Execute the mutation with the provided data
  console.log(section);
  const dataFinal = {
    enrollCd: section,
    psId: schedule,
  };
  console.log(dataFinal);
  mutation.mutate(dataFinal);
};
 
export const onSuccess = (response) => {
  toast(
    `New course Created - id: ${response[0].id} enrollCd: ${response[0].enrollCd}`,
  );
};
 
export default function SectionsTable({ sections }) {
  // Stryker restore all
  // Stryker disable BooleanLiteral
 
  const { data: currentUser } = useCurrentUser();
 
  const mutation = useBackendMutation(
    objectToAxiosParams,
    { onSuccess },
    // Stryker disable next-line all : hard to set up test for caching
    ["/api/courses/user/all"],
  );
 
  const columns = [
    {
      Header: "Quarter",
      accessor: (row) => yyyyqToQyy(row.courseInfo.quarter),
      disableGroupBy: true,
      id: "quarter",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Course ID",
      accessor: "courseInfo.courseId",
 
      Cell: ({ cell: { value } }) => value.substring(0, value.length - 2),
    },
    {
      Header: "Title",
      accessor: "courseInfo.title",
      disableGroupBy: true,
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      // Stryker disable next-line StringLiteral: this column is hidden, very hard to test
      Header: "Is Section?",
      accessor: (row) => isSection(row.section.section),
      // Stryker disable next-line StringLiteral: this column is hidden, very hard to test
      id: "isSection",
    },
    {
      Header: "Status",
      accessor: (row) => formatStatus(row.section),
      disableGroupBy: true,
      id: "status",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Enrolled",
      accessor: (row) =>
        convertToFraction(row.section.enrolledTotal, row.section.maxEnroll),
      disableGroupBy: true,
      id: "enrolled",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Location",
      accessor: (row) => formatLocation(row.section.timeLocations),
      disableGroupBy: true,
      id: "location",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Days",
      accessor: (row) => formatDays(row.section.timeLocations),
      disableGroupBy: true,
      id: "days",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Time",
      accessor: (row) => formatTime(row.section.timeLocations),
      disableGroupBy: true,
      id: "time",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Instructor",
      accessor: (row) => formatInstructors(row.section.instructors),
      disableGroupBy: true,
      id: "instructor",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Enroll Code",
      accessor: "section.enrollCode",
      disableGroupBy: true,
      Cell: ({ cell: { value }, row: { original } }) => {
        // Stryker disable all : difficult to test modal interaction
        /* istanbul ignore next : difficult to test modal interaction*/
        if (isSection(original.section.section) && currentUser.loggedIn) {
          return (
            <div className="d-flex align-items-center gap-2">
              <span>{value}</span>
              <AddToScheduleModal
                section={original}
                quarter={original.courseInfo.quarter}
                onAdd={(section, schedule) =>
                  handleAddToSchedule(section, schedule, mutation)
                }
              />
            </div>
          );
        } else {
          return value;
        }
        // Stryker restore all
      },
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => /* istanbul ignore next */ {
        if (isLectureWithNoSections(value, sections) && currentUser.loggedIn) {
          return (
            <div className="d-flex align-items-center gap-2">
              <span>{value}</span>
              <AddToScheduleModal
                section={value}
                quarter={sections[0].courseInfo.quarter}
                onAdd={(section, schedule) =>
                  handleLectureAddToSchedule(section, schedule, mutation)
                }
              />
            </div>
          );
        } else {
          return `${value}`;
        }
      },
    },
    {
      Header: "Info",
      accessor: formatInfoLink,
      Cell: renderInfoLink,
      disableGroupBy: true,
      id: "info",
 
      aggregate: getFirstVal,
      Aggregated: renderInfoLink,
    },
  ];
 
  const testid = "SectionsTable";
 
  const columnsToDisplay = columns;
 
  return (
    <SectionsTableBase
      data={sections}
      columns={columnsToDisplay}
      testid={testid}
    />
  );
}