diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f28749e3..59bd59c9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ - Refactored the `Times` table to `Time` with a database migration - Extracted `loadGraphData.ts` and `util.ts` (both TypeScript) out of `js/components/graph/Graph.js`, and moved `js/util/util.ts` to `js/components/graph/requirementParser.ts` - Changed SVG parsing to leave the `fill` column empty when inserting hybrid nodes into the database +- Refactored `js/components/grid/course_panel.js.jsx` into separate typescript react components ## [0.8.1] - 2026-08-10 diff --git a/js/components/common/types.ts b/js/components/common/types.ts index 62892d082..c013310a6 100644 --- a/js/components/common/types.ts +++ b/js/components/common/types.ts @@ -46,6 +46,8 @@ export interface MeetingItem { cap: number enrol: number wait: number + code: string + extra?: number } timeData: TimeData[] } diff --git a/js/components/common/utils.ts b/js/components/common/utils.ts index bec066c19..dad402f70 100644 --- a/js/components/common/utils.ts +++ b/js/components/common/utils.ts @@ -1,11 +1,11 @@ -import type { ProgramData, ProgramNotModified } from "./types" +import type { ProgramData, ProgramNotModified, RawCourseData } from "./types" /** * Retrieves a course from file. * @param courseName The course code. This + '.txt' is the name of the file. * @returns Promise object representing the JSON object containing course information * or null if not found. */ -export function getCourse(courseName: string): Promise { +export function getCourse(courseName: string): Promise { "use strict" return fetch("course?name=" + courseName).then(response => { diff --git a/js/components/grid/__tests__/CoursePanel.test.js b/js/components/grid/__tests__/CoursePanel.test.js index 03473209f..fb95af293 100644 --- a/js/components/grid/__tests__/CoursePanel.test.js +++ b/js/components/grid/__tests__/CoursePanel.test.js @@ -1,12 +1,11 @@ import { render, screen, cleanup, waitFor } from "@testing-library/react" import userEvent from "@testing-library/user-event" -import { CoursePanel } from "../course_panel.js.jsx" +import { CoursePanel } from "../course_panel" import fetchMock from "fetch-mock" describe("CoursePanel", () => { const coursePanelProps = { selectedCourses: [], - selectedLectures: [], removeCourse: jest.fn(), clearCourses: jest.fn(), hoverLecture: jest.fn(), diff --git a/js/components/grid/course.tsx b/js/components/grid/course.tsx new file mode 100644 index 000000000..d59afbe83 --- /dev/null +++ b/js/components/grid/course.tsx @@ -0,0 +1,190 @@ +import { type JSX, useState, useEffect, useCallback } from "react" +import { getCourse } from "../common/utils" +import type { SelectedLecture } from "./types" +import type { MeetingItem, TimeData } from "../common/types" +import { SectionList } from "./section_list" + +export interface CourseProps { + courseCode: string + removeCourse: (courseCode: string) => void + hoverLecture: (lecture: SelectedLecture) => void + unhoverLecture: () => void + selectLecture: (lecture: SelectedLecture) => void + displayInfo: (courseId: string) => void +} + +interface ParsedLecture { + courseCode: string + lectureCode: string + session: string + times: TimeData[] +} + +interface ParsedCourse { + courseCode: string + F: ParsedLecture[] + S: ParsedLecture[] + Y: ParsedLecture[] +} + +/** + * A selected course with a delete button, and info button, which displays information + * about the course when clicked. + * Also retrieves the course information. It holds list of F, S and Y lecture, tutorial + * and practical sections for the course. + */ +export function Course({ + courseCode, + removeCourse, + hoverLecture, + unhoverLecture, + selectLecture, + displayInfo, +}: CourseProps): JSX.Element { + const [selected, setSelected] = useState(false) + const [courseInfo, setCourseInfo] = useState({ + courseCode: "", + F: [], + S: [], + Y: [], + }) + + const filterLectureList = useCallback( + (lectures: ParsedLecture[], session: string): ParsedLecture[] => { + return lectures + .filter(lec => lec.session === session) + .sort((firstLec, secondLec) => + firstLec.lectureCode > secondLec.lectureCode ? 1 : -1 + ) + }, + [] + ) + + const toggleSelect = useCallback(() => { + setSelected(s => !s) + }, []) + + const removeCourseCallback = useCallback(() => { + removeCourse(courseCode) + }, [removeCourse, courseCode]) + + const parseLectures = useCallback((lectures: MeetingItem[]): ParsedLecture[] => { + const parsedLectures: ParsedLecture[] = [] + + // Loop through the lecture sections to get each section's session code and lecture times + lectures.forEach(lectureInfo => { + // Check to make sure its not an online section (online sections have course codes beginning with 9) or + // restricted section. Restricted sections have enrollment restricted for a particular group of students, + // but happens at the same time and place as a regular lecture/tutorial section. + if ( + lectureInfo.meetData.section.charAt(3) !== "2" && + lectureInfo.meetData.section.charAt(3) !== "9" + ) { + const lecture: ParsedLecture = { + courseCode: + lectureInfo.meetData.code + + " (" + + lectureInfo.meetData.section.substring(0, 1) + + ")", + lectureCode: + lectureInfo.meetData.section.substring(0, 1) + + lectureInfo.meetData.section.substring(3), + session: lectureInfo.meetData.session, + times: lectureInfo.timeData, + } + parsedLectures.push(lecture) + } + }) + return parsedLectures + }, []) + + useEffect(() => { + getCourse(courseCode) + .then(data => { + const course: ParsedCourse = { + courseCode: "", + F: [], + S: [], + Y: [], + } + + course.courseCode = data.name ? data.name : "" + + const parsedLectures = parseLectures(data.allMeetingTimes) + // Split the lecture sections into Fall, Spring and Years + course.F = filterLectureList(parsedLectures, "F") + course.S = filterLectureList(parsedLectures, "S") + course.Y = filterLectureList(parsedLectures, "Y") + setCourseInfo(course) + }) + .catch(_ => { + console.error(`Course with code ${courseCode} not found`) + setCourseInfo({ + courseCode: "", + F: [], + S: [], + Y: [], + }) + }) + }, []) + + return ( +
  • +
    +
    + + displayInfo(courseCode)} + /> +
    +

    + {courseCode} +

    +
    + {selected && ( +
    + + + +
    + )} +
  • + ) +} diff --git a/js/components/grid/course_entry.tsx b/js/components/grid/course_entry.tsx new file mode 100644 index 000000000..b9581e501 --- /dev/null +++ b/js/components/grid/course_entry.tsx @@ -0,0 +1,36 @@ +import { type JSX, useCallback } from "react" + +export interface CourseEntryProps { + course: string + selectCourse: (courseCode: string) => void + removeCourse: (courseCode: string) => void + selectedCourses: string[] +} + +/** + * Describes a course based on its course code, and whether or not it has been selected + * (If the course is selected, it is a "starred-course"). + */ +export function CourseEntry({ + course, + selectCourse, + removeCourse, + selectedCourses, +}: CourseEntryProps): JSX.Element { + // Check whether the course is already in the selectCourses list. + // Remove the course if it is, or add the course if it is not. + const select = useCallback(() => { + if (selectedCourses.indexOf(course) != -1) { + removeCourse(course) + } else { + selectCourse(course) + } + }, [course, selectedCourses, selectCourse, removeCourse]) + + const classes = selectedCourses.indexOf(course) != -1 ? "starred-course" : "" + return ( +
  • + {course} +
  • + ) +} diff --git a/js/components/grid/course_list.tsx b/js/components/grid/course_list.tsx new file mode 100644 index 000000000..8ee950834 --- /dev/null +++ b/js/components/grid/course_list.tsx @@ -0,0 +1,67 @@ +import { type JSX, useState, useEffect } from "react" +import { CourseEntry } from "./course_entry" + +export interface CourseListProps { + courseFilter: string + selectedCourses: string[] + selectCourse: (courseCode: string) => void + removeCourse: (courseCode: string) => void +} + +/** + * A list of courses in column flow + * Composed of many CourseEntry + */ +export function CourseList({ + courseFilter, + selectedCourses, + selectCourse, + removeCourse, +}: CourseListProps): JSX.Element { + const [courses, setCourses] = useState([]) + + useEffect(() => { + // AJAX requests allow the programmer to: + // 1. update a webpage without refreshing + // 2. Request data from a server AFTER the webpage is loaded + // 3. Send data to the server - in the background + + // This makes an AJAX call to retrieve courses from the database + fetch( + "/courses" // url to which the AJAX request is sent to + ) + .then(response => response.text()) + .then(data => { + // searches through all of the courses in "data", + // and stores each individual course code name + // into 'courses' list + const courses = data.split("\n").map(course => course.substring(0, 8)) + setCourses(courses) + }) + }, []) + + let searchList: React.ReactElement[] = [] + // If there are courses to be filtered + if (courseFilter !== "") { + // From the "courses" list, filter out elements based off of the prop "courseFilter" passed to + // CourseList by SearchPanel + searchList = courses + .filter(course => course.indexOf(courseFilter) > -1) + .map(course => ( + + )) + } + + // Return all the unfiltered courses in the "courses" list in a list + return ( +
    +
      {searchList}
    +
    + ) +} diff --git a/js/components/grid/course_panel.js.jsx b/js/components/grid/course_panel.js.jsx deleted file mode 100644 index bf7b42f2a..000000000 --- a/js/components/grid/course_panel.js.jsx +++ /dev/null @@ -1,359 +0,0 @@ -import { useCallback, useState, useEffect } from "react" -import { CourseModal } from "../common/CourseModal" -import { getCourse } from "../common/utils" - -/** - * Creates a search box and stores the current user input that is in the search box - * Holds courses selected from the search box, and lists of their F, S and Y lecture, tutorial, - * and practical sections. - */ -export function CoursePanel(props) { - const [value, setValue] = useState("") - const [courseInfoId, setCourseInfoId] = useState(null) // The course code to display in the CourseModal - - const handleInput = useCallback(event => { - setValue(event.target.value) - }, []) - - // Only clear all selected courses if the user confirms in the alert - // pop up window. - const clearAllCourses = useCallback(() => { - if (window.confirm("Clear all selected courses?")) { - props.clearCourses() - } - }, [props.clearCourses]) - - const displayInfo = useCallback(courseId => { - setCourseInfoId(courseId) - }, []) - - const courses = props.selectedCourses.map(course => ( - - )) - - return ( -
    -
    -
    false}> - -
    -
    -
    - -
    -
    -
      -
    • -

      Clear All

      -
    • - {courses} -
    -
    - setCourseInfoId(null)} - /> -
    - ) -} - -/** - * A selected course with a delete button, and info button, which displays information - * about the course when clicked. - * Also retrieves the course information. It holds list of F, S and Y lecture, tutorial - * and practical sections for the course. - */ -function Course(props) { - const [selected, setSelected] = useState(false) - const [courseInfo, setCourseInfo] = useState({}) - - const filterLectureList = useCallback((lectures, session) => { - return lectures - .filter(lec => lec.session === session) - .sort((firstLec, secondLec) => - firstLec.lectureCode > secondLec.lectureCode ? 1 : -1 - ) - }, []) - - const toggleSelect = useCallback(() => { - setSelected(s => !s) - }, []) - - const removeCourse = useCallback(() => { - props.removeCourse(props.courseCode) - }, [props.removeCourse, props.courseCode]) - - const parseLectures = useCallback(lectures => { - const parsedLectures = [] - - // Loop through the lecture sections to get each section's session code and lecture times - lectures.forEach(lectureInfo => { - // Check to make sure its not an online section (online sections have course codes beginning with 9) or - // restricted section. Restricted sections have enrollment restricted for a particular group of students, - // but happens at the same time and place as a regular lecture/tutorial section. - if ( - lectureInfo.meetData.section.charAt(3) !== "2" && - lectureInfo.meetData.section.charAt(3) !== "9" - ) { - const lecture = { - courseCode: - lectureInfo.meetData.code + - " (" + - lectureInfo.meetData.section.substring(0, 1) + - ")", - lectureCode: - lectureInfo.meetData.section.substring(0, 1) + - lectureInfo.meetData.section.substring(3), - session: lectureInfo.meetData.session, - times: lectureInfo.timeData, - } - parsedLectures.push(lecture) - } - }) - return parsedLectures - }, []) - - const containsSelectedLecture = useCallback(() => { - // Only use method subString on the value of courseInfo.courseCode if - // if the courseInfo.courseCode exists (ie the course information has already been fetched) - if (courseInfo.courseCode) { - const lectures = props.selectedLectures.map(lecture => - lecture.courseCode.substring(0, 6) - ) - const courseCode = courseInfo.courseCode - return lectures.indexOf(courseCode.substring(0, 6)) >= 0 - } - return false - }, [props.selectedLectures]) - - useEffect(() => { - getCourse(props.courseCode) - .then(data => { - const course = { - courseCode: "", - F: [], - S: [], - Y: [], - } - - course.courseCode = data.name - - const parsedLectures = parseLectures(data.allMeetingTimes) - // Split the lecture sections into Fall, Spring and Years - course.F = filterLectureList(parsedLectures, "F") - course.S = filterLectureList(parsedLectures, "S") - course.Y = filterLectureList(parsedLectures, "Y") - setCourseInfo(course) - }) - .catch(error => { - console.error(`Course with code ${props.courseCode} not found`) - setCourseInfo({ - courseCode: "", - F: [], - S: [], - Y: [], - }) - }) - }, []) - - return ( -
  • -
    -
    - - props.displayInfo(props.courseCode)} - /> -
    -

    - {props.courseCode} -

    -
    - {selected && ( -
    - - - -
    - )} -
  • - ) -} - -/** - * A list of lecture, tutorial and practical sections for the specified course for the specified - * session. - */ -function SectionList(props) { - const lectureSections = props.lectures.map(lecture => ( - - )) - return ( -
      - {lectureSections} -
    - ) -} - -/** - * A lecture, tutorial or practical section for the specified course in the specified - * session. The section is added to or removed from a list of selected lecture upon click. - */ -const LectureSection = props => { - return ( -
  • props.selectLecture(props.lecture)} - onMouseOver={() => props.hoverLecture(props.lecture)} - onMouseOut={() => props.unhoverLecture()} - data-satisfied={"true"} - > - {props.lecture.lectureCode} -
  • - ) -} - -function CourseList(props) { - const [courses, setCourses] = useState([]) - - useEffect(() => { - // AJAX requests allow the programmer to: - // 1. update a webpage without refreshing - // 2. Request data from a server AFTER the webpage is loaded - // 3. Send data to the server - in the background - - // This makes an AJAX call to retrieve courses from the database - fetch( - "/courses" // url to which the AJAX request is sent to - ) - .then(response => response.text()) - .then(data => { - // searches through all of the courses in "data", - // and stores each individual course code name - // into 'courses' list - const courses = data.split("\n").map(course => course.substring(0, 8)) - setCourses(courses) - }) - }, []) - - let searchList = [] - // If there are courses to be filtered - if (props.courseFilter !== "") { - // From the "courses" list, filter out elements based off of the prop "courseFilter" passed to - // CourseList by SearchPanel - searchList = courses - .filter(course => course.indexOf(props.courseFilter) > -1) - .map(course => ( - - )) - } - - // Return all the unfiltered courses in the "courses" list in a list - return ( -
    -
      {searchList}
    -
    - ) -} - -/** - * Describes a course based on its course code, and whether or not it has been selected - * (If the course is selected, it is a "starred-course"). - */ -function CourseEntry(props) { - // Check whether the course is already in the selectCourses list. - // Remove the course if it is, or add the course if it is not. - const select = useCallback(() => { - if (props.selectedCourses.indexOf(props.course) != -1) { - props.removeCourse(props.course) - } else { - props.selectCourse(props.course) - } - }, [props.course, props.selectedCourses, props.selectCourse, props.removeCourse]) - - const classes = - props.selectedCourses.indexOf(props.course) != -1 ? "starred-course" : "" - return ( -
  • - {props.course} -
  • - ) -} diff --git a/js/components/grid/course_panel.tsx b/js/components/grid/course_panel.tsx new file mode 100644 index 000000000..c3987c282 --- /dev/null +++ b/js/components/grid/course_panel.tsx @@ -0,0 +1,100 @@ +import { type JSX, useCallback, useState } from "react" +import { CourseModal } from "../common/CourseModal" +import { CourseList } from "./course_list" +import { Course } from "./course" +import type { SelectedLecture } from "./types" + +export interface CoursePanelProps { + selectedCourses: string[] + removeCourse: (courseCode: string) => void + clearCourses: () => void + hoverLecture: (lecture: SelectedLecture) => void + unhoverLecture: () => void + selectLecture: (lecture: SelectedLecture) => void + selectCourse: (courseCode: string) => void +} + +/** + * Creates a search box and stores the current user input that is in the search box + * Holds courses selected from the search box, and lists of their F, S and Y lecture, tutorial, + * and practical sections. + */ +export function CoursePanel({ + selectedCourses, + removeCourse, + clearCourses, + hoverLecture, + unhoverLecture, + selectLecture, + selectCourse, +}: CoursePanelProps): JSX.Element { + const [value, setValue] = useState("") + const [courseInfoId, setCourseInfoId] = useState(undefined) // The course code to display in the CourseModal + + const handleInput = useCallback((event: React.ChangeEvent) => { + setValue(event.target.value) + }, []) + + // Only clear all selected courses if the user confirms in the alert + // pop up window. + const clearAllCourses = useCallback(() => { + if (window.confirm("Clear all selected courses?")) { + clearCourses() + } + }, [clearCourses]) + + const displayInfo = useCallback((courseId: string) => { + setCourseInfoId(courseId) + }, []) + + const courses = selectedCourses.map(course => ( + + )) + + return ( +
    +
    +
    false}> + +
    +
    +
    + +
    +
    +
      +
    • +

      Clear All

      +
    • + {courses} +
    +
    + setCourseInfoId(undefined)} + /> +
    + ) +} diff --git a/js/components/grid/grid.tsx b/js/components/grid/grid.tsx index 19961a69a..1e31f5150 100644 --- a/js/components/grid/grid.tsx +++ b/js/components/grid/grid.tsx @@ -1,6 +1,6 @@ import { useEffect, useState, useRef } from "react" -import { CoursePanel } from "./course_panel.js.jsx" +import { CoursePanel } from "./course_panel" import { Row } from "./calendar.js.jsx" import { ExportModal, ExportModalHandle } from "../common/export" import Disclaimer from "../common/Disclaimer" @@ -178,7 +178,6 @@ export default function Grid() { void + unhoverLecture: () => void + selectLecture: (lecture: SelectedLecture) => void +} + +/** + * A lecture, tutorial or practical section for the specified course in the specified + * session. The section is added to or removed from a list of selected lecture upon click. + */ +export function LectureSection({ + session, + courseCode, + lecture, + hoverLecture, + unhoverLecture, + selectLecture, +}: LectureSectionProps): JSX.Element { + return ( +
  • selectLecture(lecture)} + onMouseOver={() => hoverLecture(lecture)} + onMouseOut={() => unhoverLecture()} + data-satisfied={"true"} + > + {lecture.lectureCode} +
  • + ) +} diff --git a/js/components/grid/section_list.tsx b/js/components/grid/section_list.tsx new file mode 100644 index 000000000..7fff16724 --- /dev/null +++ b/js/components/grid/section_list.tsx @@ -0,0 +1,42 @@ +import type { JSX } from "react" +import type { SelectedLecture } from "./types" +import { LectureSection } from "./lecture_section" + +export interface SectionListProps { + courseCode: string + session: string + lectures: SelectedLecture[] + hoverLecture: (lecture: SelectedLecture) => void + unhoverLecture: () => void + selectLecture: (lecture: SelectedLecture) => void +} + +/** + * A list of lecture, tutorial and practical sections for the specified course for the specified + * session. + */ +export function SectionList({ + courseCode, + session, + lectures, + hoverLecture, + unhoverLecture, + selectLecture, +}: SectionListProps): JSX.Element { + const lectureSections = lectures.map(lecture => ( + + )) + return ( +
      + {lectureSections} +
    + ) +}