Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 - translated slug for dynamic pages #809

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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