Skip to content

Commit

Permalink
feat(pageBuilder): translated slug field for dynamic pages
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Oct 24, 2024
1 parent 39f88b0 commit e9c78fd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
10 changes: 9 additions & 1 deletion src/utils/pageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ async function fetchDynamicPage({
if (!isNonNullQueryResponse(queryResponse)) {
return null;
}
const pathTranslations =
await loadStudioQuery<InternationalizedString | null>(
SLUG_FIELD_TRANSLATIONS_FROM_LANGUAGE_QUERY,
{
slug: path[0],
language,
},
);
return {
queryResponse,
docType: pageBuilderID,
pathTranslations: [],
pathTranslations: pathTranslations.data ?? [],
};
}

Expand Down
11 changes: 2 additions & 9 deletions studio/lib/queries/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const SEO_FRAGMENT = groq`

export const PAGE_FRAGMENT = groq`
...,
"slug": ${translatedFieldFragment("slug")},
${LANGUAGE_FIELD_FRAGMENT},
${SECTIONS_FRAGMENT},
${SEO_FRAGMENT}
Expand All @@ -66,16 +67,8 @@ export const PAGES_SITEMAP_QUERY = groq`
}
`;

export const PAGE_SEO_QUERY = groq`
*[_type == "pageBuilder" && _id == $id][0]{
"title": seo.seoTitle,
"description": seo.seoDescription,
"imageUrl": seo.seoImage.asset->url
}
`;

export const PAGE_BY_SLUG_QUERY = groq`
*[_type == "pageBuilder" && slug.current == $slug][0]{
*[_type == "pageBuilder" && ${translatedFieldFragment("slug")} == $slug][0]{
${PAGE_FRAGMENT}
}
`;
16 changes: 11 additions & 5 deletions studio/schemas/documents/pageBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineField, defineType } from "sanity";

import { StringInputWithCharacterCount } from "studio/components/stringInputWithCharacterCount/StringInputWithCharacterCount";
import { isInternationalizedString } from "studio/lib/interfaces/global";
import article from "studio/schemas/objects/sections/article";
import callout from "studio/schemas/objects/sections/callout";
import callToAction from "studio/schemas/objects/sections/callToAction";
Expand All @@ -11,6 +12,7 @@ import logoSalad from "studio/schemas/objects/sections/logoSalad";
import testimonals from "studio/schemas/objects/sections/testimonials";
import seo from "studio/schemas/objects/seo";
import { pageSlug } from "studio/schemas/schemaTypes/slug";
import { firstTranslation } from "studio/utils/i18n";

export const pageBuilderID = "pageBuilder";

Expand Down Expand Up @@ -53,13 +55,17 @@ const pageBuilder = defineType({
preview: {
select: {
title: "page",
urlSlug: "slug.current",
slug: pageSlug.name,
},
prepare(selection) {
const { title, urlSlug } = selection;
prepare({ title, slug }) {
if (!isInternationalizedString(slug)) {
throw new TypeError(
`Expected 'slug' to be InternationalizedString, was ${typeof slug}`,
);
}
return {
title: title,
subtitle: urlSlug,
title: title ?? undefined,
subtitle: firstTranslation(slug) ?? undefined,
};
},
},
Expand Down

0 comments on commit e9c78fd

Please sign in to comment.