Skip to content

Commit

Permalink
Merge pull request #10879 from wellcomecollection/guide-links-on-exhi…
Browse files Browse the repository at this point in the history
…bition-page

Guide links on exhibition page
  • Loading branch information
gestchild authored May 22, 2024
2 parents 6dcd634 + 24b2bfb commit 2666669
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 24 deletions.
7 changes: 6 additions & 1 deletion common/services/prismic/link-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ function linkResolver(doc: Props | DataProps): string {

if (type === 'webcomics') return `/articles/${id}`;
if (type === 'webcomic-series') return `/series/${id}`;
if (type === 'exhibition-guides') return `/guides/exhibitions/${id}`;
if (
type === 'exhibition-guides' ||
type === 'exhibition-texts' ||
type === 'exhibition-highlight-tours'
)
return `/guides/exhibitions/${id}`;

if (type === 'visual-stories') {
if ('data' in doc) {
Expand Down
24 changes: 12 additions & 12 deletions content/webapp/pages/exhibitions/[exhibitionId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ export const getServerSideProps: GetServerSideProps<
}

const client = createClient(context);
const { exhibition, pages, visualStories, exhibitionGuides } =
await fetchExhibition(client, exhibitionId);
const { exhibition, pages, visualStories, allGuides } = await fetchExhibition(
client,
exhibitionId
);

if (isNotUndefined(exhibition)) {
const serverData = await getServerData(context);
Expand All @@ -98,16 +100,14 @@ export const getServerSideProps: GetServerSideProps<
type: 'visual-story',
};
});
const exhibitionGuidesLinks = exhibitionGuides.results.map(
exhibitionGuide => {
const url = linkResolver(exhibitionGuide);
return {
text: exhibitionGuideLinkText,
url,
type: 'exhibition-guide',
};
}
);
const exhibitionGuidesLinks = allGuides.map(exhibitionGuide => {
const url = linkResolver(exhibitionGuide);
return {
text: exhibitionGuideLinkText,
url,
type: 'exhibition-guide',
};
});

const jsonLd = exhibitionLd(exhibitionDoc);

Expand Down
90 changes: 79 additions & 11 deletions content/webapp/services/prismic/fetch/exhibitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import { fetchPages } from './pages';
import { fetchVisualStories } from './visual-stories';
import { fetchExhibitionGuides } from '@weco/content/services/prismic/fetch/exhibition-guides';
import { fetchExhibitionTexts } from '@weco/content/services/prismic/fetch/exhibition-texts';
import { fetchExhibitionHighlightTours } from '@weco/content/services/prismic/fetch/exhibition-highlight-tours';
import * as prismic from '@prismicio/client';
import { PagePrismicDocument } from '../types/pages';
import { VisualStoryDocument } from '../types/visual-stories';
Expand All @@ -21,7 +23,6 @@ import {
Exhibition,
ExhibitionRelatedContent,
} from '../../../types/exhibitions';
import { ExhibitionGuidePrismicDocument } from '../types/exhibition-guides';
import {
articleFormatsFetchLinks,
contributorFetchLinks,
Expand All @@ -30,6 +31,7 @@ import {
exhibitionsFetchLinks,
seasonsFetchLinks,
} from '../types';
import { isFilledLinkToDocument } from '@weco/common/services/prismic/types';
import { placesFetchLinks } from '../types/places';
import { teamsFetchLinks } from '../types/teams';
import {
Expand Down Expand Up @@ -58,11 +60,29 @@ const exhibitionsFetcher = fetcher<ExhibitionPrismicDocument>(
fetchLinks
);

function returnEmptyResults() {
return {
page: 1,
results_per_page: 20,
results_size: 0,
total_results_size: 0,
total_pages: 0,
next_page: null,
prev_page: null,
results: [],
version: '',
license: '',
};
}

export type FetchExhibitionResult = {
exhibition?: ExhibitionPrismicDocument;
pages: prismic.Query<PagePrismicDocument>;
visualStories: prismic.Query<VisualStoryDocument>;
exhibitionGuides: prismic.Query<ExhibitionGuidePrismicDocument>;
allGuides: {
id: string;
type: string;
}[];
};

export async function fetchExhibition(
Expand All @@ -78,22 +98,70 @@ export async function fetchExhibition(
filters: [prismic.filter.at('my.visual-stories.relatedDocument', id)],
});
const exhibitionGuidesQueryPromise = fetchExhibitionGuides(client, {
filters: [prismic.filter.at('my.exhibition-guides.related-exhibition', id)], // TODO consider renaming field to follow naming convention
filters: [prismic.filter.at('my.exhibition-guides.related-exhibition', id)],
});

const [exhibition, pages, visualStories, exhibitionGuides] =
await Promise.all([
exhibitionPromise,
pageQueryPromise,
visualStoriesQueryPromise,
exhibitionGuidesQueryPromise,
]);
const exhibitionTextsQueryPromise = fetchExhibitionTexts(client, {
filters: [prismic.filter.at('my.exhibition-texts.related_exhibition', id)],
}).catch(returnEmptyResults);

const exhibitionHighlightToursQueryPromise = fetchExhibitionHighlightTours(
client,
{
filters: [
prismic.filter.at(
'my.exhibition-highlight-tours.related_exhibition',
id
),
],
}
).catch(returnEmptyResults);

const [
exhibition,
pages,
visualStories,
exhibitionGuidesQuery,
exhibitionTextsQuery,
exhibitionHighlightToursQuery,
] = await Promise.all([
exhibitionPromise,
pageQueryPromise,
visualStoriesQueryPromise,
exhibitionGuidesQueryPromise,
exhibitionTextsQueryPromise,
exhibitionHighlightToursQueryPromise,
]);

// The exhibitionTexts and exhibitionHighlightTours can belong to the same
// exhibition, but we only want to provide one link to the guide index page,
// so we merge the ones with the same related_exhibition id.
// We also want to include the deprecated ExhibitionGuides.
const allGuides = [
...new Map(
[
...exhibitionTextsQuery.results,
...exhibitionHighlightToursQuery.results,
].map(item => {
const relatedExhibition = isFilledLinkToDocument(
item.data.related_exhibition
)
? item.data.related_exhibition
: undefined;
return [relatedExhibition?.id, item];
})
).values(),
...exhibitionGuidesQuery.results,
].map(guide => ({
id: guide.id,
type: guide.type,
}));

return {
exhibition,
pages,
visualStories,
exhibitionGuides,
allGuides,
};
}

Expand Down

0 comments on commit 2666669

Please sign in to comment.