From 6276b676d6411d9d0cdeee368341d8481190fad6 Mon Sep 17 00:00:00 2001 From: Halvor Haugan <83693529+HalvorHaugan@users.noreply.github.com> Date: Tue, 25 Jun 2024 09:52:07 +0200 Subject: [PATCH] [aksel.nav.no] Validate unique title in exampletext_block (#3024) --- .../exampletext-block/ExampletextBlock.tsx | 4 +- .../schema/objects/shared/expansion-card.tsx | 9 +++++ .../objects/templates/ExampletextBlock.tsx | 37 ++++++++++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/aksel.nav.no/website/components/sanity-modules/exampletext-block/ExampletextBlock.tsx b/aksel.nav.no/website/components/sanity-modules/exampletext-block/ExampletextBlock.tsx index 4d9361da9b..f5acac4816 100644 --- a/aksel.nav.no/website/components/sanity-modules/exampletext-block/ExampletextBlock.tsx +++ b/aksel.nav.no/website/components/sanity-modules/exampletext-block/ExampletextBlock.tsx @@ -5,8 +5,8 @@ import ShowMore from "@/web/ShowMore"; type ExampletextBlockProps = { node: { - title: string; - text: string; + title?: string; + text?: string; readMore?: boolean; }; }; diff --git a/aksel.nav.no/website/sanity/schema/objects/shared/expansion-card.tsx b/aksel.nav.no/website/sanity/schema/objects/shared/expansion-card.tsx index b8796fc762..e3fcc1e451 100644 --- a/aksel.nav.no/website/sanity/schema/objects/shared/expansion-card.tsx +++ b/aksel.nav.no/website/sanity/schema/objects/shared/expansion-card.tsx @@ -1,6 +1,15 @@ import { defineField, defineType } from "sanity"; import { ChevronDownIcon } from "@navikt/aksel-icons"; +export type ExpansionCardT = { + _key: string; + _type: "expansioncard"; + heading: string; + heading_level: "h3" | "h4"; + description?: string; + body: any[]; +}; + export const ExpansionCard = defineType({ name: "expansioncard", title: "ExpansionCard", diff --git a/aksel.nav.no/website/sanity/schema/objects/templates/ExampletextBlock.tsx b/aksel.nav.no/website/sanity/schema/objects/templates/ExampletextBlock.tsx index acbd68153e..87a0ad6cc8 100644 --- a/aksel.nav.no/website/sanity/schema/objects/templates/ExampletextBlock.tsx +++ b/aksel.nav.no/website/sanity/schema/objects/templates/ExampletextBlock.tsx @@ -1,6 +1,17 @@ import { defineField, defineType } from "sanity"; import { Chat2Icon } from "@navikt/aksel-icons"; import AkselExampletextBlock from "@/cms/exampletext-block/ExampletextBlock"; +import { ExpansionCardT } from "../shared/expansion-card"; + +type ExampletextBlockT = { + _key: string; + _type: "exampletext_block"; + title?: string; + text?: string; + readMore?: boolean; +}; + +type ContentTypesWeCareAbout = ExampletextBlockT | ExpansionCardT; export const ExampletextBlock = defineType({ name: "exampletext_block", @@ -13,7 +24,27 @@ export const ExampletextBlock = defineType({ name: "title", type: "string", initialValue: "Eksempeltekst", - validation: (Rule) => Rule.required(), + validation: (Rule) => + Rule.required().custom((value, context) => { + if (!context.document) return true; + const content = context.document.content as ContentTypesWeCareAbout[]; + let blocksWithThisTitle = 0; + + content.forEach((block) => { + if (block._type === "exampletext_block" && block.title === value) { + blocksWithThisTitle++; + } else if (block._type === "expansioncard") { + blocksWithThisTitle += block.body + .filter((subBlock) => subBlock._type === "exampletext_block") + .filter((subBlock) => subBlock.title === value).length; + } + }); + + if (blocksWithThisTitle > 1) { + return "Tittelen må være unik på tvers av alle eksempeltekst-blokkene."; + } + return true; + }), }), defineField({ title: "Tekst", @@ -34,6 +65,8 @@ export const ExampletextBlock = defineType({ }, }, components: { - preview: (values) => , + preview: (values: Omit) => ( + + ), }, });