All files / components/Common ConvertedSectionExpandableTable.jsx

100% Statements 27/27
100% Branches 17/17
100% Functions 17/17
100% Lines 26/26

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                            10x   10x 14x 14x   14x 10x                   14x 9x   5x       10x             10x 10x     15x                 19x                               19x         19x         19x         19x         19x         19x         19x         19x         19x         19x         19x       10x                    
import SectionsTableBase from "main/components/SectionsTableBase";
import { yyyyqToQyy } from "main/utils/quarterUtilities";
import {
  formatDays,
  formatInstructors,
  formatLocation,
  formatTime,
  formatStatus,
  enrollmentFraction,
  getSection,
  getSectionField,
} from "main/utils/sectionUtils.jsx";
 
function convertToPrimaryRows(sections) {
  const groups = {};
 
  sections.forEach(({ courseInfo, section }) => {
    const lectureGroup = Math.floor(Number(section.section) / 100);
    const key = `${courseInfo.quarter}-${courseInfo.courseId}-${lectureGroup}`;
 
    if (!groups[key]) {
      groups[key] = {
        quarter: courseInfo.quarter,
        courseId: courseInfo.courseId,
        title: courseInfo.title,
        description: courseInfo.description,
        primary: null,
        subRows: [],
      };
    }
 
    if (section.section.endsWith("00")) {
      groups[key].primary = section;
    } else {
      groups[key].subRows.push(section);
    }
  });
 
  return Object.values(groups).filter((group) => group.primary);
}
 
function ConvertedSectionExpandableTable({
  sections,
  testid = "ConvertedSectionExpandableTable",
}) {
  const convertedSections = convertToPrimaryRows(sections);
  const columns = [
    {
      id: "expander",
      header: ({ table }) => (
        <button
          data-testid={`${testid}-expand-all-rows`}
          onClick={table.getToggleAllRowsExpandedHandler()}
        >
          {table.getIsAllRowsExpanded() ? "➖" : "➕"}
        </button>
      ),
      cell: ({ row }) =>
        row.getCanExpand() ? (
          <button
            data-testid={`${testid}-row-${row.index}-expand-button`}
            onClick={row.getToggleExpandedHandler()}
            style={{ cursor: "pointer" }}
          >
            {row.getIsExpanded() ? "➖" : "➕"}
          </button>
        ) : (
          <span data-testid={`${testid}-row-${row.index}-cannot-expand`} />
        ),
    },
    {
      header: "Quarter",
      accessorKey: "quarter",
      cell: ({ row }) =>
        row.original.quarter ? yyyyqToQyy(row.original.quarter) : "",
    },
    {
      header: "CourseId",
      accessorKey: "courseId",
      cell: ({ row }) => row.original.courseId ?? "",
    },
    {
      header: "Title",
      accessorKey: "title",
      cell: ({ row }) => row.original.title ?? "",
    },
    {
      header: "EnrollCd",
      accessorKey: "enrollCode",
      cell: ({ row }) => getSectionField(row, "enrollCode"),
    },
    {
      header: "Status",
      accessorKey: "status",
      cell: ({ row }) => formatStatus(getSection(row)),
    },
    {
      header: "Enrolled",
      accessorKey: "enrolled",
      cell: ({ row }) => enrollmentFraction(row),
    },
    {
      header: "Days",
      accessorKey: "days",
      cell: ({ row }) => formatDays(getSection(row).timeLocations),
    },
    {
      header: "Time",
      accessorKey: "time",
      cell: ({ row }) => formatTime(getSection(row).timeLocations),
    },
    {
      header: "Location",
      accessorKey: "location",
      cell: ({ row }) => formatLocation(getSection(row).timeLocations),
    },
    {
      header: "Instructors",
      accessorKey: "instructors",
      cell: ({ row }) => formatInstructors(getSection(row).instructors),
    },
    {
      header: "Section",
      accessorKey: "section",
      cell: ({ row }) => getSectionField(row, "section"),
    },
  ];
 
  return (
    <SectionsTableBase
      data={convertedSections}
      columns={columns}
      testid={testid}
    />
  );
}
 
export default ConvertedSectionExpandableTable;