-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Louis Fettet <[email protected]> Co-authored-by: Louis Fettet <[email protected]> Co-authored-by: Dan Hinze <[email protected]>
- Loading branch information
1 parent
fe0c1ee
commit 9aa03aa
Showing
8 changed files
with
182 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import gql from "graphql-tag"; | ||
import { print as printQuery } from "graphql"; | ||
import constructEventSlug from "./util/constructEventSlug"; | ||
import type { ContentfulClient } from "$lib/services/server/contentful"; | ||
import type { PageMetadataMapItem } from "$lib/loadPageMetadataMap"; | ||
|
||
import type { NewsAndEventsCollectionQuery } from "./$queries.generated"; | ||
|
||
const queryNewsAndEvents = gql` | ||
query NewsAndEventsCollection { | ||
newsCollection(where: { indexInSearch: true }, order: [publicationDate_DESC]) { | ||
items { | ||
sys { | ||
id | ||
} | ||
__typename | ||
type | ||
title | ||
subhead | ||
publicationDate | ||
slug | ||
byline | ||
metaTitle | ||
metaDescription | ||
} | ||
} | ||
eventEntryCollection(where: { indexInSearch: true }, order: [eventDateAndTime_ASC]) { | ||
items { | ||
sys { | ||
id | ||
} | ||
__typename | ||
slug | ||
shortTitle | ||
eventSummary | ||
eventDateAndTime | ||
metaTitle | ||
metaDescription | ||
} | ||
} | ||
} | ||
`; | ||
|
||
type NewsSearchIndexingMetadataMapItem = NonNullable< | ||
NewsAndEventsCollectionQuery["newsCollection"] | ||
>["items"][number]; | ||
type EventSearchIndexingMetadataMapItem = NonNullable< | ||
NewsAndEventsCollectionQuery["eventEntryCollection"] | ||
>["items"][number]; | ||
|
||
type AdditionalMetadataFields = { url?: string | null; children?: string[] }; | ||
|
||
export type SearchIndexingMetadataMapItem = | ||
| PageMetadataMapItem | ||
| (NewsSearchIndexingMetadataMapItem & AdditionalMetadataFields) | ||
| (EventSearchIndexingMetadataMapItem & AdditionalMetadataFields); | ||
|
||
export const loadEventsAndNewsMap = async ({ | ||
contentfulClient, | ||
}: { | ||
contentfulClient?: ContentfulClient; | ||
}): Promise<Map<string, SearchIndexingMetadataMapItem>> => { | ||
const entryMap: NonNullable<Map<string, SearchIndexingMetadataMapItem>> = new Map(); | ||
|
||
if (!contentfulClient) return entryMap; | ||
|
||
const data = await contentfulClient.fetch<NewsAndEventsCollectionQuery>( | ||
printQuery(queryNewsAndEvents), | ||
); | ||
|
||
if (data?.newsCollection?.items && data?.newsCollection?.items.length > 0) { | ||
const allNewsMetadata = data.newsCollection.items; | ||
allNewsMetadata.forEach((item) => { | ||
if (!item) return; | ||
const url = "about/news/article/" + item.slug; | ||
entryMap.set(item.sys.id, { ...item, url }); | ||
}); | ||
} | ||
if (data?.eventEntryCollection?.items && data?.eventEntryCollection?.items.length > 0) { | ||
const allEventMetadata = data.eventEntryCollection.items; | ||
|
||
allEventMetadata.forEach((item) => { | ||
if (!item || !item.eventDateAndTime || !item.slug) return; | ||
// if event date has changed, the URL should update as well | ||
const url = | ||
item.eventDateAndTime && item.slug | ||
? `/about/events/event/${constructEventSlug(new Date(item.eventDateAndTime), item.slug)}` | ||
: null; | ||
entryMap.set(item.sys.id, { ...item, url }); | ||
}); | ||
} | ||
|
||
return entryMap; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.