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

[Bug] Navbar search persisting loading state #67

Merged
merged 10 commits into from
Sep 27, 2024
2 changes: 1 addition & 1 deletion app/(main)/professors/[id]/@reviews/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getKey =
(pageIndex: number, previousPageData: ProfessorsIDReviewsResponse) => {
if (previousPageData && previousPageData.page === previousPageData.pages)
return null;
return `/django/core/professors/${id}/reviews/?page=${pageIndex + 1}&${params}`;
return `/django/core/professors/${id}/reviews?page=${pageIndex + 1}&${params}`;
};

const Skeleton = () =>
Expand Down
2 changes: 1 addition & 1 deletion app/(main)/professors/[id]/@schedules/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getKey =
(pageIndex: number, previousPageData: ProfessorsIDSchedulesResponse) => {
if (previousPageData && previousPageData.page === previousPageData.pages)
return null;
return `/django/core/professors/${id}/schedules/?page=${pageIndex + 1}`;
return `/django/core/professors/${id}/schedules?page=${pageIndex + 1}`;
};

export default function Page({ params }: { params: { id: string } }) {
Expand Down
14 changes: 9 additions & 5 deletions components/molecules/client/nav-search-bar/component.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client';

import Link from 'next/link';
import { useSearchParams } from 'next/navigation';
import useSWR from 'swr';

Expand All @@ -12,6 +11,7 @@ import {
SchedulesSearchResponse,
} from '@/types';
import SWRConfigProvider from '@/wrappers/swr-config';
import Link from 'next/link';

type Error = { message: string };

Expand Down Expand Up @@ -75,6 +75,7 @@ const StatusMessage: React.FC<StatusMessageProps> = ({

const CourseSearchResults: React.FC = () => {
const searchParams = useSearchParams();
const currentOption = searchParams.get('navOption') ?? 'courses';
const currentQuery = searchParams.get('navQuery') ?? '';
const { data, error, isLoading } = useCoursesSearchResults(currentQuery);
return (
Expand All @@ -84,7 +85,7 @@ const CourseSearchResults: React.FC = () => {
? 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}`}
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">
Expand All @@ -103,6 +104,7 @@ const CourseSearchResults: React.FC = () => {

const ProfessorSearchResults: React.FC = () => {
const searchParams = useSearchParams();
const currentOption = searchParams.get('navOption') ?? 'courses';
const currentQuery = searchParams.get('navQuery') ?? '';
const { data, error, isLoading } = useProfessorsSearchResults(currentQuery);
return (
Expand All @@ -112,7 +114,7 @@ const ProfessorSearchResults: React.FC = () => {
? 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]}`}
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">
Expand All @@ -131,6 +133,7 @@ const ProfessorSearchResults: React.FC = () => {

const ScheduleSearchResults: React.FC = () => {
const searchParams = useSearchParams();
const currentOption = searchParams.get('navOption') ?? 'courses';
const currentQuery = searchParams.get('navQuery') ?? '';
const { data, error, isLoading } = useSchedulesSearchResults(currentQuery);
return (
Expand All @@ -140,7 +143,7 @@ const ScheduleSearchResults: React.FC = () => {
? data.items.map((schedule, i) => (
<li key={i} className="border-b-2 border-border last:border-b-0">
<Link
href={`/courses/${schedule.department}-${schedule.course_number}`}
href={`/courses/${schedule.department}-${schedule.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">
Expand Down Expand Up @@ -184,13 +187,14 @@ export const NavSearchBar: React.FC = () => {
param="navOption"
shouldResetPageOnChange={false}
className="rounded-l-none border-border bg-background"
value={currentOption}
>
<option value="courses">Courses</option>
<option value="professors">Professors</option>
<option value="schedules">Schedules</option>
</ParamSelect>
{currentQuery ? (
<Card className="absolute left-0 top-[50px] z-50 block w-[500px] max-w-[100dvw] shadow-paper peer-has-[:placeholder-shown]:hidden max-lg:hidden">
<Card className="absolute left-0 top-[50px] z-50 hidden w-[500px] max-w-[100dvw] shadow-paper hover:block peer-focus-within:block peer-has-[:placeholder-shown]:hidden max-lg:hidden">
<SWRConfigProvider>
{currentOption === 'courses' ? (
<CourseSearchResults />
Expand Down
8 changes: 7 additions & 1 deletion components/molecules/param-select/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ export const ParamSelect: React.FC<Props> = ({
replace(`${pathname}?${params.toString()}`);
}

React.useEffect(() => {
setPendingParam(currentParam);
}, [currentParam]);

return (
<Select
disabled={disabled || currentParam !== pendingParam}
disabled={
disabled || (currentParam !== pendingParam && currentParam !== null)
}
onChange={(e) => handleChange(e.target.value)}
defaultValue={currentParam ?? ''}
{...props}
Expand Down
6 changes: 5 additions & 1 deletion components/molecules/search-bar/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export const SearchBar: React.FC<Props> = ({
debouncedReplace(`${pathname}?${params.toString()}#${scrollTarget || ''}`);
}

React.useEffect(() => {
setPendingParam(currentParam);
}, [currentParam]);

return (
<span className={cn('relative', className)} {...props}>
<TextInput
Expand All @@ -53,7 +57,7 @@ export const SearchBar: React.FC<Props> = ({
defaultValue={currentParam ?? ''}
className="w-full pl-xxl"
/>
{currentParam !== pendingParam ? (
{currentParam !== pendingParam && currentParam !== null ? (
<Spinner className="absolute left-3 top-0 flex h-full items-center" />
) : (
<MagnifyingGlassIcon
Expand Down
1 change: 0 additions & 1 deletion utils/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class FetchError extends Error {

// From the SWR docs
const fetcher = async (url: string) => {
console.log('fetching', url);
const res = await fetch(url);
// artificial timeout to see loading state
// await new Promise((resolve) => setTimeout(resolve, 2000));
Expand Down
Loading