Skip to content

Commit

Permalink
News and Events Search (#579)
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Fettet <[email protected]>
Co-authored-by: Louis Fettet <[email protected]>
Co-authored-by: Dan Hinze <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2024
1 parent fe0c1ee commit 9aa03aa
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 300 deletions.
94 changes: 94 additions & 0 deletions src/lib/loadEventsNewsMetadataMap.ts
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;
};
6 changes: 1 addition & 5 deletions src/lib/loadPageMetadataMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export type PageMetadataMapItem = NonNullable<
breadcrumbs?: Breadcrumbs;
};

// This type is used for search indexing in /api/v1/* endpoints
export type PageMetadataMapItemWithObjectID = PageMetadataMapItem & {
objectID: string;
};

export type PageMetadataMap = Map<string, PageMetadataMapItem>;

const pageMetadataItems = gql`
Expand All @@ -32,6 +27,7 @@ const pageMetadataItems = gql`
sys {
id
}
__typename
title
slug
isRoot
Expand Down
100 changes: 0 additions & 100 deletions src/lib/services/server/contentful/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1463,92 +1463,6 @@ enum EventEntryOrder {
sys_publishedVersion_DESC
}

"""
One-off content type for managing the description text on the fire danger page [See type definition](https://app.contentful.com/spaces/pc5e1rlgfrov/content_types/fireDangerMap)
"""
type FireDangerMap implements Entry & _Node {
_id: ID!
contentfulMetadata: ContentfulMetadata!
description(locale: String): FireDangerMapDescription
linkedFrom(allowedLocales: [String]): FireDangerMapLinkingCollections
pageMetadata(locale: String, preview: Boolean, where: PageMetadataFilter): PageMetadata
sys: Sys!
}

type FireDangerMapCollection {
items: [FireDangerMap]!
limit: Int!
skip: Int!
total: Int!
}

type FireDangerMapDescription {
json: JSON!
links: FireDangerMapDescriptionLinks!
}

type FireDangerMapDescriptionAssets {
block: [Asset]!
hyperlink: [Asset]!
}

type FireDangerMapDescriptionEntries {
block: [Entry]!
hyperlink: [Entry]!
inline: [Entry]!
}

type FireDangerMapDescriptionLinks {
assets: FireDangerMapDescriptionAssets!
entries: FireDangerMapDescriptionEntries!
resources: FireDangerMapDescriptionResources!
}

type FireDangerMapDescriptionResources {
block: [FireDangerMapDescriptionResourcesBlock!]!
hyperlink: [FireDangerMapDescriptionResourcesHyperlink!]!
inline: [FireDangerMapDescriptionResourcesInline!]!
}

type FireDangerMapDescriptionResourcesBlock implements ResourceLink {
sys: ResourceSys!
}

type FireDangerMapDescriptionResourcesHyperlink implements ResourceLink {
sys: ResourceSys!
}

type FireDangerMapDescriptionResourcesInline implements ResourceLink {
sys: ResourceSys!
}

input FireDangerMapFilter {
AND: [FireDangerMapFilter]
OR: [FireDangerMapFilter]
contentfulMetadata: ContentfulMetadataFilter
description_contains: String
description_exists: Boolean
description_not_contains: String
pageMetadata: cfPageMetadataNestedFilter
pageMetadata_exists: Boolean
sys: SysFilter
}

type FireDangerMapLinkingCollections {
entryCollection(limit: Int = 100, locale: String, preview: Boolean, skip: Int = 0): EntryCollection
}

enum FireDangerMapOrder {
sys_firstPublishedAt_ASC
sys_firstPublishedAt_DESC
sys_id_ASC
sys_id_DESC
sys_publishedAt_ASC
sys_publishedAt_DESC
sys_publishedVersion_ASC
sys_publishedVersion_DESC
}

"""
Big wide image intended for the top of a page. [See type definition](https://app.contentful.com/spaces/pc5e1rlgfrov/content_types/heroImage)
"""
Expand Down Expand Up @@ -3187,7 +3101,6 @@ union PageMetadataInternalRedirect = EventEntry | News | PageMetadata
type PageMetadataLinkingCollections {
aggregationCollection(limit: Int = 100, locale: String, order: [PageMetadataLinkingCollectionsAggregationCollectionOrder], preview: Boolean, skip: Int = 0): AggregationCollection
entryCollection(limit: Int = 100, locale: String, preview: Boolean, skip: Int = 0): EntryCollection
fireDangerMapCollection(limit: Int = 100, locale: String, order: [PageMetadataLinkingCollectionsFireDangerMapCollectionOrder], preview: Boolean, skip: Int = 0): FireDangerMapCollection
homeCollection(limit: Int = 100, locale: String, order: [PageMetadataLinkingCollectionsHomeCollectionOrder], preview: Boolean, skip: Int = 0): HomeCollection
menuItemCollection(limit: Int = 100, locale: String, order: [PageMetadataLinkingCollectionsMenuItemCollectionOrder], preview: Boolean, skip: Int = 0): MenuItemCollection
officePageCollection(limit: Int = 100, locale: String, order: [PageMetadataLinkingCollectionsOfficePageCollectionOrder], preview: Boolean, skip: Int = 0): OfficePageCollection
Expand All @@ -3212,17 +3125,6 @@ enum PageMetadataLinkingCollectionsAggregationCollectionOrder {
title_DESC
}

enum PageMetadataLinkingCollectionsFireDangerMapCollectionOrder {
sys_firstPublishedAt_ASC
sys_firstPublishedAt_DESC
sys_id_ASC
sys_id_DESC
sys_publishedAt_ASC
sys_publishedAt_DESC
sys_publishedVersion_ASC
sys_publishedVersion_DESC
}

enum PageMetadataLinkingCollectionsHomeCollectionOrder {
commissionerByline_ASC
commissionerByline_DESC
Expand Down Expand Up @@ -3623,8 +3525,6 @@ type Query {
errorCollection(limit: Int = 100, locale: String, order: [ErrorOrder], preview: Boolean, skip: Int = 0, where: ErrorFilter): ErrorCollection
eventEntry(id: String!, locale: String, preview: Boolean): EventEntry
eventEntryCollection(limit: Int = 100, locale: String, order: [EventEntryOrder], preview: Boolean, skip: Int = 0, where: EventEntryFilter): EventEntryCollection
fireDangerMap(id: String!, locale: String, preview: Boolean): FireDangerMap
fireDangerMapCollection(limit: Int = 100, locale: String, order: [FireDangerMapOrder], preview: Boolean, skip: Int = 0, where: FireDangerMapFilter): FireDangerMapCollection
heroImage(id: String!, locale: String, preview: Boolean): HeroImage
heroImageCollection(limit: Int = 100, locale: String, order: [HeroImageOrder], preview: Boolean, skip: Int = 0, where: HeroImageFilter): HeroImageCollection
home(id: String!, locale: String, preview: Boolean): Home
Expand Down
Loading

0 comments on commit 9aa03aa

Please sign in to comment.