Add Term type
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Jared He 2021-11-16 20:20:40 -06:00
parent e2ff165a39
commit e5fd9bfec9
9 changed files with 46 additions and 41 deletions

View File

@ -1,6 +1,6 @@
import React from "react";
import { capitalize } from "@/utils";
import { capitalize, Term } from "@/utils";
import { Link } from "./Link";
import {
@ -16,7 +16,7 @@ export interface Props {
type: "news" | "events";
items: {
year: string;
terms: string[];
terms: Term[];
}[];
}

View File

@ -7,10 +7,11 @@ import matter from "gray-matter";
import { MDXRemoteSerializeResult } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize";
import { Term, TERMS, isTerm } from "@/utils";
import type { Props } from "../pages/events/[year]/[term]/index";
const EVENTS_PATH = path.join("content", "events");
export const TERMS = ["winter", "spring", "fall"];
export async function getEventYears(): Promise<string[]> {
return (await fs.readdir(EVENTS_PATH, { withFileTypes: true }))
@ -19,12 +20,12 @@ export async function getEventYears(): Promise<string[]> {
.sort();
}
export async function getEventTermsByYear(year: string): Promise<string[]> {
export async function getEventTermsByYear(year: string): Promise<Term[]> {
return (
await fs.readdir(path.join(EVENTS_PATH, year), { withFileTypes: true })
)
.filter((dirent) => dirent.isDirectory() && TERMS.includes(dirent.name))
.map((dirent) => dirent.name)
.filter((dirent) => dirent.isDirectory() && isTerm(dirent.name))
.map((dirent) => dirent.name as Term)
.sort((a, b) => TERMS.indexOf(a) - TERMS.indexOf(b));
}
@ -58,7 +59,7 @@ export const DATE_FORMAT = "MMMM dd yyyy HH:mm";
export async function getEventBySlug(
year: string,
term: string,
term: Term,
slug: string
): Promise<Event> {
const file = await fs.readFile(
@ -84,7 +85,7 @@ export async function getEventBySlug(
export async function getEventsByTerm(
year: string,
term: string
term: Term
): Promise<string[]> {
try {
return (await fs.readdir(path.join(EVENTS_PATH, year, term)))
@ -149,7 +150,7 @@ export async function getEventsPageProps({
term,
}: {
year: string;
term: string;
term: Term;
}): Promise<Props> {
const eventNames = await getEventsByTerm(year, term);
@ -177,7 +178,7 @@ export async function getEventsPageProps({
const eventYears = await getEventYears();
const minYear = eventYears[0];
const pastTerms: { year: string; term: string }[] = [];
const pastTerms: { year: string; term: Term }[] = [];
let curPastYear = year;
let curPastTerm = term;
while (parseInt(curPastYear) >= parseInt(minYear) && pastTerms.length < 2) {
@ -191,7 +192,7 @@ export async function getEventsPageProps({
pastTerms.reverse();
const maxYear = eventYears[eventYears.length - 1];
const futureTerms: { year: string; term: string }[] = [];
const futureTerms: { year: string; term: Term }[] = [];
let curFutureYear = year;
let curFutureTerm = term;
while (
@ -217,7 +218,7 @@ export async function getEventsPageProps({
};
}
export function getCurrentTerm() {
export function getCurrentTerm(): { year: string; term: Term } {
const today = new Date().toLocaleDateString("en-CA", {
timeZone: "EST",
year: "numeric",
@ -241,17 +242,14 @@ export function getCurrentTerm() {
term = "fall";
}
if (term === "") {
if (!isTerm(term)) {
throw new Error("Error setting the current term");
}
return { year, term };
}
function getPastTerm(
year: string,
term: string
): { year: string; term: string } {
function getPastTerm(year: string, term: Term): { year: string; term: Term } {
const index = TERMS.indexOf(term);
if (index === -1) {
@ -269,10 +267,7 @@ function getPastTerm(
};
}
function getFutureTerm(
year: string,
term: string
): { year: string; term: string } {
function getFutureTerm(year: string, term: Term): { year: string; term: Term } {
const index = TERMS.indexOf(term);
if (index === -1) {

View File

@ -6,10 +6,11 @@ import matter from "gray-matter";
import { MDXRemoteSerializeResult } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize";
import { isTerm, Term, TERMS } from "@/utils";
import { DATE_FORMAT, getLocalDateFromEST } from "./events";
export const NEWS_PATH = path.join("content", "news");
const TERMS = ["winter", "spring", "fall"];
export interface Metadata {
author: string;
@ -28,14 +29,14 @@ export async function getNewsYears(): Promise<string[]> {
.sort();
}
export async function getNewsTermsByYear(year: string): Promise<string[]> {
export async function getNewsTermsByYear(year: string): Promise<Term[]> {
return (
await fs.readdir(path.join(NEWS_PATH, year), {
withFileTypes: true,
})
)
.filter((dirent) => dirent.isDirectory() && TERMS.includes(dirent.name))
.map((dirent) => dirent.name)
.filter((dirent) => dirent.isDirectory() && isTerm(dirent.name))
.map((dirent) => dirent.name as Term)
.sort((a, b) => TERMS.indexOf(a) - TERMS.indexOf(b));
}

View File

@ -18,7 +18,7 @@ import {
getEventsByTerm,
getEventBySlug,
} from "@/lib/events";
import { capitalize } from "@/utils";
import { capitalize, Term } from "@/utils";
export default function EventInfoPage({ year, term, event }: Props) {
return (
@ -43,13 +43,13 @@ EventInfoPage.getShapesConfig = ((width, height) => {
interface Props {
year: string;
term: string;
term: Term;
event: Event;
}
interface Params extends ParsedUrlQuery {
year: string;
term: string;
term: Term;
event: string;
}

View File

@ -14,21 +14,21 @@ import {
getEventYears,
getEventTermsByYear,
} from "@/lib/events";
import { capitalize } from "@/utils";
import { capitalize, Term } from "@/utils";
import styles from "./index.module.css";
export interface Props {
year: string;
term: string;
term: Term;
pastEvents: Event[];
futureEvents: Event[];
isCurrentTerm: boolean;
pastTerms: { year: string; term: string }[];
futureTerms: { year: string; term: string }[];
pastTerms: { year: string; term: Term }[];
futureTerms: { year: string; term: Term }[];
}
export default function Term(props: Props) {
export default function TermPage(props: Props) {
let headerTerms = [{ year: props.year, term: props.term }];
// p, Current, f
@ -120,7 +120,7 @@ export default function Term(props: Props) {
function HeaderLink(props: {
year: string;
term: string;
term: Term;
isCurrentTerm?: boolean;
}) {
return (
@ -134,7 +134,7 @@ function HeaderLink(props: {
interface Params extends ParsedUrlQuery {
year: string;
term: string;
term: Term;
}
export const getStaticProps: GetStaticProps<Props, Params> = async (

View File

@ -6,12 +6,13 @@ import React from "react";
import { Link } from "@/components/Link";
import { Title } from "@/components/Title";
import { getEventYears, getEventTermsByYear } from "@/lib/events";
import { Term } from "@/utils";
import styles from "./index.module.css";
interface Props {
year: string;
terms: string[];
terms: Term[];
}
export default function Year(props: Props) {

View File

@ -2,9 +2,9 @@ import { GetStaticProps } from "next";
import { getCurrentTerm, getEventsPageProps } from "@/lib/events";
import Term, { Props } from "./[year]/[term]";
import TermEvent, { Props } from "./[year]/[term]";
export default Term;
export default TermEvent;
export const getStaticProps: GetStaticProps<Props> = async () => {
return { props: await getEventsPageProps(getCurrentTerm()) };

View File

@ -18,13 +18,13 @@ import {
getNewsYears,
News,
} from "@/lib/news";
import { capitalize } from "@/utils";
import { capitalize, Term } from "@/utils";
import styles from "./[term].module.css";
interface Props {
year: string;
term: string;
term: Term;
news: News[];
}
@ -75,7 +75,7 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (
interface Params extends ParsedUrlQuery {
year: string;
term: string;
term: Term;
}
export const getStaticPaths: GetStaticPaths<Params> = async () => {

View File

@ -1,3 +1,11 @@
export const TERMS = ["winter", "spring", "fall"] as const;
export type Term = typeof TERMS[number];
// https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
export function isTerm(x: string): x is Term {
return x === "winter" || x === "spring" || x === "fall";
}
export function capitalize(str: string) {
return str.slice(0, 1).toUpperCase() + str.slice(1);
}