From e1b41e2a37f0f69a8d3f17313b5015ada2988f28 Mon Sep 17 00:00:00 2001 From: Aditya Thakral Date: Fri, 27 Aug 2021 16:04:03 -0400 Subject: [PATCH] PR feedback --- lib/events.ts | 86 ++++++++++----------- pages/events/[year]/[term]/index.tsx | 110 ++++++++++++--------------- 2 files changed, 87 insertions(+), 109 deletions(-) diff --git a/lib/events.ts b/lib/events.ts index ac89237b..a909434d 100644 --- a/lib/events.ts +++ b/lib/events.ts @@ -60,15 +60,13 @@ export async function getEventsByTerm( year: string, term: string ): Promise { - let retval: string[] = []; try { - retval = (await fs.readdir(path.join(EVENTS_PATH, year, term))) + return (await fs.readdir(path.join(EVENTS_PATH, year, term))) .filter((name) => name.endsWith(".md")) .map((name) => name.slice(0, -".md".length)); } catch { return []; } - return retval; } export async function getUpcomingEvents(): Promise { @@ -106,10 +104,7 @@ export async function getUpcomingEvents(): Promise { }); } -export async function getProps( - year: string, - term: string -): Promise<{ props: Props }> { +export async function getProps(year: string, term: string): Promise { const eventNames = await getEventsByTerm(year, term); const events: Event[] = ( @@ -145,6 +140,7 @@ export async function getProps( pastTerms.push(pastTerm); } } + pastTerms.reverse(); const maxYear = eventYears[eventYears.length - 1]; const futureTerms: { year: string; term: string }[] = []; @@ -163,15 +159,13 @@ export async function getProps( } return { - props: { - year: year, - term: term, - pastEvents: pastEvents, - futureEvents: futureEvents, - isCurrentTerm: term === current.term && year === current.year, - pastTerms: pastTerms, - futureTerms: futureTerms, - }, + year: year, + term: term, + pastEvents: pastEvents, + futureEvents: futureEvents, + isCurrentTerm: term === current.term && year === current.year, + pastTerms: pastTerms, + futureTerms: futureTerms, }; } @@ -181,18 +175,18 @@ export function getCurrentTerm(): { year: string; term: string } { const year = date.getUTCFullYear().toString(); if ( - new Date(`${year}-01-01`).getTime() <= date.getTime() && - date.getTime() <= new Date(`${year}-04-30`).getTime() + new Date(`${year}-01-01 EST`).getTime() <= date.getTime() && + date.getTime() <= new Date(`${year}-04-30 EST`).getTime() ) { term = "winter"; } else if ( - new Date(`${year}-05-01`).getTime() <= date.getTime() && - date.getTime() <= new Date(`${year}-08-31`).getTime() + new Date(`${year}-05-01 EST`).getTime() <= date.getTime() && + date.getTime() <= new Date(`${year}-08-31 EST`).getTime() ) { term = "spring"; } else if ( - new Date(`${year}-09-01`).getTime() <= date.getTime() && - date.getTime() <= new Date(`${year}-12-31`).getTime() + new Date(`${year}-09-01 EST`).getTime() <= date.getTime() && + date.getTime() <= new Date(`${year}-12-31 EST`).getTime() ) { term = "fall"; } @@ -205,20 +199,20 @@ function getPastTerm( term: string ): { year: string; term: string } { const index = TERMS.indexOf(term); + if (index === -1) { - console.error("Not a valid term"); - return { year: "", term: "" }; - } else if (index === 0) { - return { - year: (parseInt(year) - 1).toString(), - term: TERMS[TERMS.length - 1], - }; - } else { - return { - year: year, - term: TERMS[index - 1], - }; + throw new Error("Not a valid term"); } + + return index === 0 + ? { + year: (parseInt(year) - 1).toString(), + term: TERMS[TERMS.length - 1], + } + : { + year: year, + term: TERMS[index - 1], + }; } function getFutureTerm( @@ -226,18 +220,18 @@ function getFutureTerm( term: string ): { year: string; term: string } { const index = TERMS.indexOf(term); + if (index === -1) { - console.error("Not a valid term"); - return { year: "", term: "" }; - } else if (index === TERMS.length - 1) { - return { - year: (parseInt(year) + 1).toString(), - term: TERMS[0], - }; - } else { - return { - year: year, - term: TERMS[index + 1], - }; + throw new Error("Not a valid term"); } + + return index === TERMS.length - 1 + ? { + year: (parseInt(year) + 1).toString(), + term: TERMS[0], + } + : { + year: year, + term: TERMS[index + 1], + }; } diff --git a/pages/events/[year]/[term]/index.tsx b/pages/events/[year]/[term]/index.tsx index 065cff4a..34a8c8e4 100644 --- a/pages/events/[year]/[term]/index.tsx +++ b/pages/events/[year]/[term]/index.tsx @@ -27,72 +27,42 @@ export interface Props { } export default function Term(props: Props) { - const hasPastTerms = props.pastTerms.length !== 0; - const hasFutureTerms = props.futureTerms.length !== 0; + let headerTerms = [{ year: props.year, term: props.term }]; + + // p, Current, f + if (props.pastTerms.length > 0 && props.futureTerms.length > 0) { + headerTerms = [ + ...props.pastTerms.slice(-1), + ...headerTerms, + ...props.futureTerms.slice(0, 1), + ]; + } + // p, p, Current + else if (props.pastTerms.length > 0) { + headerTerms = [...props.pastTerms.slice(-2), ...headerTerms]; + } + // Current, f, f + else { + headerTerms = [...headerTerms, ...props.futureTerms.slice(0, 2)]; + } + + headerTerms.reverse(); + const hasPastEvents = props.pastEvents.length !== 0; const hasFutureEvents = props.futureEvents.length !== 0; + return (
- {!hasPastTerms && hasFutureTerms && ( -
- - - {`${capitalize(props.term)} ${props.year}`} - - - {`${capitalize(props.futureTerms[0].term)} ${ - props.futureTerms[0].year - }`} - {`${capitalize(props.futureTerms[1].term)} ${ - props.futureTerms[1].year - }`} - Archives -
- )} - {hasPastTerms && hasFutureTerms && ( -
- {`${capitalize(props.pastTerms[0].term)} ${ - props.pastTerms[0].year - }`} - - - {`${capitalize(props.term)} ${props.year}`} - - - {`${capitalize(props.futureTerms[0].term)} ${ - props.futureTerms[0].year - }`} - Archives -
- )} - {hasPastTerms && !hasFutureTerms && ( -
- {`${capitalize(props.pastTerms[1].term)} ${ - props.pastTerms[1].year - } `} - {`${capitalize(props.pastTerms[0].term)} ${ - props.pastTerms[0].year - }`} - - - {`${capitalize(props.term)} ${props.year}`} - - - Archives -
- )} +
+ {headerTerms.map((link) => ( + + ))} + Archives +
{hasFutureEvents && ( <>

Upcoming Events

@@ -132,6 +102,20 @@ export default function Term(props: Props) { ); } +function HeaderLink(props: { + year: string; + term: string; + isCurrentTerm?: boolean; +}) { + return ( + + + {`${capitalize(props.term)} ${props.year}`} + + + ); +} + interface Params extends ParsedUrlQuery { year: string; term: string; @@ -143,7 +127,7 @@ export const getStaticProps: GetStaticProps = async ( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const { year, term } = context.params!; - return await getProps(year, term); + return { props: await getProps(year, term) }; }; export const getStaticPaths: GetStaticPaths = async () => {