From a57d06cee17ed90f6d170775e7b2e3a20451b9d3 Mon Sep 17 00:00:00 2001 From: Ane Date: Thu, 26 Sep 2024 08:27:40 +0200 Subject: [PATCH] replace legaldocumentquery with language param, to retrieve legal document to spesific lang --- src/app/(main)/layout.tsx | 6 ++-- src/app/(main)/legal/[id]/page.tsx | 7 +++-- src/components/navigation/footer/Footer.tsx | 4 +-- studio/lib/interfaces/legalDocuments.ts | 12 +++++-- studio/lib/queries/legalDocuments.ts | 8 ++--- studio/studioConfig.tsx | 35 ++++++--------------- 6 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/app/(main)/layout.tsx b/src/app/(main)/layout.tsx index 9a4763ff2..9efaac746 100644 --- a/src/app/(main)/layout.tsx +++ b/src/app/(main)/layout.tsx @@ -11,7 +11,7 @@ import { Navigation } from "studio/lib/interfaces/navigation"; import { SocialMediaProfiles } from "studio/lib/interfaces/socialMedia"; import { BRAND_ASSETS_QUERY } from "studio/lib/queries/brandAssets"; import { COMPANY_INFO_QUERY } from "studio/lib/queries/companyDetails"; -import { LEGAL_DOCUMENTS_QUERY } from "studio/lib/queries/legalDocuments"; +import { LEGAL_DOCUMENTS_BY_LANG_QUERY } from "studio/lib/queries/legalDocuments"; import { NAV_QUERY } from "studio/lib/queries/navigation"; import { SOMEPROFILES_QUERY } from "studio/lib/queries/socialMediaProfiles"; import { loadStudioQuery } from "studio/lib/store"; @@ -42,8 +42,8 @@ export default async function Layout({ { perspective }, ), loadStudioQuery( - LEGAL_DOCUMENTS_QUERY, - {}, + LEGAL_DOCUMENTS_BY_LANG_QUERY, + { language: "en" }, //TODO: replace this with selected language for the page { perspective }, ), loadStudioQuery(BRAND_ASSETS_QUERY, {}, { perspective }), diff --git a/src/app/(main)/legal/[id]/page.tsx b/src/app/(main)/legal/[id]/page.tsx index 9013bad4f..efeda4d0f 100644 --- a/src/app/(main)/legal/[id]/page.tsx +++ b/src/app/(main)/legal/[id]/page.tsx @@ -2,7 +2,7 @@ import Legal from "src/blog/components/legal/Legal"; import LegalPreview from "src/blog/components/legal/LegalPreview"; import { getDraftModeInfo } from "src/utils/draftmode"; import { LegalDocument } from "studio/lib/interfaces/legalDocuments"; -import { LEGAL_DOCUMENT_SLUG_QUERY } from "studio/lib/queries/legalDocuments"; +import { LEGAL_DOCUMENTS_BY_SLUG_AND_LANG_QUERY } from "studio/lib/queries/legalDocuments"; import { loadStudioQuery } from "studio/lib/store"; export const dynamic = "force-dynamic"; @@ -20,8 +20,8 @@ async function Page({ params }: Props) { const { perspective, isDraftMode } = getDraftModeInfo(); const initialDocument = await loadStudioQuery( - LEGAL_DOCUMENT_SLUG_QUERY, - { slug: id }, + LEGAL_DOCUMENTS_BY_SLUG_AND_LANG_QUERY, + { slug: id, language: "en" }, //TODO: replace this with selected language for the page { perspective }, ); @@ -32,6 +32,7 @@ async function Page({ params }: Props) { if (isDraftMode) { return ; } + if (initialDocument) { return ; } diff --git a/src/components/navigation/footer/Footer.tsx b/src/components/navigation/footer/Footer.tsx index f1c9a400d..8851dd57e 100644 --- a/src/components/navigation/footer/Footer.tsx +++ b/src/components/navigation/footer/Footer.tsx @@ -54,7 +54,7 @@ const Footer = ({ {legalData?.map((legal) => { const path = `legal/${legal.slug.current}`; const link = { - _key: legal._key, + _key: legal._id, _type: legal._type, linkTitle: legal.basicTitle, linkType: LinkType.Internal, @@ -63,7 +63,7 @@ const Footer = ({ }, }; return ( -
  • +
  • ); diff --git a/studio/lib/interfaces/legalDocuments.ts b/studio/lib/interfaces/legalDocuments.ts index 929c3826e..ac02152e7 100644 --- a/studio/lib/interfaces/legalDocuments.ts +++ b/studio/lib/interfaces/legalDocuments.ts @@ -3,9 +3,17 @@ import { PortableTextBlock } from "sanity"; import { Slug } from "./global"; export interface LegalDocument { - _key: string; + _id: string; _type: string; - basicTitle: string; slug: Slug; + basicTitle: string; richText: PortableTextBlock[]; + _translations: Translations[]; +} + +interface Translations { + basicTitle: string; + language: string; + richText: PortableTextBlock[]; + slug: Slug; } diff --git a/studio/lib/queries/legalDocuments.ts b/studio/lib/queries/legalDocuments.ts index 6beeae488..a0aca6b31 100644 --- a/studio/lib/queries/legalDocuments.ts +++ b/studio/lib/queries/legalDocuments.ts @@ -1,9 +1,5 @@ import { groq } from "next-sanity"; -export const LEGAL_DOCUMENTS_QUERY = groq` - *[_type == "legalDocuments"] -`; +export const LEGAL_DOCUMENTS_BY_LANG_QUERY = groq`*[_type == "legalDocument" && language == $language]`; -export const LEGAL_DOCUMENT_SLUG_QUERY = groq` - *[_type == "legalDocuments" && slug.current == $slug][0] -`; +export const LEGAL_DOCUMENTS_BY_SLUG_AND_LANG_QUERY = groq`*[_type == "legalDocument" && language == $language && slug.current == $slug][0]`; diff --git a/studio/studioConfig.tsx b/studio/studioConfig.tsx index 887ae44ca..20f90e924 100644 --- a/studio/studioConfig.tsx +++ b/studio/studioConfig.tsx @@ -14,6 +14,8 @@ import { apiVersion, dataset, projectId } from "./env"; import { schema } from "./schema"; import { legalDocumentID } from "./schemas/documents/admin/legalDocuments"; +const SUPPORTED_LANGUAGES_QUERY = `*[_type == "languageSettings" && !(_id in path("drafts.*"))].languages[]{id, title}`; + const config: WorkspaceOptions = { name: "studio", title: "Studio", @@ -22,29 +24,16 @@ const config: WorkspaceOptions = { basePath: "/studio", projectId, dataset, - schema: { - ...schema, - templates: (prev) => prev.filter((template) => !template.value.language), - }, + schema, plugins: [ structureTool({ structure: deskStructure, }), visionTool({ defaultApiVersion: apiVersion }), documentInternationalization({ - // When async initial value templates are supported by the plugin, we can fetch dynamic languages like: - // (client) => { - // return client.fetch( - // `*[_type == "languageSettings"].languages[]{id, title}` - // ); - // } - // Currently, we have to use a static list of supported languages for both Norway and Sweden - // Limitation: Variant Norway and Varaint Sweden will not be able to filter out unsupported languages - supportedLanguages: [ - { id: "no", title: "Norwegian" }, - { id: "se", title: "Swedish" }, - { id: "en", title: "English" }, - ], + supportedLanguages: (client) => { + return client.fetch(SUPPORTED_LANGUAGES_QUERY); + }, schemaTypes: [legalDocumentID], languageField: languageID, apiVersion, @@ -53,15 +42,9 @@ const config: WorkspaceOptions = { // bulkPublish: true, }), internationalizedArray({ - languages: [ - { id: "no", title: "Norwegian" }, - { id: "se", title: "Swedish" }, - { id: "en", title: "English" }, - ], - // languages: (client) => - // client.fetch( - // `*[_type == "languageSettings"].languages[!default]{id, title}` - // ), + languages: (client) => { + return client.fetch(SUPPORTED_LANGUAGES_QUERY); + }, fieldTypes: ["string"], }), presentationTool({