Skip to content

Commit 1264674

Browse files
committed
group by day in YearPage
1 parent f485a2f commit 1264674

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

explorer/__fixtures__/drawings.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Drawing, YearDrawingSets, DayDrawingSets } from '../types'
1+
import { Drawing, YearDrawingSets, DayDrawingSets, DayDrawingSetsByYear } from '../types'
22

33
export const drawingYears: number[] = [
44
2000,
@@ -24,22 +24,29 @@ export const yearDrawingSets: YearDrawingSets = drawingYears
2424
.reverse()
2525
.reduce((accumulator, year) => {
2626
const rawYearSet = require(`./drawings/${year}.json`)
27-
const processedYearSet = rawYearSet.map((drawing: any) => {
27+
const processedYearSet: Drawing[] = rawYearSet.map((drawing: any) => {
2828
const [year, number] = drawing.id.split('.')
2929
return { ...drawing, year, number }
3030
})
31-
accumulator[year] = (processedYearSet as Drawing[]).sort((a, b) => b.number - a.number)
31+
accumulator[year] = processedYearSet.sort((a, b) => b.number - a.number)
3232
return accumulator
33-
}, {} as Partial<YearDrawingSets>) as YearDrawingSets
33+
}, {} as any) as YearDrawingSets
3434

3535
export const drawings: Drawing[] = drawingYears.reduce((accumulator, year) => {
3636
return [...accumulator, ...yearDrawingSets[year]] as Drawing[]
3737
}, [] as Drawing[])
3838

3939
export const dayDrawingSets: DayDrawingSets = drawings.reduce((accumulator, drawing) => {
4040
if (!accumulator[drawing.date]) accumulator[drawing.date] = []
41-
accumulator[drawing.date]!.push(drawing)
41+
accumulator[drawing.date].push(drawing)
4242
return accumulator
43-
}, {} as Partial<DayDrawingSets>) as DayDrawingSets
43+
}, {} as any) as DayDrawingSets
4444

4545
export const drawingDays: string[] = Object.keys(dayDrawingSets).sort().reverse()
46+
47+
export const dayDrawingSetsByYear: DayDrawingSetsByYear = drawingDays.reduce((accumulator, date) => {
48+
const year = parseInt(date.slice(0, 4), 10)
49+
if (!accumulator[year]) accumulator[year] = {}
50+
accumulator[year][date] = dayDrawingSets[date]
51+
return accumulator
52+
}, {} as any) as DayDrawingSetsByYear

explorer/components/YearPage.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
import { Drawing } from '../types/drawing-models'
1+
import React from 'react'
22
import { PageLayout } from './PageLayout'
33
import { DrawingTile } from './DrawingTile'
44
import styled from 'styled-components'
55
import { YearBar } from './YearBar'
6-
import { yearDrawingSets } from '../__fixtures__/drawings'
6+
import { dayDrawingSetsByYear } from '../__fixtures__/drawings'
77

88
export const YearPage: React.FC<{ year: number }> = ({ year }) => {
9-
const drawings = yearDrawingSets[year]
9+
const dayDrawingSets = dayDrawingSetsByYear[year]
10+
const dates = Object.keys(dayDrawingSets)
1011
return (
1112
<PageLayout title={`explodingdog ${year}`}>
1213
<YearBar activeYear={year} />
1314
<YearHeading>{year}</YearHeading>
1415
<DrawingSection>
15-
{drawings && drawings.map(drawing => (
16-
<DrawingTile key={`${drawing.date} ${drawing.slug}`} {...drawing} />
16+
{dates.map(date => (
17+
<DrawingDate>
18+
<DrawingDateHeading>{date}</DrawingDateHeading>
19+
<DrawingDateTiles>
20+
{dayDrawingSets[date].map(drawing => (
21+
<DrawingTile key={`${drawing.date} ${drawing.slug}`} {...drawing} />
22+
))}
23+
</DrawingDateTiles>
24+
</DrawingDate>
1725
))}
1826
</DrawingSection>
1927
</PageLayout>
@@ -28,3 +36,18 @@ const YearHeading = styled.h1.attrs({ className: 'Explorer__YearPage__YearHeadin
2836
const DrawingSection = styled.section.attrs({ className: 'Explorer__YearPage__DrawingSection'})`
2937
text-align: center;
3038
`
39+
40+
const DrawingDate = styled.div.attrs({ className: 'Explorer__YearPage__DrawingDate'})`
41+
margin: 24px 0 48px;
42+
`
43+
44+
const DrawingDateHeading = styled.h3.attrs({ className: 'Explorer__YearPage__DrawingDateHeading'})`
45+
align-items: center;
46+
color: #BBB;
47+
display: flex;
48+
height: 48px;
49+
justify-content: center;
50+
margin: 0;
51+
`
52+
53+
const DrawingDateTiles = styled.div.attrs({ className: 'Explorer__YearPage__DrawingDateTiles'})``

explorer/types/drawing-models.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ export interface DayDrawingSets
1818
extends Readonly<{
1919
[day: string]: Drawing[]
2020
}> {}
21+
22+
export interface DayDrawingSetsByYear
23+
extends Readonly<{
24+
[year: number]: DayDrawingSets
25+
}> {}

0 commit comments

Comments
 (0)