Skip to content

Commit

Permalink
Select the current term in the schedule display by default (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
39bytes authored Jan 11, 2025
1 parent a8a48b4 commit 811f72c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
13 changes: 10 additions & 3 deletions client/src/components/SchedulesDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { toast } from 'sonner';
import { twMerge } from 'tailwind-merge';

import * as buildingCodes from '../assets/buildingCodes.json';
import { sortTerms } from '../lib/utils';
import { getCurrentTerm, sortTerms } from '../lib/utils';
import type { Course } from '../model/Course';
import type { Block, Schedule } from '../model/Schedule';
import { Tooltip } from './Tooltip';
Expand Down Expand Up @@ -160,6 +160,11 @@ const ScheduleRow = ({ block }: ScheduleRowProps) => {
);
};

const getDefaultTerm = (offeredTerms: string[]) => {
const currentTerm = getCurrentTerm();
return offeredTerms.includes(currentTerm) ? currentTerm : offeredTerms.at(0);
};

type SchedulesDisplayProps = {
course: Course;
className?: string;
Expand All @@ -179,15 +184,17 @@ export const SchedulesDisplay = ({
)
);

const [selectedTerm, setSelectedTerm] = useState(offeredTerms.at(0));
const [selectedTerm, setSelectedTerm] = useState(
getDefaultTerm(offeredTerms)
);
const [showAll, setShowAll] = useState(false);
const scheduleByTerm = useMemo(() => getSections(schedules), [course]);
const [blocks, setBlocks] = useState(
selectedTerm ? scheduleByTerm[selectedTerm] : undefined
);

useEffect(() => {
setSelectedTerm(offeredTerms.at(0));
setSelectedTerm(getDefaultTerm(offeredTerms));
}, [course]);

useEffect(() => {
Expand Down
27 changes: 25 additions & 2 deletions client/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const groupCurrentCourseTermInstructors = (
export const getCurrentTerms = (): [string, string, string] => {
const now = new Date();

const month = now.getMonth() + 1,
year = now.getFullYear();
const month = now.getMonth() + 1;
const year = now.getFullYear();

if (month >= 5 && month < 8) {
return [`Summer ${year}`, `Fall ${year}`, `Winter ${year + 1}`];
Expand All @@ -55,6 +55,29 @@ export const getCurrentTerms = (): [string, string, string] => {
return [`Fall ${year - 1}`, `Winter ${year}`, `Summer ${year}`];
};

/**
* Determines the current academic term based on the current date.
* - May-July: Summer <year>
* - August-December: Fall <year>
* - January-April: Winter <year>
* @returns string The current term
*/
export const getCurrentTerm = (): string => {
const now = new Date();
const month = now.getMonth() + 1;
const year = now.getFullYear();

if (month >= 5 && month < 8) {
return `Summer ${year}`;
}

if (month >= 8) {
return `Fall ${year}`;
}

return `Winter ${year}`;
};

/**
* Compares two academic terms for sorting.
* Terms are compared first by year, then by season according to TERM_ORDER.
Expand Down

0 comments on commit 811f72c

Please sign in to comment.