Skip to content

Commit

Permalink
[aksel.nav.no] Validate unique title in exampletext_block (#3024)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalvorHaugan committed Jun 25, 2024
1 parent c2ffd46 commit 6276b67
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import ShowMore from "@/web/ShowMore";

type ExampletextBlockProps = {
node: {
title: string;
text: string;
title?: string;
text?: string;
readMore?: boolean;
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -34,6 +65,8 @@ export const ExampletextBlock = defineType({
},
},
components: {
preview: (values) => <AkselExampletextBlock node={values as any} />,
preview: (values: Omit<ExampletextBlockT, "_key">) => (
<AkselExampletextBlock node={values} />
),
},
});

0 comments on commit 6276b67

Please sign in to comment.