diff --git a/frontend/src/Routes.tsx b/frontend/src/Routes.tsx index 4778161f2..b1ae2bdff 100644 --- a/frontend/src/Routes.tsx +++ b/frontend/src/Routes.tsx @@ -58,7 +58,7 @@ const router = createBrowserRouter([ ErrorBoundary: Error, children: [ { path: '/catalog/:semester?/:abbreviation?/:courseNumber?', Component: Catalog }, - { path: '/scheduler/new', lazy: LocalScheduler } + { path: '/scheduler/new/', lazy: LocalScheduler } ] } ]); diff --git a/frontend/src/app/Scheduler/LocalSchedulerPage.tsx b/frontend/src/app/Scheduler/LocalSchedulerPage.tsx index 1bf7bb157..e6bdaceb9 100644 --- a/frontend/src/app/Scheduler/LocalSchedulerPage.tsx +++ b/frontend/src/app/Scheduler/LocalSchedulerPage.tsx @@ -1,16 +1,15 @@ -import { Button, OverlayTrigger, Tooltip } from 'react-bootstrap'; - import BTLoader from 'components/Common/BTLoader'; -import { DEFAULT_SCHEDULE, Schedule, SCHEDULER_LOCALSTORAGE_KEY } from 'utils/scheduler/scheduler'; -import { useUser } from 'graphql/hooks/user'; import { useCreateSchedule } from 'graphql/hooks/schedule'; -import { useLocalStorageState } from 'utils/hooks'; -import ScheduleEditor from '../../components/Scheduler/ScheduleEditor'; -import { useNavigate } from 'react-router-dom'; import { useSemester } from 'graphql/hooks/semester'; -import Callout from '../../components/Scheduler/Callout'; -import { ReduxState } from 'redux/store'; +import { useUser } from 'graphql/hooks/user'; +import { Button, OverlayTrigger, Tooltip } from 'react-bootstrap'; import { useSelector } from 'react-redux'; +import { useNavigate, useSearchParams } from 'react-router-dom'; +import { ReduxState } from 'redux/store'; +import { useLocalStorageState } from 'utils/hooks'; +import { DEFAULT_SCHEDULE, SCHEDULER_LOCALSTORAGE_KEY, Schedule } from 'utils/scheduler/scheduler'; +import Callout from '../../components/Scheduler/Callout'; +import ScheduleEditor from '../../components/Scheduler/ScheduleEditor'; export function Component() { const [schedule, setSchedule] = useLocalStorageState( @@ -21,7 +20,31 @@ export function Component() { const { isLoggedIn, loading: loadingUser } = useUser(); const navigate = useNavigate(); - const { semester, error: semesterError } = useSemester(); + const [searchParams, setSearchParams] = useSearchParams(); + + const stringToSemester = (string: string | null) => { + if (!string) { + return undefined + } + const [semester, year] = string.trim().toLowerCase().split(' '); + return { + semester, + year + }; + }; + + const { semester, error: semesterError } = useSemester( + searchParams.has('semester') && searchParams.get('semester') + ? stringToSemester(searchParams.get('semester')) + : undefined + ); + + // useEffect(() => { + // const hasSemester = searchParams.has('semester'); + // if (!hasSemester) return; + // searchParams.delete('semester'); + // setSearchParams(searchParams); + // }, [searchParams, setSearchParams]); const [createScheduleMutation, { loading: isSaving, error: creationError }] = useCreateSchedule({ onCompleted: (data) => { @@ -60,9 +83,7 @@ export function Component() { return ; } - const createSchedule = async () => - // @ts-ignore - await createScheduleMutation(schedule, semester!); + const createSchedule = async () => await createScheduleMutation(schedule, semester!); const saveButton = ( )} - diff --git a/frontend/src/graphql/hooks/semester.ts b/frontend/src/graphql/hooks/semester.ts index 51aaede62..9e33009c9 100644 --- a/frontend/src/graphql/hooks/semester.ts +++ b/frontend/src/graphql/hooks/semester.ts @@ -1,8 +1,9 @@ -import { useGetSemestersQuery } from 'graphql'; import { ApolloError } from '@apollo/client'; +import { useGetSemestersQuery } from 'graphql'; import { getNodes } from 'utils/graphql'; import { getLatestSemester, + playlistToSemester, Semester, semesterToString, SemesterWithPlaylist @@ -21,9 +22,6 @@ export const useSemester = ( error: ApolloError | undefined; } => { const { data, loading, error } = useGetSemestersQuery({ - variables: { - name: semester && semesterToString(semester) - }, skip: !!semester?.playlistId }); @@ -32,13 +30,15 @@ export const useSemester = ( if (semester?.playlistId) { latestSemester = semester as SemesterWithPlaylist; } else if (data?.allPlaylists && data.allPlaylists.edges.length >= 1) { - latestSemester = getLatestSemester(getNodes(data.allPlaylists)); - } - - // Overriding the latest semester because the dev db has data from 2020 - if (process.env.NODE_ENV === 'development' && latestSemester) { - latestSemester.semester = 'fall'; - latestSemester.year = '2020'; + if (semester) { + latestSemester = getNodes(data.allPlaylists) + .map((node) => playlistToSemester(node)) + .filter((s) => { + return semesterToString(s) === semesterToString(semester); + })[0]; + } else { + latestSemester = getLatestSemester(getNodes(data.allPlaylists)); + } } return { semester: latestSemester, loading, error }; diff --git a/frontend/src/utils/playlists/semesters.ts b/frontend/src/utils/playlists/semesters.ts index 4efff6deb..c2031ad81 100644 --- a/frontend/src/utils/playlists/semesters.ts +++ b/frontend/src/utils/playlists/semesters.ts @@ -35,9 +35,9 @@ export type SemesterWithPlaylist = Semester & { playlistId: string }; export function getLatestSemester(playlists: FilterablePlaylist[]): SemesterWithPlaylist | null { const semesterPlaylists = playlists .filter( - (p) => - p.category === 'semester' && - (process.env.NODE_ENV !== 'development' || p.name === 'Fall 2020') + (p) => p.category === 'semester' + // && + // (process.env.NODE_ENV !== 'development' || p.name === 'Fall 2020') ) .sort((a, b) => playlistToTimeComparable(b) - playlistToTimeComparable(a)); @@ -52,15 +52,15 @@ export function getLatestSemester(playlists: FilterablePlaylist[]): SemesterWith /** * Converts playlist to semester */ -function playlistToSemester(playlist: FilterablePlaylist): SemesterWithPlaylist { +export function playlistToSemester(playlist: FilterablePlaylist): SemesterWithPlaylist { return stringToSemester(playlist.name, playlist.id); } /** * Convert a string to a semester. */ -function stringToSemester(string: string, playlistId: string): SemesterWithPlaylist; -function stringToSemester(string: string, playlistId?: string): Semester { +export function stringToSemester(string: string, playlistId: string): SemesterWithPlaylist; +export function stringToSemester(string: string, playlistId?: string): Semester { const [semester, year] = string.trim().toLowerCase().split(' '); return { semester,