All files / components/Sections SectionsTable.js

100% Statements 76/76
100% Branches 29/29
100% Functions 35/35
100% Lines 76/76

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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314                                          1034x         172x 14463x     172x   171x 171x 171x 19961x   171x     171x         116x 55x   52x               4x       153x 14440x     153x   152x 152x   152x     149x 19917x       149x       4x     3x 1x                   3x   2x       2x     3x   2x 2x       2x 2x     3x 2x 1x       1x           3x   3x 3x           30x   30x             30x     263x         163x           191x               163x         263x           263x         163x         263x         163x       263x         163x       263x         163x       263x         163x       263x         163x             163x                                     28x 4x           1x           24x         163x 163x 16x           1x         147x 85x   62x                       30x   30x   30x                
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;
    const courseSections = sections.filter(
      (section) => section.courseInfo.courseId === courseId,
    );
    const timeLocations = section.section.timeLocations;
 
    // Check if the section number is '0100', indicating a lecture
    if (sectionNumber === "0100") {
      // Filter all sections with the same courseId
      // Stryker disable all
      // Stryker restore all
      // Check if there is only one section for the course
      return courseSections.length === 1;
    } else if (sectionNumber.slice(-2) === "00") {
      // Check if the section has a location to make sure its a course
      return (
        courseSections.length === 1 &&
        typeof timeLocations !== "undefined" &&
        timeLocations.length === 1
      );
    }
  }
 
  return false;
}
export function isLectureWithSections(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 sectionNumberEnd = section.section.section.slice(2);
 
    if (sectionNumberEnd === "00") {
      // 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) => {
  if (response.length < 3) {
    toast(
      `New course Created - id: ${response[0].id} enrollCd: ${response[0].enrollCd}`,
    );
  } else {
    toast(
      `Course ${response[0].enrollCd} replaced old section ${response[2].enrollCd} with new section ${response[1].enrollCd}`,
    );
  }
};
 
export const onError = (error) => {
  const message =
    error.response?.data?.message || "An unexpected error occurred";
  toast.error(message);
};
 
export default function SectionsTable({ sections }) {
  // Stryker restore all
  // Stryker disable BooleanLiteral
  const { data: currentUser } = useCurrentUser();
 
  const mutation = useBackendMutation(
    objectToAxiosParams,
    { onSuccess, onError },
    // 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,
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Info",
      accessor: formatInfoLink,
      Cell: renderInfoLink,
      disableGroupBy: true,
      id: "info",
 
      aggregate: getFirstVal,
      Aggregated: renderInfoLink,
    },
    {
      Header: "Action",
      id: "action",
      accessor: "section.enrollCode",
      disableGroupBy: true,
      // No need for accessor if it's purely for actions like expand/collapse
      Cell: ({ row }) => {
        if (isSection(row.original.section.section) && currentUser.loggedIn) {
          return (
            <div className="d-flex align-items-center gap-2">
              <AddToScheduleModal
                section={row.original}
                quarter={row.original.courseInfo.quarter}
                onAdd={(section, schedule) =>
                  handleAddToSchedule(section, schedule, mutation)
                }
              />
            </div>
          );
        } else {
          return <div data-testid="empty-action-cell"></div>;
        }
      },
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value }, row }) => {
        const testId = `${testid}-cell-row-${row.index}-col-${value}-expand-symbols`;
        if (isLectureWithNoSections(value, sections) && currentUser.loggedIn) {
          return (
            <div className="d-flex align-items-center gap-2">
              <AddToScheduleModal
                section={value}
                quarter={sections[0].courseInfo.quarter}
                onAdd={(section, schedule) =>
                  handleLectureAddToSchedule(section, schedule, mutation)
                }
              />
            </div>
          );
        } else if (!isLectureWithSections(value, sections)) {
          return <div data-testid="empty-action-cell"></div>;
        } else {
          return (
            <span {...row.getToggleRowExpandedProps()} data-testid={testId}>
              {row.isExpanded ? "➖" : "➕"}
            </span>
          );
        }
      },
    },
  ];
 
  // Stryker enable all
 
  const testid = "SectionsTable";
 
  const columnsToDisplay = columns;
 
  return (
    <SectionsTableBase
      data={sections}
      columns={columnsToDisplay}
      testid={testid}
    />
  );
}