All files / components/Common ConvertedSectionTable.jsx

100% Statements 50/50
100% Branches 17/17
100% Functions 22/22
100% Lines 48/48

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                          4x   4x       87x         87x         87x         87x         87x         87x                 87x         87x         87x         87x         87x         87x       4x   18x                   42x                           4x     56x       16x       28x         9x   9x 28x 28x 16x     12x 12x 8x   12x     9x   9x 28x 28x   28x 12x     16x 16x 16x   14x   28x           28x       9x 9x       9x                         52x 9x     42x        
import { useMemo } from "react";
import OurTable from "main/components/OurTable";
import SectionsTableBase from "main/components/SectionsTableBase";
import { yyyyqToQyy } from "main/utils/quarterUtilities";
import {
  formatDays,
  formatInstructors,
  formatLocation,
  formatTime,
  formatStatus,
  convertToFraction,
} from "main/utils/sectionUtils.jsx";
 
const TESTID = "ConvertedSectionTable";
 
const BASE_COLUMNS = [
  {
    header: "Quarter",
    accessorKey: "quarter",
    cell: ({ row }) => yyyyqToQyy(row.original.courseInfo.quarter),
  },
  {
    header: "CourseId",
    accessorKey: "courseId",
    cell: ({ row }) => row.original.courseInfo.courseId,
  },
  {
    header: "Title",
    accessorKey: "title",
    cell: ({ row }) => row.original.courseInfo.title,
  },
  {
    header: "EnrollCd",
    accessorKey: "enrollCode",
    cell: ({ row }) => row.original.section.enrollCode,
  },
  {
    header: "Status",
    accessorKey: "status",
    cell: ({ row }) => formatStatus(row.original.section),
  },
  {
    header: "Enrolled",
    accessorKey: "enrolled",
    cell: ({ row }) =>
      convertToFraction(
        row.original.section.enrolledTotal,
        row.original.section.maxEnroll,
      ),
  },
  {
    header: "Days",
    accessorKey: "days",
    cell: ({ row }) => formatDays(row.original.section.timeLocations),
  },
  {
    header: "Time",
    accessorKey: "time",
    cell: ({ row }) => formatTime(row.original.section.timeLocations),
  },
  {
    header: "Location",
    accessorKey: "location",
    cell: ({ row }) => formatLocation(row.original.section.timeLocations),
  },
  {
    header: "Instructors",
    accessorKey: "instructors",
    cell: ({ row }) => formatInstructors(row.original.section.instructors),
  },
  {
    header: "Section",
    accessorKey: "section",
    cell: ({ row }) => row.original.section.section,
  },
  {
    header: "Summer Session",
    accessorKey: "summer_session",
    cell: ({ row }) => row.original.section.session?.replace(/^0+/, ""),
  },
];
 
const EXPANDER_COLUMN = {
  id: "expander",
  header: ({ table }) => (
    <button
      data-testid={`${TESTID}-expand-all-rows`}
      type="button"
      onClick={table.getToggleAllRowsExpandedHandler()}
    >
      {table.getIsAllRowsExpanded() ? "➖" : "➕"}
    </button>
  ),
  cell: ({ row }) =>
    row.getCanExpand() ? (
      <button
        data-testid={`${TESTID}-row-${row.index}-expand-button`}
        type="button"
        onClick={row.getToggleExpandedHandler()}
        style={{ cursor: "pointer" }}
      >
        {row.getIsExpanded() ? "➖" : "➕"}
      </button>
    ) : (
      <span data-testid={`${TESTID}-row-${row.index}-cannot-expand`} />
    ),
};
 
const GROUPED_COLUMNS = [EXPANDER_COLUMN, ...BASE_COLUMNS];
 
function isLectureSectionCode(sectionCode) {
  return sectionCode.endsWith("00");
}
 
function getLectureSectionPrefix(sectionCode) {
  return sectionCode.replace(/0+$/, "");
}
 
function courseKey(quarter, courseId) {
  return `${quarter}|${courseId}`;
}
 
// Function that groups sections with the right lecture from the original sections object
function buildGroupedSectionRows(flatSections) {
  const sectionsByCourse = new Map();
 
  for (let j = 0; j < flatSections.length; j++) {
    const entry = flatSections[j];
    if (isLectureSectionCode(entry.section.section)) {
      continue;
    }
 
    const key = courseKey(entry.courseInfo.quarter, entry.courseInfo.courseId);
    if (!sectionsByCourse.has(key)) {
      sectionsByCourse.set(key, []);
    }
    sectionsByCourse.get(key).push(entry);
  }
 
  const result = [];
 
  for (let i = 0; i < flatSections.length; i++) {
    const entry = flatSections[i];
    const sectionCode = entry.section.section;
 
    if (!isLectureSectionCode(sectionCode)) {
      continue;
    }
 
    const { quarter, courseId } = entry.courseInfo;
    const prefix = getLectureSectionPrefix(sectionCode);
    const subRows = (
      sectionsByCourse.get(courseKey(quarter, courseId)) || []
    ).filter((section) => section.section.section.startsWith(prefix));
 
    result.push({
      ...entry,
      subRows,
    });
  }
 
  return result;
}
 
function GroupedConvertedSectionTable({ sections }) {
  const groupedData = useMemo(
    () => buildGroupedSectionRows(sections),
    [sections],
  );
 
  return (
    <SectionsTableBase
      data={groupedData}
      columns={GROUPED_COLUMNS}
      testid={TESTID}
    />
  );
}
 
function ConvertedSectionTable({
  sections,
  groupSectionsUnderLectures = false,
}) {
  if (groupSectionsUnderLectures) {
    return <GroupedConvertedSectionTable sections={sections} />;
  }
 
  return <OurTable data={sections} columns={BASE_COLUMNS} testid={TESTID} />;
}
 
export default ConvertedSectionTable;