-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
315 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
components/molecules/client/compare-search-bar/component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
'use client'; | ||
|
||
import { useSearchParams } from 'next/navigation'; | ||
import useSWR from 'swr'; | ||
|
||
import { Card } from '@/components/atoms'; | ||
import { ParamSelect, SearchBar } from '@/components/molecules'; | ||
import { CoursesSearchResponse, ProfessorsSearchResponse } from '@/types'; | ||
import SWRConfigProvider from '@/wrappers/swr-config'; | ||
import Link from 'next/link'; | ||
|
||
type Error = { message: string }; | ||
|
||
const useCoursesSearchResults = (currentQuery: string) => { | ||
const apiQueryParams = new URLSearchParams(); | ||
apiQueryParams.set('query', currentQuery); | ||
apiQueryParams.set('limit', '3'); | ||
const { data, error, isLoading } = useSWR<CoursesSearchResponse, Error>( | ||
`/django/core/courses/search?${apiQueryParams.toString()}`, | ||
); | ||
return { data, error, isLoading }; | ||
}; | ||
|
||
const useProfessorsSearchResults = (currentQuery: string) => { | ||
const apiQueryParams = new URLSearchParams(); | ||
apiQueryParams.set('query', currentQuery); | ||
apiQueryParams.set('limit', '3'); | ||
|
||
const { data, error, isLoading } = useSWR<ProfessorsSearchResponse, Error>( | ||
`/django/core/professors/search?${apiQueryParams.toString()}`, | ||
); | ||
return { data, error, isLoading }; | ||
}; | ||
|
||
interface StatusMessageProps { | ||
isLoading: boolean; | ||
error: Error | undefined; | ||
data: ({ total_results: number } & unknown) | undefined; | ||
} | ||
|
||
const StatusMessage: React.FC<StatusMessageProps> = ({ | ||
isLoading, | ||
error, | ||
data, | ||
}) => { | ||
if (isLoading) { | ||
return <li className="mx-auto my-8 w-fit">Loading...</li>; | ||
} | ||
if (error) { | ||
return ( | ||
<li className="mx-auto my-8 w-fit text-important"> | ||
Error: {error.message} | ||
</li> | ||
); | ||
} | ||
if (!data || data.total_results === 0) { | ||
return <li className="mx-auto my-8 w-fit">No results found</li>; | ||
} | ||
return null; | ||
}; | ||
|
||
const CourseSearchResults: React.FC = () => { | ||
const searchParams = useSearchParams(); | ||
const currentOption = searchParams.get('compareOption') ?? 'professors'; | ||
const currentQuery = searchParams.get('compareQuery') ?? ''; | ||
const { data, error, isLoading } = useCoursesSearchResults(currentQuery); | ||
return ( | ||
<menu> | ||
<StatusMessage isLoading={isLoading} error={error} data={data} /> | ||
{data && data.total_results > 0 | ||
? data.items.map((course, i) => ( | ||
<li key={i} className="border-b-2 border-border last:border-b-0"> | ||
<Link | ||
href={`/courses/${course.department}-${course.course_number}?navOption=${currentOption}`} | ||
className="flex flex-col px-lg py-md animation hover:bg-[rgb(var(--color-primary)/0.15)] focus:bg-[rgb(var(--color-primary)/0.15)]" | ||
> | ||
<span className="overflow-ellipsis text-small-lg text-neutral"> | ||
{course.department} {course.course_number} | ||
</span> | ||
<span className="overflow-ellipsis text-p font-bold"> | ||
{course.name} | ||
</span> | ||
</Link> | ||
</li> | ||
)) | ||
: null} | ||
</menu> | ||
); | ||
}; | ||
|
||
const ProfessorSearchResults: React.FC = () => { | ||
const searchParams = useSearchParams(); | ||
const currentOption = searchParams.get('compareOption') ?? 'professors'; | ||
const currentQuery = searchParams.get('compareQuery') ?? ''; | ||
const { data, error, isLoading } = useProfessorsSearchResults(currentQuery); | ||
return ( | ||
<menu> | ||
<StatusMessage isLoading={isLoading} error={error} data={data} /> | ||
{data && data.total_results > 0 | ||
? data.items.map((professor, i) => ( | ||
<li key={i} className="border-b-2 border-border last:border-b-0"> | ||
<Link | ||
href={`/professors/${professor.email.split('@')[0]}?navOption=${currentOption}`} | ||
className="flex flex-col px-lg py-md animation hover:bg-[rgb(var(--color-primary)/0.15)] focus:bg-[rgb(var(--color-primary)/0.15)]" | ||
> | ||
<span className="overflow-ellipsis text-small-lg text-neutral"> | ||
{professor.email} | ||
</span> | ||
<span className="overflow-ellipsis text-p font-bold"> | ||
{professor.name} | ||
</span> | ||
</Link> | ||
</li> | ||
)) | ||
: null} | ||
</menu> | ||
); | ||
}; | ||
|
||
export const CompareSearchBar: React.FC = () => { | ||
const searchParams = useSearchParams(); | ||
const currentOption = searchParams.get('compareOption') ?? 'professors'; | ||
const currentQuery = searchParams.get('compareQuery') ?? ''; | ||
return ( | ||
<div className="relative flex whitespace-nowrap"> | ||
<SearchBar | ||
param="compareQuery" | ||
shouldResetPageOnChange={false} | ||
className="peer flex-1 [&>input]:!rounded-r-none" | ||
/> | ||
<ParamSelect | ||
param="compareOption" | ||
shouldResetPageOnChange={false} | ||
className="rounded-l-none border-border bg-background" | ||
value={currentOption} | ||
> | ||
<option value="professors">Professors</option> | ||
<option value="courses">Courses</option> | ||
</ParamSelect> | ||
{currentQuery ? ( | ||
<Card className="absolute left-0 top-[50px] z-50 hidden w-[1000px] max-w-[80dvw] shadow-paper hover:block peer-focus-within:block peer-has-[:placeholder-shown]:hidden"> | ||
<SWRConfigProvider> | ||
{currentOption === 'professors' ? ( | ||
<ProfessorSearchResults /> | ||
) : currentOption === 'courses' ? ( | ||
<CourseSearchResults /> | ||
) : null} | ||
</SWRConfigProvider> | ||
</Card> | ||
) : null} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { CompareSearchBar as default } from './component'; | ||
export * from './component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Card, LinkBtn, Stars } from '@/components/atoms'; | ||
import { cn } from '@/utils/cn'; | ||
import getEvaluation from '@/utils/get-evaluation'; | ||
import { | ||
ArrowPathIcon, | ||
ClipboardDocumentListIcon, | ||
} from '@heroicons/react/16/solid'; | ||
|
||
interface Props { | ||
link: string; | ||
id: string; | ||
review?: number; | ||
totalReviews: number; | ||
avgGrade?: string | null; | ||
takeAgainPercent?: number; | ||
} | ||
|
||
export const CompareItem: React.FC<Props> = (props) => ( | ||
<LinkBtn | ||
variant="tertiary" | ||
className="w-fit rounded-md p-0 text-inherit" | ||
href={props.link} | ||
> | ||
<Card className="flex w-fit flex-col gap-sm rounded-md p-md"> | ||
<div> | ||
<p className="font-bold">{props.id}</p> | ||
<div className="flex items-center gap-sm"> | ||
{props.review ? ( | ||
<p className="text-small-lg">{props.review}</p> | ||
) : ( | ||
<p className="text-small-lg text-neutral">-</p> | ||
)} | ||
|
||
<div className="w-[100px]"> | ||
<Stars rating={props.review ?? 0} /> | ||
</div> | ||
<p className="text-small-sm text-neutral"> | ||
{props.totalReviews} Reviews | ||
</p> | ||
</div> | ||
</div> | ||
<div className="flex flex-col gap-xs"> | ||
<div className="flex items-center gap-xs text-neutral"> | ||
<ClipboardDocumentListIcon width={16} height={16} /> | ||
<p className="text-small-sm"> | ||
Average Grade:{' '} | ||
{props.avgGrade ? ( | ||
<span | ||
className={cn({ | ||
'text-good': | ||
getEvaluation(props.avgGrade, 'grade') === 'good', | ||
'text-ok': getEvaluation(props.avgGrade, 'grade') === 'ok', | ||
'text-bad': getEvaluation(props.avgGrade, 'grade') === 'bad', | ||
})} | ||
> | ||
{props.avgGrade} | ||
</span> | ||
) : ( | ||
<span className="text-neutral">-</span> | ||
)} | ||
</p> | ||
</div> | ||
<div className="flex items-center gap-xs text-neutral"> | ||
<ArrowPathIcon width={16} height={16} /> | ||
<p className="text-small-sm"> | ||
Would Take Again:{' '} | ||
{props.takeAgainPercent ? ( | ||
<span | ||
className={cn({ | ||
'text-good': | ||
getEvaluation(props.takeAgainPercent, 'percentage') === | ||
'good', | ||
'text-ok': | ||
getEvaluation(props.takeAgainPercent, 'percentage') === | ||
'ok', | ||
'text-bad': | ||
getEvaluation(props.takeAgainPercent, 'percentage') === | ||
'bad', | ||
})} | ||
> | ||
{props.takeAgainPercent}% | ||
</span> | ||
) : ( | ||
<span className="text-neutral">-</span> | ||
)} | ||
</p> | ||
</div> | ||
</div> | ||
</Card> | ||
</LinkBtn> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {CompareItem as default} from './component'; | ||
export * from './component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters