A full-featured CRM built with React, shadcn/ui, and Supabase.
refactor(settings): move validation helper to avoid refresh warning
Anibal Svarcasd85a82bb543e
diff --git a/src/components/atomic-crm/settings/SettingsPage.test.ts b/src/components/atomic-crm/settings/SettingsPage.test.tsindex 712a3f42..53e1e2cb 100644--- a/src/components/atomic-crm/settings/SettingsPage.test.ts+++ b/src/components/atomic-crm/settings/SettingsPage.test.ts@@ -1,6 +1,6 @@import { describe, it, expect } from "vitest";import type { RaRecord } from "ra-core";-import { validateItemsInUse } from "./SettingsPage";+import { validateItemsInUse } from "./validateItemsInUse";describe("validateItemsInUse", () => {const deals: RaRecord[] = [diff --git a/src/components/atomic-crm/settings/SettingsPage.tsx b/src/components/atomic-crm/settings/SettingsPage.tsxindex a6039700..dda8faf6 100644--- a/src/components/atomic-crm/settings/SettingsPage.tsx+++ b/src/components/atomic-crm/settings/SettingsPage.tsx@@ -1,5 +1,4 @@import { RotateCcw, Save } from "lucide-react";-import type { RaRecord } from "ra-core";import {EditBase,Form,@@ -25,6 +24,7 @@ import {type ConfigurationContextValue,} from "../root/ConfigurationContext";import { defaultConfiguration } from "../root/defaultConfiguration";+import { validateItemsInUse } from "./validateItemsInUse";const SECTIONS = [{@@ -46,47 +46,6 @@ const SECTIONS = [const ensureValues = (items: { value?: string; label: string }[] | undefined) =>items?.map((item) => ({ ...item, value: item.value || toSlug(item.label) }));-/**- * Validate that no items were removed if they are still referenced by existing deals.- * Also rejects duplicate slug values.- * Returns undefined if valid, or an error message string.- */-export const validateItemsInUse = (- items: { value: string; label: string }[] | undefined,- deals: RaRecord[] | undefined,- fieldName: string,- displayName: string,-) => {- if (!items) return undefined;- // Check for duplicate slugs- const slugs = items.map((i) => i.value || toSlug(i.label));- const seen = new Set<string>();- const duplicates = new Set<string>();- for (const slug of slugs) {- if (seen.has(slug)) duplicates.add(slug);- seen.add(slug);- }- if (duplicates.size > 0) {- return `Duplicate ${displayName}: ${[...duplicates].join(", ")}`;- }- // Check that no in-use value was removed (skip if deals haven't loaded)- if (!deals) return "Validating…";- const values = new Set(slugs);- const inUse = [- ...new Set(- deals- .filter(- (deal) => deal[fieldName] && !values.has(deal[fieldName] as string),- )- .map((deal) => deal[fieldName] as string),- ),- ];- if (inUse.length > 0) {- return `Cannot remove ${displayName} that are still used by deals: ${inUse.join(", ")}`;- }- return undefined;-};-const transformFormValues = (data: Record<string, any>) => ({config: {title: data.title,diff --git a/src/components/atomic-crm/settings/validateItemsInUse.ts b/src/components/atomic-crm/settings/validateItemsInUse.tsnew file mode 100644index 00000000..3a87ab27--- /dev/null+++ b/src/components/atomic-crm/settings/validateItemsInUse.ts@@ -0,0 +1,43 @@+import type { RaRecord } from "ra-core";+import { toSlug } from "@/lib/toSlug";++/**+ * Validate that no items were removed if they are still referenced by existing deals.+ * Also rejects duplicate slug values.+ * Returns undefined if valid, or an error message string.+ */+export const validateItemsInUse = (+ items: { value: string; label: string }[] | undefined,+ deals: RaRecord[] | undefined,+ fieldName: string,+ displayName: string,+) => {+ if (!items) return undefined;+ // Check for duplicate slugs+ const slugs = items.map((i) => i.value || toSlug(i.label));+ const seen = new Set<string>();+ const duplicates = new Set<string>();+ for (const slug of slugs) {+ if (seen.has(slug)) duplicates.add(slug);+ seen.add(slug);+ }+ if (duplicates.size > 0) {+ return `Duplicate ${displayName}: ${[...duplicates].join(", ")}`;+ }+ // Check that no in-use value was removed (skip if deals haven't loaded)+ if (!deals) return "Validating…";+ const values = new Set(slugs);+ const inUse = [+ ...new Set(+ deals+ .filter(+ (deal) => deal[fieldName] && !values.has(deal[fieldName] as string),+ )+ .map((deal) => deal[fieldName] as string),+ ),+ ];+ if (inUse.length > 0) {+ return `Cannot remove ${displayName} that are still used by deals: ${inUse.join(", ")}`;+ }+ return undefined;+};