Skip to content

Commit

Permalink
Merge branch 'padms/1864' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
padms committed Nov 3, 2023
2 parents a0938e3 + 852adbf commit 5106c74
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 169 deletions.
58 changes: 58 additions & 0 deletions search/IndexSanityContent/common/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { pipe } from 'fp-ts/lib/function'
import { TextBlockIndex, AccordionIndex } from '../../common'
import { TopicPage } from '../topic/sanity'
import { MagazineArticle } from '../magazine/sanity'
import { option } from 'fp-ts'

type PageType = TopicPage | MagazineArticle

export type Mappable = {
_key: string
title: string
ingress: string
}
export type MappableTextBlockType = Mappable & {
text: string
}

export type MappableAccordionType = Mappable & {
accordionItems: {
_key: string
title: string
content: string
}[]
}

// map textblocks
export const mappedTextBlocks = (page: PageType) =>
pipe(
option.fromNullable(page.textBlocks as MappableTextBlockType[]),
option.map((textBlocks) =>
textBlocks.map((textBlock) => {
return {
title: textBlock.title,
ingress: textBlock.ingress,
objectID: `${page._id}-${textBlock._key}`,
text: textBlock.text,
}
}),
),
option.getOrElse(() => [] as TextBlockIndex[]),
)

// Map accordions
export const mappedAccordions = (page: PageType) =>
pipe(
option.fromNullable(page.accordions as MappableAccordionType[]),
option.map((accordions) =>
accordions.map((accordion) => {
return {
title: accordion.title,
ingress: accordion.ingress,
objectID: `${page._id}-${accordion._key}`,
text: accordion.accordionItems?.map((item) => item.title + ': ' + item.content).join(' '),
}
}),
),
option.getOrElse(() => [] as AccordionIndex[]),
)
5 changes: 5 additions & 0 deletions search/IndexSanityContent/common/news/SharedNewsFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ export type SharedNewsFields = Page & {
text: string
}[]
}[]
factboxes: {
blockKey: string
title: string
text: string
}[]
_id: string
}
37 changes: 35 additions & 2 deletions search/IndexSanityContent/localNews/mapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,35 @@ describe('Local News', () => {
],
},
],
factboxes: [
{
blockKey: 'factboxkey',
title: 'Facts',
text: 'Factbox text',
},
],
}

const sut = mapData
const result = sut(newsArticle)

it('contains one entry', () => {
expect(result).toHaveLength(1)
expect(result).toHaveLength(2)
})

it('entry looks as expected', () => {
expect(result[1]).toEqual({
slug: '/a/slug',
objectID: 'id-factboxkey',
pageTitle: 'title',
ingress: 'ingress',
type: 'localNews',
text: 'Facts: Factbox text',
publishDateTime: '2021-11-26T07:00:00.000Z',
year: 2021,
localNewsTag: 'Germany',
} as NewsIndex)

expect(result[0]).toEqual({
slug: '/a/slug',
objectID: 'id-blockKey-childKey',
Expand Down Expand Up @@ -86,13 +105,25 @@ describe('Local News', () => {
],
},
],
factboxes: [
{
blockKey: 'factboxkey',
title: 'Facts',
text: 'Factbox text',
},
{
blockKey: 'factboxkey1',
title: 'Facts1',
text: 'Factbox text1',
},
],
}

const sut = mapData
const result = sut(newsArticle)

it('contains one entry', () => {
expect(result).toHaveLength(4)
expect(result).toHaveLength(6)
})

it('entry looks as expected', () => {
Expand Down Expand Up @@ -156,6 +187,7 @@ describe('Local News', () => {
children: [],
},
],
factboxes: [],
}

const sut = mapData
Expand All @@ -174,6 +206,7 @@ describe('Local News', () => {
localNewsTag: 'USA',
_id: 'id',
blocks: [],
factboxes: [],
}

const sut = mapData
Expand Down
18 changes: 17 additions & 1 deletion search/IndexSanityContent/localNews/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { LocalNewsArticle } from './sanity'

type MapDataType = (article: LocalNewsArticle) => NewsIndex[]
export const mapData: MapDataType = (article) => {
const { publishDateTime, localNewsTag, title, ingress, slug } = article
const { publishDateTime, localNewsTag, title, ingress, slug, factboxes } = article
// Hu hei hvor det går
const year = publishDateTime ? new Date(publishDateTime).getFullYear() : ''
return pipe(
Expand All @@ -25,5 +25,21 @@ export const mapData: MapDataType = (article) => {
year,
} as NewsIndex),
),
A.concat(
factboxes.map(
(factbox) =>
({
slug,
objectID: `${article._id}-${factbox.blockKey}`,
type: 'localNews',
pageTitle: title,
ingress,
text: factbox.title + ': ' + factbox.text,
publishDateTime: publishDateTime,
localNewsTag,
year,
} as NewsIndex),
),
),
)
}
5 changes: 5 additions & 0 deletions search/IndexSanityContent/localNews/sanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export const query = /* groq */ `*[_type == "localNews" && _lang == $lang && !(_
"text": text
}
},
"factboxes": content[_type == "factbox"] {
"blockKey": _key,
title,
"text": pt::text(content)
},
"docToClear": _id match $id
}
`
Expand Down
90 changes: 25 additions & 65 deletions search/IndexSanityContent/magazine/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { pipe } from 'fp-ts/lib/function'
import { ap } from 'fp-ts/lib/Identity'
import * as A from 'fp-ts/lib/Array'
import * as O from 'fp-ts/lib/Option'
import { ImageWithAlt, MagazineArticle } from './sanity'
import { MagazineIndex } from '../../common'
import type { ImageWithAltAndCaption } from './sanity'

type MappableObjectType = {
_key: string
title: string
ingress: string
text: string
magazineTags?: string[]
heroFigure?: ImageWithAltAndCaption
publishDateTime?: string
}

type MapperFunctionType = (article: MagazineArticle) => (obj: MappableObjectType) => MagazineIndex
import { AccordionIndex, MagazineIndex, TextBlockIndex } from '../../common'
import { mappedAccordions, mappedTextBlocks } from '../common/mappers'

const getHeroImage = (article: MagazineArticle): ImageWithAlt | null => {
if (article?.heroFigure?.image?.asset) {
Expand All @@ -30,55 +17,28 @@ const getHeroImage = (article: MagazineArticle): ImageWithAlt | null => {
return null
}

const mapperFunction: MapperFunctionType =
(article) =>
({ _key, title, ingress, text }) => ({
slug: article.slug,
objectID: `${article._id}-${_key}`,
documentID: article._id,
type: 'magazine',
pageTitle: article.title,
title,
ingress,
text,
magazineTags: article.magazineTags,
heroImage: getHeroImage(article),
publishDateTime: article.publishDateTime,
})

type MapDataType = (article: MagazineArticle) => MagazineIndex[]
export const mapData: MapDataType = (article) =>
pipe(mapperFunction, ap(article), (fn) =>
pipe(
pipe(
O.fromNullable(article.textBlocks),
O.map(A.map(fn)),
O.getOrElse(() => [] as MagazineIndex[]),
),
A.concat(
pipe(
O.fromNullable(article.accordions),
O.map(A.map(fn)),
O.getOrElse(() => [] as MagazineIndex[]),
),
),
A.concat(
pipe(
O.fromNullable(
article.ingress
? Array.of({
_key: `ingress`,
type: 'magazine',
pageTitle: article.title,
title: null,
ingress: article.ingress,
text: null,
} as unknown as MappableObjectType)
: null,
),
O.map(A.map(fn)),
O.getOrElse(() => [] as MagazineIndex[]),
),
),
),
type ContentsDataType = (page: MagazineArticle) => AccordionIndex[] | TextBlockIndex[]

const contents: ContentsDataType = (page) => pipe(page, mappedAccordions, A.concat(mappedTextBlocks(page)))
export const mapData: MapDataType = (article: MagazineArticle) =>
pipe(
article,
contents,
O.fromNullable,
O.map((items) => {
return items.map((it) => {
return {
slug: article.slug,
documentID: article._id,
type: 'magazine',
pageTitle: article.title,
magazineTags: article.magazineTags,
heroImage: getHeroImage(article),
publishDateTime: article.publishDateTime,
...it,
} as unknown as MagazineIndex
})
}),
O.getOrElse(() => [] as MagazineIndex[]),
)
23 changes: 10 additions & 13 deletions search/IndexSanityContent/magazine/sanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as TE from 'fp-ts/lib/TaskEither'
import { pipe } from 'fp-ts/lib/function'
import { SanityClient } from '@sanity/client'
import { Language } from '../../common'
import { MappableAccordionType, MappableTextBlockType } from '../common/mappers'

export enum HeroTypes {
DEFAULT = 'default',
Expand Down Expand Up @@ -38,7 +39,12 @@ export const query = /* groq */ `*[_type == "magazine" && _lang == $lang && !(_i
"accordions": content[_type == "accordion"] {
"_key": _key,
"title": pt::text(title),
"ingress": pt::text(ingress)
"ingress": pt::text(ingress),
"accordionItems":accordion[]{
"id": _key,
title,
"content": pt::text(content)
}
},
"magazineTags": magazineTags[]->.title[$lang],
"heroFigure": select(
Expand Down Expand Up @@ -90,18 +96,9 @@ export type MagazineArticle = {
slug: string
title: string
ingress: string
textBlocks: {
_key: string
title: string
ingress: string
text: string
}[]
accordions: {
_key: string
title: string
ingress: string
text: string
}[]
type: string
textBlocks: MappableTextBlockType[]
accordions: MappableAccordionType[]
_id: string
magazineTags?: string[]
heroFigure?: { image: ImageWithAlt } | ImageWithAltAndCaption
Expand Down
Loading

0 comments on commit 5106c74

Please sign in to comment.