Skip to content

Commit

Permalink
V3 customer case field translation (#782)
Browse files Browse the repository at this point in the history
* Field translation for customer cases in /shared

* refactor(customerCases): format GROQ query and add language field fragment

---------

Co-authored-by: Mathias Oterhals Myklebust <[email protected]>
  • Loading branch information
anemne and mathiazom authored Oct 16, 2024
1 parent ca46b0a commit 6467d13
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 47 deletions.
13 changes: 1 addition & 12 deletions studioShared/deskStructure.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { StructureResolver } from "sanity/structure";

import { defaultLanguage } from "i18n/supportedLanguages";

import { customerCaseID } from "./schemas/documents/customerCase";

export const deskStructure: StructureResolver = (S) =>
Expand All @@ -10,14 +8,5 @@ export const deskStructure: StructureResolver = (S) =>
.items([
S.listItem()
.title("Customer cases")
.child(
S.documentTypeList(customerCaseID)
.title("Customer cases")
// only show costumer cases in the base language
.filter("_type == $type && language == $lang")
.params({
type: customerCaseID,
lang: defaultLanguage?.id,
}),
),
.child(S.documentTypeList(customerCaseID).title("Customer cases")),
]);
10 changes: 9 additions & 1 deletion studioShared/lib/queries/customerCases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { groq } from "next-sanity";

export const CUSTOMER_CASES_QUERY = groq`*[_type == "customerCase" && language == $language]
import { LANGUAGE_FIELD_FRAGMENT } from "studio/lib/queries/i18n";
import { translatedFieldFragment } from "studio/lib/queries/utils/i18n";

export const CUSTOMER_CASES_QUERY = groq`
*[_type == "customerCase"]{
${LANGUAGE_FIELD_FRAGMENT},
"basicTitle": ${translatedFieldFragment("basicTitle")},
"richText": ${translatedFieldFragment("richText")}
}
`;

export const CUSTOMER_CASE_QUERY = groq`*[_type == "customerCase" && slug.current == $slug && language == $language][0]`;
Expand Down
4 changes: 3 additions & 1 deletion studioShared/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { type SchemaTypeDefinition } from "sanity";

import { richText } from "studio/schemas/fields/text";

import customerCases from "./schemas/documents/customerCase";

export const schema: { types: SchemaTypeDefinition[] } = {
types: [customerCases],
types: [customerCases, richText],
};
40 changes: 23 additions & 17 deletions studioShared/schemas/documents/customerCase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { defineField, defineType } from "sanity";

import languageSchemaField from "i18n/languageSchemaField";
import { richText, title } from "studio/schemas/fields/text";
import {
isSlugUniqueAcrossDocuments,
titleSlug,
} from "studio/schemas/schemaTypes/slug";
import { isInternationalizedString } from "studio/lib/interfaces/global";
import { richTextID, titleID } from "studio/schemas/fields/text";
import { titleSlug } from "studio/schemas/schemaTypes/slug";
import { firstTranslation } from "studio/utils/i18n";

export const customerCaseID = "customerCase";

Expand All @@ -14,27 +12,35 @@ const customerCase = defineType({
type: "document",
title: "Customer Case",
fields: [
languageSchemaField,
title,
{
name: titleID.basic,
type: "internationalizedArrayString",
title: "Customer Case Title",
},
{
...titleSlug,
options: {
...titleSlug.options,
isUnique: (slug, ctx) =>
isSlugUniqueAcrossDocuments(slug, ctx, customerCaseID),
},
type: "internationalizedArrayString",
},
defineField({
...richText,
description: "Enter the body content of the Customer case.",
name: richTextID,
title: "Body",
type: "internationalizedArrayRichText",
}),
],
preview: {
select: {
title: "basicTitle",
title: titleID.basic,
},
prepare({ title }) {
return { title, subtitle: "Customer case" };
if (!isInternationalizedString(title)) {
throw new TypeError(
`Expected 'title' to be InternationalizedString, was ${typeof title}`,
);
}
return {
title: firstTranslation(title) ?? undefined,
subtitle: "Customer case",
};
},
},
});
Expand Down
21 changes: 5 additions & 16 deletions studioShared/studioConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { documentInternationalization } from "@sanity/document-internationalization";
import { visionTool } from "@sanity/vision";
import { WorkspaceOptions } from "sanity";
import { structureTool } from "sanity/structure";
import { internationalizedArray } from "sanity-plugin-internationalized-array";
import { media } from "sanity-plugin-media";

import { languageID } from "i18n/languageSchemaField";
import { defaultLanguage, supportedLanguages } from "i18n/supportedLanguages";
import { supportedLanguages } from "i18n/supportedLanguages";
import StudioIcon from "studio/components/studioIcon/StudioIcon";

import { deskStructure } from "./deskStructure";
import { apiVersion, dataset, projectId } from "./env";
import { schema } from "./schema";
import { customerCaseID } from "./schemas/documents/customerCase";

const config: WorkspaceOptions = {
name: "sharedStudio",
Expand All @@ -23,25 +21,16 @@ const config: WorkspaceOptions = {
dataset,
schema: {
...schema,
templates: (prev) =>
prev.filter(
(template) => template.value.language === defaultLanguage?.id,
),
},
plugins: [
structureTool({
structure: deskStructure,
}),
visionTool({ defaultApiVersion: apiVersion }),
media(),
documentInternationalization({
supportedLanguages: supportedLanguages,
schemaTypes: [customerCaseID],
languageField: languageID,
apiVersion,
// Optional. Adds UI for publishing all translations at once. Requires access to the Scheduling API
// https://www.sanity.io/docs/scheduling-api
// bulkPublish: true,
internationalizedArray({
languages: supportedLanguages,
fieldTypes: ["string", "richText"],
}),
],
};
Expand Down

0 comments on commit 6467d13

Please sign in to comment.