Skip to content

Commit

Permalink
updated loaders for new term data
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartlage3 committed Oct 23, 2023
1 parent af7da8d commit 81d3f8f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/components/App/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './navigation';
import { classes } from '../../utils/misc';
import { AccountContextValue } from '../../contexts/account';
import { Term } from '../../types';

/**
* Renders the actual content at the root of the app
Expand Down Expand Up @@ -85,7 +86,7 @@ export type AppSkeletonProps = {
children: React.ReactNode;
accountState?: AccountContextValue;
termsState?: {
terms: string[];
terms: Term[];
currentTerm: string;
onChangeTerm: (next: string) => void;
};
Expand Down
6 changes: 3 additions & 3 deletions src/components/AppDataLoader/stages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Immutable, Draft, castDraft } from 'immer';
import { Oscar } from '../../data/beans';
import useDownloadOscarData from '../../data/hooks/useDownloadOscarData';
import useDownloadTerms from '../../data/hooks/useDownloadTerms';
import { NonEmptyArray } from '../../types';
import { NonEmptyArray, Term } from '../../types';
import LoadingDisplay from '../LoadingDisplay';
import { SkeletonContent, AppSkeleton, AppSkeletonProps } from '../App/content';
import {
Expand Down Expand Up @@ -78,7 +78,7 @@ export function StageLoadUIState({

export type StageEnsureValidTermProps = {
skeletonProps?: StageSkeletonProps;
terms: NonEmptyArray<string>;
terms: NonEmptyArray<Term>;
currentTermRaw: string;
setTerm: (next: string) => void;
children: (props: { currentTerm: string }) => React.ReactNode;
Expand Down Expand Up @@ -343,7 +343,7 @@ export function StageCreateScheduleDataProducer({

export type StageLoadTermsProps = {
skeletonProps?: StageSkeletonProps;
children: (props: { terms: NonEmptyArray<string> }) => React.ReactNode;
children: (props: { terms: NonEmptyArray<Term> }) => React.ReactNode;
};

/**
Expand Down
7 changes: 4 additions & 3 deletions src/components/HeaderDisplay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Modal from '../Modal';
import { AccountContextValue } from '../../contexts/account';

import './stylesheet.scss';
import { Term } from '../../types';

type VersionState =
| { type: 'loading' }
Expand Down Expand Up @@ -49,7 +50,7 @@ export type HeaderDisplayProps = {
| { type: 'loading' }
| {
type: 'loaded';
terms: readonly string[];
terms: Term[];
currentTerm: string;
onChangeTerm: (next: string) => void;
};
Expand Down Expand Up @@ -108,8 +109,8 @@ export default function HeaderDisplay({
onChange={termsState.onChangeTerm}
current={termsState.currentTerm}
options={termsState.terms.map((currentTerm) => ({
id: currentTerm,
label: getSemesterName(currentTerm),
id: currentTerm.term,
label: getSemesterName(currentTerm.term),
}))}
className="semester"
/>
Expand Down
12 changes: 5 additions & 7 deletions src/data/hooks/useDownloadTerms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState, useEffect } from 'react';
import axios from 'axios';

import { softError, ErrorWithFields } from '../../log';
import { LoadingState, NonEmptyArray } from '../../types';
import { LoadingState, NonEmptyArray, Term } from '../../types';
import { exponentialBackoff, isAxiosNetworkError } from '../../utils/misc';
import Cancellable from '../../utils/cancellable';

Expand All @@ -14,10 +14,8 @@ const CRAWLER_INDEX_URL =
* Repeatedly attempts to load in the case of errors,
* and cancels itself once the parent context is unmounted.
*/
export default function useDownloadTerms(): LoadingState<
NonEmptyArray<string>
> {
const [state, setState] = useState<LoadingState<NonEmptyArray<string>>>({
export default function useDownloadTerms(): LoadingState<NonEmptyArray<Term>> {
const [state, setState] = useState<LoadingState<NonEmptyArray<Term>>>({
type: 'loading',
});

Expand All @@ -28,7 +26,7 @@ export default function useDownloadTerms(): LoadingState<
let attemptNumber = 1;
while (!loadOperation.isCancelled) {
try {
const promise = axios.get<{ terms: string[] }>(CRAWLER_INDEX_URL);
const promise = axios.get<{ terms: Term[] }>(CRAWLER_INDEX_URL);
const result = await loadOperation.perform(promise);
if (result.cancelled) {
return;
Expand All @@ -45,7 +43,7 @@ export default function useDownloadTerms(): LoadingState<

setState({
type: 'loaded',
result: newTerms as NonEmptyArray<string>,
result: newTerms as NonEmptyArray<Term>,
});

return;
Expand Down
14 changes: 9 additions & 5 deletions src/data/hooks/useEnsureValidTerm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from 'react';

import { NonEmptyArray, LoadingState } from '../../types';
import { NonEmptyArray, LoadingState, Term } from '../../types';

type HookResult = {
currentTerm: string;
Expand All @@ -16,13 +16,13 @@ export default function useEnsureValidTerm({
setTerm,
currentTermRaw,
}: {
terms: NonEmptyArray<string>;
terms: NonEmptyArray<Term>;
setTerm: (next: string) => void;
currentTermRaw: string;
}): LoadingState<HookResult> {
// Set the term to be the first one if it is unset or no longer valid.
useEffect(() => {
const mostRecentTerm = terms[0];
const mostRecentTerm = terms[0].term;
const correctedTerm = !isValidTerm(currentTermRaw, terms)
? mostRecentTerm
: currentTermRaw;
Expand All @@ -48,6 +48,10 @@ export default function useEnsureValidTerm({
* Determines if the given term is considered "valid";
* helps to recover from invalid cookie values if possible.
*/
export function isValidTerm(term: string, terms: string[]): boolean {
return term !== '' && term !== 'undefined' && terms.includes(term);
export function isValidTerm(currTerm: string, terms: Term[]): boolean {
return (
currTerm !== '' &&
currTerm !== 'undefined' &&
terms.map((term) => term.term).includes(currTerm)
);
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export interface Event {
days: string[];
}

export interface Term {
term: string;
finalized: boolean;
}

// Note: if this type ever changes,
// the course gpa cache needs to be invalidated
// (by changing the local storage key).
Expand Down

0 comments on commit 81d3f8f

Please sign in to comment.