Skip to content

Commit 5375991

Browse files
committed
add Drawing year and number, sort drawings in reverse
1 parent 8c0d5dd commit 5375991

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

explorer/__fixtures__/drawings.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ export const drawingYears: number[] = [
1919
2015,
2020
]
2121

22-
export const yearDrawingSets: YearDrawingSets = drawingYears.reduce((accumulator, year) => {
23-
accumulator[year] = require(`./drawings/${year}.json`)
24-
return accumulator
25-
}, {} as Partial<YearDrawingSets>) as YearDrawingSets
22+
export const yearDrawingSets: YearDrawingSets = drawingYears
23+
.sort()
24+
.reverse()
25+
.reduce((accumulator, year) => {
26+
const rawYearSet = require(`./drawings/${year}.json`)
27+
const processedYearSet = rawYearSet.map((drawing: any) => {
28+
const [year, number] = drawing.id.split('.')
29+
return { ...drawing, year, number }
30+
})
31+
accumulator[year] = (processedYearSet as Drawing[]).sort((a, b) => b.number - a.number)
32+
return accumulator
33+
}, {} as Partial<YearDrawingSets>) as YearDrawingSets
2634

2735
export const drawings: Drawing[] = drawingYears.reduce((accumulator, year) => {
2836
return [...accumulator, ...yearDrawingSets[year]] as Drawing[]

explorer/components/DrawingPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const DrawingPage: React.FC<{ drawing: Drawing, year: number }> = ({ draw
3737
<YearLink>{year}</YearLink>
3838
</Link>
3939
<a href={`http://explodingdog.com/title/${drawing.slug}.html`}>
40-
<DrawingId>#{drawing.id.split('.').slice(-1)}</DrawingId>
40+
<DrawingId>#{drawing.number}</DrawingId>
4141
</a>
4242
<Date>{drawing.date}</Date>
4343
<Link href="/drawing/[id]" as={`/drawing/${getNextSlug(drawing.slug)}`}>

explorer/lib/drawings.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ export const getDrawingBySlug = (slug: string): Drawing | undefined => {
55
return drawings.find(o => o.slug === slug)
66
}
77

8-
export const getDrawingYear = (drawing: Drawing): number => {
9-
return parseInt(drawing.date.slice(0, 4), 10)
10-
}
11-
128
export const getPreviousSlug = (slug: string): string => {
139
const drawing = getDrawingBySlug(slug)
1410
if (!drawing) return 'something'

explorer/pages/drawing/[id].tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DrawingPage } from '../../components/DrawingPage'
22
import { GetStaticProps, GetStaticPaths } from 'next'
33
import { drawings } from '../../__fixtures__/drawings'
4-
import { getDrawingBySlug, getDrawingYear } from '../../lib/drawings'
4+
import { getDrawingBySlug } from '../../lib/drawings'
55
import { singleQueryParamValue } from '../../lib/next'
66

77
export default DrawingPage
@@ -14,6 +14,6 @@ export const getStaticPaths: GetStaticPaths = async () => {
1414
export const getStaticProps: GetStaticProps = async ({ params }) => {
1515
const slug = singleQueryParamValue(params?.id)
1616
const drawing = getDrawingBySlug(slug) || getDrawingBySlug('something')
17-
const year = getDrawingYear(drawing!)
17+
const year = drawing!.year
1818
return { props: { year, drawing } }
1919
}

explorer/types/drawing-models.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
export interface YearDrawingSets
1+
export interface Drawing
22
extends Readonly<{
3-
[year: number]: Drawing[]
3+
id: string // year + number, e.g. 2000.123
4+
year: number
5+
number: number // sequence index for that year
6+
date: string // e.g. 2000-01-01
7+
title: string // display text
8+
slug: string // for file and url
9+
image: string // path relative to image root, may have subdirectory
410
}> {}
511

6-
export interface Drawing
12+
export interface YearDrawingSets
713
extends Readonly<{
8-
id: string
9-
date: string
10-
title: string
11-
slug: string
12-
image: string
14+
[year: number]: Drawing[]
1315
}> {}

0 commit comments

Comments
 (0)