Add Multiple News Items on Homepage and Single News Item Page (Closes #281) #390
support-multiple-news-homepage
into main
1 year ago
@ -0,0 +1,8 @@ |
||||
.page { |
||||
padding-bottom: calc(30rem / 16); |
||||
} |
||||
|
||||
.page > h1 { |
||||
padding-bottom: calc(16rem / 16); |
||||
border-bottom: calc(1rem / 16) solid var(--primary-heading); |
||||
} |
@ -0,0 +1,108 @@ |
||||
import { ParsedUrlQuery } from "querystring"; |
||||
|
||||
import { GetStaticPaths, GetStaticProps } from "next"; |
||||
import { MDXRemote } from "next-mdx-remote"; |
||||
import React from "react"; |
||||
|
||||
import { NewsCard } from "@/components/NewsCard"; |
||||
import { |
||||
ShapesConfig, |
||||
defaultGetShapesConfig, |
||||
GetShapesConfig, |
||||
} from "@/components/ShapesBackground"; |
||||
import { Title } from "@/components/Title"; |
||||
import { |
||||
getNewsBySlug, |
||||
getNewsByTerm, |
||||
getNewsTermsByYear, |
||||
getNewsDateByTerm, |
||||
getNewsYears, |
||||
News, |
||||
} from "@/lib/news"; |
||||
import { Term } from "@/utils"; |
||||
|
||||
import styles from "./[date].module.css"; |
||||
|
||||
interface Props { |
||||
year: string; |
||||
term: Term; |
||||
news: News[]; |
||||
} |
||||
|
||||
export default function DateNews({ news }: Props) { |
||||
const date = new Date(news[0].metadata.date).toLocaleDateString("en-US", { |
||||
year: "numeric", |
||||
month: "long", |
||||
day: "numeric", |
||||
}); |
||||
return ( |
||||
<div className={styles.page}> |
||||
<Title>{["News", `${date}`]}</Title> |
||||
<h1>News: {date}</h1> |
||||
{news.map(({ content, metadata }, idx) => ( |
||||
a258wang
commented 1 year ago
Review
Is the |
||||
<NewsCard |
||||
key={idx} |
||||
{...metadata} |
||||
date={new Date(metadata.date)} |
||||
fit={true} |
||||
> |
||||
<MDXRemote {...content} /> |
||||
a258wang
commented 1 year ago
Review
It looks like we're passing
e26chiu
commented 1 year ago
Review
The "fit" style is also declared in
a258wang
commented 1 year ago
Review
Ah, I think what's happening is we're using the |
||||
</NewsCard> |
||||
))} |
||||
</div> |
||||
); |
||||
} |
||||
|
||||
DateNews.getShapesConfig = ((width, height) => { |
||||
return window.innerWidth <= 768 |
||||
? ({} as ShapesConfig) |
||||
: defaultGetShapesConfig(width, height); |
||||
}) as GetShapesConfig; |
||||
|
||||
export const getStaticProps: GetStaticProps<Props, Params> = async ( |
||||
context |
||||
) => { |
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
const { year, term, date } = context.params!; |
||||
const slugs = (await getNewsByTerm(year, term)).filter((slug) => |
||||
slug.includes(date) |
||||
); |
||||
|
||||
const news = await Promise.all( |
||||
slugs.map((slug) => getNewsBySlug(year, term, slug)) |
||||
); |
||||
// Reverse so that we are displaying the most recent news
|
||||
// of term first
|
||||
return { props: { year, term, news: news.reverse() } }; |
||||
a258wang
commented 1 year ago
Review
Is there a reason why we want to console log here? If it's just for debugging/testing purposes, we should probably get rid of it before merging. 🙂 |
||||
}; |
||||
|
||||
interface Params extends ParsedUrlQuery { |
||||
year: string; |
||||
term: Term; |
||||
date: string; |
||||
} |
||||
|
||||
export const getStaticPaths: GetStaticPaths<Params> = async () => { |
||||
const years = await getNewsYears(); |
||||
const terms = await Promise.all( |
||||
years.map(async (year) => { |
||||
const termsInYear = await getNewsTermsByYear(year); |
||||
return termsInYear.map((term) => ({ year, term })); |
||||
}) |
||||
); |
||||
const dates = await Promise.all( |
||||
terms.map(async (termInYear) => { |
||||
const datesInTerm: Params[] = []; |
||||
for (const { year, term } of termInYear) { |
||||
const dates = await getNewsDateByTerm(year, term); |
||||
dates.map((date) => datesInTerm.push({ year, term, date })); |
||||
} |
||||
return datesInTerm.flat(); |
||||
}) |
||||
); |
||||
|
||||
return { |
||||
paths: dates.flat().map((params) => ({ params })), |
||||
fallback: false, |
||||
}; |
||||
}; |
It might be helpful to leave a comment here explaining what the
fit
prop is used for.