Add getNextTerm, getPreviousTerm
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Jared He 2021-08-23 22:31:21 -05:00
parent 2abc7e75c1
commit 8d59ae1692
2 changed files with 74 additions and 21 deletions

View File

@ -6,7 +6,7 @@ import { MDXRemoteSerializeResult } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize"; import { serialize } from "next-mdx-remote/serialize";
const EVENTS_PATH = path.join("content", "events"); const EVENTS_PATH = path.join("content", "events");
const TERMS = ["winter", "spring", "fall"]; export const TERMS = ["winter", "spring", "fall"];
export async function getEventYears(): Promise<string[]> { export async function getEventYears(): Promise<string[]> {
return (await fs.readdir(EVENTS_PATH, { withFileTypes: true })) return (await fs.readdir(EVENTS_PATH, { withFileTypes: true }))

View File

@ -12,6 +12,7 @@ import {
getEventsByTerm, getEventsByTerm,
getEventBySlug, getEventBySlug,
Event, Event,
TERMS,
} from "@/lib/events"; } from "@/lib/events";
import styles from "./index.module.css"; import styles from "./index.module.css";
@ -21,7 +22,7 @@ interface Props {
year: string; year: string;
pastEvents: Event[]; pastEvents: Event[];
futureEvents: Event[]; futureEvents: Event[];
currentTerm: boolean; isCurrentTerm: boolean;
} }
export default function Term(props: Props) { export default function Term(props: Props) {
@ -46,8 +47,8 @@ export default function Term(props: Props) {
</div> </div>
</> </>
)} )}
{hasPastEvents && props.currentTerm && <h2>Past Events</h2>} {hasPastEvents && props.isCurrentTerm && <h2>Past Events</h2>}
{hasPastEvents && !props.currentTerm && ( {hasPastEvents && !props.isCurrentTerm && (
<h2> <h2>
Events Archive: Events Archive:
<span className={styles.blue}> <span className={styles.blue}>
@ -82,6 +83,32 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { year, term } = context.params!; const { year, term } = context.params!;
return await getProps(year, term);
};
export const getStaticPaths: GetStaticPaths<Params> = async () => {
const years = await getEventYears();
const paths = (
await Promise.all(
years.map(async (year) => {
const terms = await getEventTermsByYear(year);
return terms.map((curTerm) => ({
params: { year: year, term: curTerm },
}));
})
)
).flat();
return {
paths: paths,
fallback: false,
};
};
export const getProps = async (
year: string,
term: string
): Promise<{ props: Props }> => {
const eventNames = await getEventsByTerm(year, term); const eventNames = await getEventsByTerm(year, term);
const events: Event[] = ( const events: Event[] = (
@ -103,34 +130,60 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (
const current = getCurrentTerm(); const current = getCurrentTerm();
// console.log(getPreviousTerm(term, year));
// console.log(getNextTerm(term, year));
return { return {
props: { props: {
term: term, term: term,
year: year, year: year,
pastEvents: pastEvents, pastEvents: pastEvents,
futureEvents: futureEvents, futureEvents: futureEvents,
currentTerm: term === current.term && year === current.year, isCurrentTerm: term === current.term && year === current.year,
}, },
}; };
}; };
export const getStaticPaths: GetStaticPaths<Params> = async () => { const getPreviousTerm = (
const years = await getEventYears(); year: string,
const paths = ( term: string
await Promise.all( ): { year: string; term: string } => {
years.map(async (year) => { const index = TERMS.indexOf(term);
const terms = await getEventTermsByYear(year); if (index === -1) {
return terms.map((curTerm) => ({ console.error("Not a valid term");
params: { year: year, term: curTerm }, return { year: "", term: "" };
})); } else if (index === 0) {
}) return {
) year: (parseInt(year) - 1).toString(),
).flat(); term: TERMS[TERMS.length - 1],
};
} else {
return {
year: year,
term: TERMS[index - 1],
};
}
};
return { const getNextTerm = (
paths: paths, year: string,
fallback: false, 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],
};
}
}; };
export function getCurrentTerm() { export function getCurrentTerm() {