Skip to content

Commit 8a749ea

Browse files
committed
feat: show image blocks on generic pages
1 parent 737240b commit 8a749ea

File tree

7 files changed

+61
-16
lines changed

7 files changed

+61
-16
lines changed

gatsby-node.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import {
66
createFundraiserSchemaCustomization,
77
createFundraisersFromMarkdown,
88
} from './gatsby/fundraisers/transformers'
9+
import { createBlockPhotoResolvers } from './gatsby/generic-pages/resolvers'
910
import { sourceNeedsAssessments } from './gatsby/needs-assessment/sourceNeedsAssessmentData'
1011
import {
1112
createPhotoResolvers,
1213
createPhotoSchemaCustomization,
1314
} from './gatsby/photos/photos'
1415
import transformers from './gatsby/transform-nodes'
15-
1616
/*
1717
Customize the GraqphQL Schema
1818
================================================================================
@@ -59,6 +59,7 @@ export const createResolvers: GatsbyNode['createResolvers'] = (args) => {
5959
resolvers.resolveSubregionFields(args)
6060
resolvers.resolveTeamMemberFields(args)
6161
createPhotoResolvers(args)
62+
createBlockPhotoResolvers(args)
6263
}
6364

6465
/*

gatsby/generic-pages/content-blocks.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ export const schema = `
2222
}
2323
2424
type DABlockImage implements Node {
25-
asset: String!
25+
relativePath: String!
26+
alt: String!
27+
image: ImageSharp!
28+
2629
caption: String
2730
attribution: String!
2831
dateUploaded: Date
2932
date: Date
30-
altText: String!
3133
tags: [String!]!
3234
3335
alignmentPhoto: String
@@ -152,21 +154,22 @@ export const deriveImageBlockNode: DeriveBlockFn = (
152154
parentId,
153155
{ createNodeId, createContentDigest },
154156
) => {
155-
const altText = getStringProperty(block, 'altText')
157+
const alt = getStringProperty(block, 'altText')
156158
return {
157-
asset: getStringProperty(block, 'asset'),
159+
relativePath: getStringProperty(block, 'asset'),
160+
alt: alt,
161+
158162
caption: getStringProperty(block, 'caption'),
159163
attribution: getStringProperty(block, 'attribution'),
160164
dateUploaded: getDateProperty(block, 'dateUploaded'),
161165
date: block?.date ? getDateProperty(block, 'date') : undefined,
162-
altText: altText,
163166
tags: getArrayProperty(block, 'tags'),
164167

165168
alignmentPhoto: getStringProperty(block, 'alignmentPhoto'),
166169
alignmentCaption: getStringProperty(block, 'alignmentCaption'),
167170

168171
// Gatsby Fields
169-
id: createNodeId(`DABlockImage - ${altText}`),
172+
id: createNodeId(`DABlockImage - ${alt}`),
170173
parent: parentId,
171174
children: [],
172175
internal: {

gatsby/generic-pages/resolvers.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { CreateResolversArgs } from 'gatsby'
2+
import { imageSharpResolver } from '../create-resolvers'
3+
4+
export const createBlockPhotoResolvers = (args: CreateResolversArgs) => {
5+
const { createResolvers, getNode } = args
6+
createResolvers({
7+
DABlockImage: {
8+
image: imageSharpResolver(getNode, 'relativePath'),
9+
},
10+
})
11+
}

src/components/section/blocks/BlockImage.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GatsbyImage } from 'gatsby-plugin-image'
12
import { FC } from 'react'
23
import { BlockImage as BlockImageType } from '../../../types/generic-page.d'
34

@@ -7,5 +8,13 @@ type BlockImageProps = {
78
}
89

910
export const BlockImage: FC<BlockImageProps> = ({ block, className }) => {
10-
return <div className={`${className}`}>image</div>
11+
return (
12+
<div className={`${className}`}>
13+
<GatsbyImage
14+
className="w-full"
15+
alt={block.alt}
16+
image={block.image.gatsbyImageData}
17+
/>
18+
</div>
19+
)
1120
}

src/pages/{DAPageGeneric.slug}.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,19 @@ export const query = graphql`
8181
type
8282
}
8383
84-
asset
84+
image {
85+
gatsbyImageData(
86+
width: 640
87+
aspectRatio: 1.2
88+
transformOptions: { fit: COVER }
89+
)
90+
}
91+
alt
92+
8593
caption
8694
attribution
8795
dateUploaded
8896
date
89-
altText
9097
tags
9198
9299
alignmentPhoto

src/types/generic-page.d.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Node, NodeInput } from 'gatsby'
2+
import { GatsbyImage } from 'gatsby-plugin-image'
23
import { LinksList, UpdatesList } from './list.d'
34

45
/*
@@ -190,7 +191,15 @@ export interface BlockImage {
190191
/**
191192
* relative path to media file
192193
*/
193-
asset: string
194+
relativePath: string
195+
/**
196+
* Alternative text
197+
*/
198+
alt: string
199+
/**
200+
* Image process by sharp
201+
*/
202+
image: GatsbyImage
194203

195204
caption?: string
196205

@@ -201,7 +210,6 @@ export interface BlockImage {
201210

202211
dateUploaded?: Date
203212
date?: Date
204-
altText: string
205213
tags: string[]
206214

207215
alignmentPhoto?: 'Left' | 'Center' | 'Right'

src/types/generic-page.test-helpers.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,12 @@ export const getBlockImageData = (props?: Record<string, any>) => {
478478
...props,
479479

480480
asset: '',
481+
alt: 'A Fake Image',
482+
481483
caption: 'An Image Block',
482484
attribution: 'Distribute Aid',
483485
dateUploaded: new Date(2020, 9, 8),
484486
date: new Date(2020, 9, 8),
485-
altText: 'A Fake Image',
486487
tags: ['a tag'],
487488

488489
alignmentPhoto: 'Left',
@@ -498,12 +499,14 @@ export const getBlockImageNodeInput = (props?: Record<string, any>) => {
498499
parent: 'parent-id',
499500
children: [],
500501

501-
asset: '',
502+
relativePath: '',
503+
alt: 'A Fake Image',
504+
image: undefined,
505+
502506
caption: 'An Image Block',
503507
attribution: 'Distribute Aid',
504508
dateUploaded: new Date(2020, 9, 8),
505509
date: new Date(2020, 9, 8),
506-
altText: 'A Fake Image',
507510
tags: ['a tag'],
508511

509512
alignmentPhoto: 'Left',
@@ -526,12 +529,15 @@ export const getBlockImageNode = (props?: Record<string, any>) => {
526529
parent: 'parent-id',
527530
children: [],
528531

532+
relativePath: '',
533+
alt: 'A Fake Image',
534+
image: undefined,
535+
529536
asset: '',
530537
caption: 'An Image Block',
531538
attribution: 'Distribute Aid',
532539
dateUploaded: new Date(2020, 9, 8),
533540
date: new Date(2020, 9, 8),
534-
altText: 'A Fake Image',
535541
tags: ['a tag'],
536542

537543
alignmentPhoto: 'Left',

0 commit comments

Comments
 (0)