Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the ui displaying last year's instructors #576

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 63 additions & 60 deletions client/src/components/CourseTerms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Link } from 'react-router-dom';
import { twMerge } from 'tailwind-merge';

import {
compareTerms,
getCurrentTerms,
groupCurrentCourseTermInstructors,
} from '../lib/utils';
Expand Down Expand Up @@ -98,69 +99,71 @@ export const CourseTerms = ({ course, variant, query }: CourseTermsProps) => {

return (
<div className='mr-auto flex flex-wrap gap-x-2'>
{instructorGroups.map(([term, instructors], i) => {
const season = term.split(' ')[0].toLowerCase();
return (
<div className='relative' key={term}>
<div
className={twMerge(
'my-1.5 flex text-sm dark:bg-neutral-700',
variant === 'small' ? 'px-2 py-1' : 'max-w-fit px-2 py-1',
termColorMap[season],
expandedState[i] ? 'rounded-xl' : 'rounded-full'
)}
>
{instructors.length > 0 ? (
<div className='flex flex-col gap-y-1'>
{(expandedState[i]
? instructors
: instructors.slice(0, 1)
).map((ins) => (
<Link
key={ins.name}
to={`/instructor/${encodeURIComponent(ins.name)}`}
>
<div className='flex items-center space-x-1.5 whitespace-nowrap'>
<SeasonIcon term={term} variant={variant} />
<div className='pr-1 font-medium dark:text-gray-200'>
<Highlight
text={ins.name}
query={query || undefined}
/>
{instructorGroups
SamZhang02 marked this conversation as resolved.
Show resolved Hide resolved
.sort((a, b) => compareTerms(a[0], b[0]))
SamZhang02 marked this conversation as resolved.
Show resolved Hide resolved
.map(([term, instructors], i) => {
const season = term.split(' ')[0].toLowerCase();
return (
<div className='relative' key={term}>
<div
className={twMerge(
'my-1.5 flex text-sm dark:bg-neutral-700',
variant === 'small' ? 'px-2 py-1' : 'max-w-fit px-2 py-1',
termColorMap[season],
expandedState[i] ? 'rounded-xl' : 'rounded-full'
)}
>
{instructors.length > 0 ? (
<div className='flex flex-col gap-y-1'>
{(expandedState[i]
? instructors
: instructors.slice(0, 1)
).map((ins) => (
<Link
key={ins.name}
to={`/instructor/${encodeURIComponent(ins.name)}`}
>
<div className='flex items-center space-x-1.5 whitespace-nowrap'>
<SeasonIcon term={term} variant={variant} />
<div className='pr-1 font-medium dark:text-gray-200'>
<Highlight
text={ins.name}
query={query || undefined}
/>
</div>
</div>
</div>
</Link>
))}
</div>
) : (
<div className='flex items-center space-x-1.5 whitespace-nowrap'>
<SeasonIcon term={term} variant={variant} />
<div className={'pr-1 font-medium dark:text-gray-200'}>
No Instructor Assigned
</Link>
))}
</div>
</div>
)}
{instructors.length > 1 && (
<span
className='cursor-pointer font-semibold dark:text-gray-200'
onClick={() => handleToggle(i)}
>
+{instructors.length - 1}
{variant === 'large' && (
<ChevronDown
className={twMerge(
'ml-1 inline-block',
expandedState[i] ? 'rotate-180' : 'rotate-0'
)}
size={16}
/>
)}
</span>
)}
) : (
<div className='flex items-center space-x-1.5 whitespace-nowrap'>
<SeasonIcon term={term} variant={variant} />
<div className={'pr-1 font-medium dark:text-gray-200'}>
No Instructor Assigned
</div>
</div>
)}
{instructors.length > 1 && (
<span
className='cursor-pointer font-semibold dark:text-gray-200'
onClick={() => handleToggle(i)}
>
+{instructors.length - 1}
{variant === 'large' && (
<ChevronDown
className={twMerge(
'ml-1 inline-block',
expandedState[i] ? 'rotate-180' : 'rotate-0'
)}
size={16}
/>
)}
</span>
)}
</div>
</div>
</div>
);
})}
);
})}
</div>
);
};
11 changes: 5 additions & 6 deletions client/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { Course } from '../model/Course';
import { Instructor } from '../model/Instructor';
import type { Schedule } from '../model/Schedule';

const COURSE_TERM_ORDER = ['Fall', 'Winter', 'Summer'];

export const groupCurrentCourseTermInstructors = (course: Course) => {
const currentTerms = getCurrentTerms();

const currentInstructors = course.instructors.filter((i) =>
currentTerms.includes(i.term)
);

const termGroups = _.groupBy(currentInstructors, (i: Instructor) => i.term);

for (const term of course.terms) {
Expand All @@ -21,17 +20,17 @@ export const groupCurrentCourseTermInstructors = (course: Course) => {

const entries = Object.entries(termGroups);

const indexOfTerm = (term: string) =>
COURSE_TERM_ORDER.indexOf(term.split(' ')[0]);

return entries.sort(([a], [b]) => indexOfTerm(a) - indexOfTerm(b));
return entries;
SamZhang02 marked this conversation as resolved.
Show resolved Hide resolved
};

export const getCurrentTerms = (): [string, string, string] => {
const now = new Date();
const month = now.getMonth() + 1;
const year = now.getFullYear();

if (month >= 5 && month < 8)
return [`Summer ${year}`, `Fall ${year}`, `Winter ${year + 1}`];

if (month >= 8)
return [`Fall ${year}`, `Winter ${year + 1}`, `Summer ${year + 1}`];

Expand Down
Loading