Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Don't allow adding an empty note

Anibal Svarcasf3b432b18af9
diff --git a/src/components/atomic-crm/notes/NoteInputs.test.tsx b/src/components/atomic-crm/notes/NoteInputs.test.tsx
index d3bc4e7b..cc87775a 100644
--- a/src/components/atomic-crm/notes/NoteInputs.test.tsx
+++ b/src/components/atomic-crm/notes/NoteInputs.test.tsx
@@ -1,8 +1,10 @@
import { render } from "vitest-browser-react";
import { CoreAdminContext, Form } from "ra-core";
import fakeDataProvider from "ra-data-fakerest";
+import { useFormContext } from "react-hook-form";
import { NoteInputs } from "./NoteInputs";
+import { SaveButton } from "@/components/admin/form";
import { defaultConfiguration } from "../root/defaultConfiguration";
vi.mock("../root/ConfigurationContext", () => ({
@@ -25,6 +27,9 @@ const testI18nProvider = {
"resources.notes.fields.status": "Status",
"resources.notes.fields.contact_id": "Contact",
"resources.notes.fields.deal_id": "Deal",
+ "resources.notes.validation.note_or_attachment_required":
+ "A note or an attachment is required",
+ "ra.action.save": "Save",
}) as Record<string, string>
)[key] ?? key,
changeLocale: () => Promise.resolve(),
@@ -43,6 +48,26 @@ const Wrapper = ({ children }: { children: React.ReactNode }) => (
<Form>{children}</Form>
</CoreAdminContext>
);
+
+const AttachmentSetter = () => {
+ const { setValue } = useFormContext();
+
+ return (
+ <button
+ type="button"
+ onClick={() => {
+ setValue(
+ "attachments",
+ [{ src: "blob:test", title: "evidence.pdf" }],
+ { shouldDirty: true, shouldValidate: true },
+ );
+ }}
+ >
+ Add attachment
+ </button>
+ );
+};
+
describe("NoteInputs", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -191,4 +216,61 @@ describe("NoteInputs", () => {
await expect(dateInput).toHaveValue("2024-01-01T12:00");
});
+
+ it("shows a validation error when submitting an empty note without attachments", async () => {
+ const screen = await render(
+ <>
+ <NoteInputs />
+ <SaveButton type="button" />
+ </>,
+ {
+ wrapper: Wrapper,
+ },
+ );
+
+ await screen.getByRole("button", { name: "Save" }).click();
+
+ await expect
+ .element(screen.getByText("A note or an attachment is required"))
+ .toBeVisible();
+ });
+
+ it("treats whitespace-only note text as empty", async () => {
+ const screen = await render(
+ <>
+ <NoteInputs />
+ <SaveButton type="button" />
+ </>,
+ {
+ wrapper: Wrapper,
+ },
+ );
+
+ await screen.getByPlaceholder("Add a note").fill(" ");
+ await screen.getByRole("button", { name: "Save" }).click();
+
+ await expect
+ .element(screen.getByText("A note or an attachment is required"))
+ .toBeVisible();
+ });
+
+ it("allows submitting a note with an attachment and no text", async () => {
+ const screen = await render(
+ <>
+ <NoteInputs />
+ <AttachmentSetter />
+ <SaveButton type="button" />
+ </>,
+ {
+ wrapper: Wrapper,
+ },
+ );
+
+ await screen.getByRole("button", { name: "Add attachment" }).click();
+ await screen.getByRole("button", { name: "Save" }).click();
+
+ await expect
+ .element(screen.getByText("A note or an attachment is required"))
+ .not.toBeInTheDocument();
+ });
});
diff --git a/src/components/atomic-crm/notes/NoteInputs.tsx b/src/components/atomic-crm/notes/NoteInputs.tsx
index 9f51201a..7dca1526 100644
--- a/src/components/atomic-crm/notes/NoteInputs.tsx
+++ b/src/components/atomic-crm/notes/NoteInputs.tsx
@@ -16,6 +16,24 @@ import { AutocompleteInput, ReferenceInput } from "@/components/admin";
import { contactOptionText } from "../misc/ContactOption";
import { useIsMobile } from "@/hooks/use-mobile";
+type NoteFormValues = {
+ attachments?: unknown[] | null;
+ text?: string | null;
+};
+
+const validateNoteOrAttachmentRequired = (
+ value: string | null | undefined,
+ values: NoteFormValues,
+) => {
+ const hasText = typeof value === "string" && value.trim().length > 0;
+ const hasAttachments =
+ Array.isArray(values?.attachments) && values.attachments.length > 0;
+
+ return hasText || hasAttachments
+ ? undefined
+ : "resources.notes.validation.note_or_attachment_required";
+};
+
export const NoteInputs = ({
showStatus,
selectReference,
@@ -42,6 +60,7 @@ export const NoteInputs = ({
helperText={false}
placeholder={translate("resources.notes.inputs.add_note")}
rows={6}
+ validate={validateNoteOrAttachmentRequired}
/>
{selectReference && reference && (
diff --git a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
index 71904a42..ed4c7d5c 100644
--- a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
@@ -275,6 +275,9 @@ export const englishCrmMessages = {
options_hint: "(attach files, or change details)",
show_options: "Show options",
},
+ validation: {
+ note_or_attachment_required: "A note or an attachment is required",
+ },
},
sales: {
name: "User |||| Users",
diff --git a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
index 26b16fc8..f8b8d108 100644
--- a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
@@ -280,6 +280,10 @@ export const frenchCrmMessages = {
options_hint: "(joindre des fichiers ou modifier les détails)",
show_options: "Afficher les options",
},
+ validation: {
+ note_or_attachment_required:
+ "Une note ou une pièce jointe est requise",
+ },
},
sales: {
name: "Utilisateur |||| Utilisateurs",