Skip to content

Commit 160c3d3

Browse files
committed
fix: parse story dates, add Story type
1 parent 5407a74 commit 160c3d3

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export type Story = {
2+
storyEnglish: string
3+
storyOriginal: string
4+
category: string
5+
name: string
6+
date: string
7+
country: string
8+
twitter: string
9+
region: string
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { formatDate, isValidDate } from "@/lib/utils/date"
2+
3+
import { DEFAULT_LOCALE } from "@/lib/constants"
4+
5+
import type { Story } from "./types"
6+
7+
const parseDate = (date: string, locale = DEFAULT_LOCALE): string => {
8+
// TODO: Remove this check when spreadsheet is fixed
9+
// Currently dates are in the formatted as "DD.MM." which is not parsable by Date.parse
10+
// If partially valid date, reformat it
11+
const partiallyValidDate = /^(\d{1,2})\.(\d{1})\.$/
12+
if (partiallyValidDate.test(date)) {
13+
const [, day, month] = date.match(partiallyValidDate) || []
14+
const newDate = `2025-${month.padStart(2, "0")}-${day.padStart(2, "0")}`
15+
return formatDate(newDate, locale)
16+
}
17+
18+
// If the date is already in a valid format, return it
19+
if (isValidDate(date)) return formatDate(date, locale)
20+
// If the date is not recognized, return original value
21+
return date
22+
}
23+
24+
export const parseStoryDates = (
25+
stories: Story[],
26+
locale = DEFAULT_LOCALE
27+
): Story[] =>
28+
stories.map(({ date, ...story }) => ({
29+
...story,
30+
date: parseDate(date, locale),
31+
}))

app/[locale]/10years/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import InnovationSwiper from "./_components/InnovationSwiper"
2929
import Stories from "./_components/Stories"
3030
import TenYearGlobe from "./_components/TenYearGlobe"
3131
import TenYearHero from "./_components/TenYearHero"
32+
import { parseStoryDates } from "./_components/utils"
3233

3334
import { fetch10YearEvents } from "@/lib/api/fetch10YearEvents"
3435
import { fetch10YearStories } from "@/lib/api/fetch10YearStories"
@@ -54,6 +55,8 @@ const Page = async ({ params }: { params: Promise<{ locale: Lang }> }) => {
5455

5556
const [fetched10YearEvents, fetched10YearStories] = await loadData()
5657

58+
const stories = parseStoryDates(fetched10YearStories, locale)
59+
5760
// Get i18n messages
5861
const allMessages = await getMessages({ locale })
5962
const requiredNamespaces = getRequiredNamespacesForPage("/10years")
@@ -319,7 +322,7 @@ const Page = async ({ params }: { params: Promise<{ locale: Lang }> }) => {
319322
</ButtonLink>
320323
</div>
321324
</div>
322-
<Stories stories={fetched10YearStories} />
325+
<Stories stories={stories} />
323326
</div>
324327

325328
<div className="w-full gap-8 px-8 py-8 pt-32">

0 commit comments

Comments
 (0)