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";
const EVENTS_PATH = path.join("content", "events");
const TERMS = ["winter", "spring", "fall"];
export const TERMS = ["winter", "spring", "fall"];
export async function getEventYears(): Promise<string[]> {
return (await fs.readdir(EVENTS_PATH, { withFileTypes: true }))

View File

@ -12,6 +12,7 @@ import {
getEventsByTerm,
getEventBySlug,
Event,
TERMS,
} from "@/lib/events";
import styles from "./index.module.css";
@ -21,7 +22,7 @@ interface Props {
year: string;
pastEvents: Event[];
futureEvents: Event[];
currentTerm: boolean;
isCurrentTerm: boolean;
}
export default function Term(props: Props) {
@ -46,8 +47,8 @@ export default function Term(props: Props) {
</div>
</>
)}
{hasPastEvents && props.currentTerm && <h2>Past Events</h2>}
{hasPastEvents && !props.currentTerm && (
{hasPastEvents && props.isCurrentTerm && <h2>Past Events</h2>}
{hasPastEvents && !props.isCurrentTerm && (
<h2>
Events Archive:
<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
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 events: Event[] = (
@ -103,34 +130,60 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (
const current = getCurrentTerm();
// console.log(getPreviousTerm(term, year));
// console.log(getNextTerm(term, year));
return {
props: {
term: term,
year: year,
pastEvents: pastEvents,
futureEvents: futureEvents,
currentTerm: term === current.term && year === current.year,
isCurrentTerm: term === current.term && year === current.year,
},
};
};
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();
const getPreviousTerm = (
year: string,
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 {
paths: paths,
fallback: false,
year: (parseInt(year) - 1).toString(),
term: TERMS[TERMS.length - 1],
};
} else {
return {
year: year,
term: TERMS[index - 1],
};
}
};
const getNextTerm = (
year: string,
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() {