A full-featured CRM built with React, shadcn/ui, and Supabase.
feat: Delete a tag
erwanMarmelab0e11d4fc1172
diff --git a/doc/src/content/docs/users/settings.mdx b/doc/src/content/docs/users/settings.mdxindex c95a0dfc..63cc973f 100644--- a/doc/src/content/docs/users/settings.mdx+++ b/doc/src/content/docs/users/settings.mdx@@ -26,4 +26,12 @@ The Settings page lets you customize:Once you save your changes, they will be immediately reflected across the application. For example, if you update the deal stages, the new stages will be available for users to drag and drop deals in the Kanban view, and they will also be available when creating or editing a deal.-When updating the name of an existing category (e.g., a deal stage), the new name will be applied to all existing records that belong to that category. For instance, if you rename the "Prospecting" deal stage to "Lead", all deals that were previously in the "Prospecting" stage will now be in the "Lead" stage.\ No newline at end of file+When updating the name of an existing category (e.g., a deal stage), the new name will be applied to all existing records that belong to that category. For instance, if you rename the "Prospecting" deal stage to "Lead", all deals that were previously in the "Prospecting" stage will now be in the "Lead" stage.+## Managing Tags++The Tags section lists all the tags used to label contacts. For each tag, you can:++- **Edit** it to change its name or color. The new name and color are immediately visible on all the contacts that have this tag.+- **Delete** it. The tag is removed from all the contacts that had it, without changing anything else on these contacts. You can undo the deletion for a few seconds using the "Undo" button of the notification.++Unlike the other settings, changes to tags are applied immediately: you don't need to click the Save button.diff --git a/registry.json b/registry.jsonindex 2f20432b..a1b38437 100644--- a/registry.json+++ b/registry.json@@ -145,6 +145,10 @@"path": "src/components/atomic-crm/simple-list/ListNoResults.tsx","type": "registry:component"},+ {+ "path": "src/components/atomic-crm/settings/SettingsTags.tsx",+ "type": "registry:component"+ },{"path": "src/components/atomic-crm/settings/SettingsPageMobile.tsx","type": "registry:component"diff --git a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts b/src/components/atomic-crm/providers/commons/englishCrmMessages.tsindex 15a8eabe..c4d56672 100644--- a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts+++ b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts@@ -357,6 +357,7 @@ export const englishCrmMessages = {name_label: "Tag name",name_placeholder: "Enter tag name",},+ empty: "No tags yet.",},},crm: {diff --git a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts b/src/components/atomic-crm/providers/commons/frenchCrmMessages.tsindex 20448cc8..95a7a6ad 100644--- a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts+++ b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts@@ -362,6 +362,7 @@ export const frenchCrmMessages = {name_label: "Nom de l'étiquette",name_placeholder: "Saisir le nom de l'étiquette",},+ empty: "Aucune étiquette pour le moment.",},},crm: {diff --git a/src/components/atomic-crm/providers/fakerest/dataProvider.test.ts b/src/components/atomic-crm/providers/fakerest/dataProvider.test.tsnew file mode 100644index 00000000..1def8843--- /dev/null+++ b/src/components/atomic-crm/providers/fakerest/dataProvider.test.ts@@ -0,0 +1,32 @@+import { describe, expect, it } from "vitest";++import type { Db } from "./dataGenerator/types";+import { createDataProvider } from "./dataProvider";++describe("fakerest dataProvider", () => {+ it("removes a deleted tag from contacts without touching other fields", async () => {+ const lastSeen = "2020-01-01T00:00:00.000Z";+ const dataProvider = createDataProvider({+ db: {+ tags: [+ { id: 1, name: "vip", color: "#fff" },+ { id: 2, name: "lead", color: "#000" },+ ],+ contacts: [+ { id: 1, first_name: "Ann", tags: [1, 2], last_seen: lastSeen },+ { id: 2, first_name: "Bob", tags: [2], last_seen: lastSeen },+ ],+ } as unknown as Db,+ latency: 0,+ silent: true,+ });++ await dataProvider.delete("tags", { id: 1 });++ const { data: contacts } = await dataProvider.getMany("contacts", {+ ids: [1, 2],+ });+ expect(contacts.map((c) => c.tags)).toEqual([[2], [2]]);+ expect(contacts.map((c) => c.last_seen)).toEqual([lastSeen, lastSeen]);+ });+});diff --git a/src/components/atomic-crm/providers/fakerest/dataProvider.ts b/src/components/atomic-crm/providers/fakerest/dataProvider.tsindex 4469fd47..55e41c5c 100644--- a/src/components/atomic-crm/providers/fakerest/dataProvider.ts+++ b/src/components/atomic-crm/providers/fakerest/dataProvider.ts@@ -17,6 +17,7 @@ import type {Sale,SalesFormData,SignUpData,+ Tag,Task,} from "../../types";import type { ConfigurationContextValue } from "../../root/ConfigurationContext";@@ -597,6 +598,31 @@ export const createDataProvider = ({return result;},} satisfies ResourceCallbacks<Deal>,+ {+ resource: "tags",+ afterDelete: async (result, dataProvider) => {+ // remove the deleted tag from all contacts, like the on_tag_deleted DB trigger+ const tagId = result.data.id;+ const { data: contacts } = await dataProvider.getList<Contact>(+ "contacts",+ {+ filter: { "tags@cs": `{${tagId}}` },+ pagination: { page: 1, perPage: 10_000 },+ sort: { field: "id", order: "ASC" },+ },+ );+ await Promise.all(+ contacts.map((contact) =>+ dataProvider.update("contacts", {+ id: contact.id,+ data: { tags: contact.tags.filter((id) => id !== tagId) },+ previousData: contact,+ }),+ ),+ );+ return result;+ },+ } satisfies ResourceCallbacks<Tag>,{resource: "contact_notes",beforeSave: async (params) => preserveAttachmentMimeType(params),diff --git a/src/components/atomic-crm/settings/SettingsPage.tsx b/src/components/atomic-crm/settings/SettingsPage.tsxindex d45f73a5..2ee4ba64 100644--- a/src/components/atomic-crm/settings/SettingsPage.tsx+++ b/src/components/atomic-crm/settings/SettingsPage.tsx@@ -27,6 +27,7 @@ import {type ConfigurationContextValue,} from "../root/ConfigurationContext";import { defaultConfiguration } from "../root/defaultConfiguration";+import { SettingsTags } from "./SettingsTags";const SECTIONS = [{@@ -42,6 +43,7 @@ const SECTIONS = [{ id: "deals", label: "resources.deals.name", fallback: "Deals" },{ id: "notes", label: "resources.notes.name", fallback: "Notes" },{ id: "tasks", label: "resources.tasks.name", fallback: "Tasks" },+ { id: "tags", label: "resources.tags.name", fallback: "Tags" },];/** Ensure every item in a { value, label } array has a value (slug from label). */@@ -462,6 +464,9 @@ const SettingsFormFields = () => {</ArrayInput></CardContent></Card>++ {/* Tags */}+ <SettingsTags /></div>{/* Sticky save button */}diff --git a/src/components/atomic-crm/settings/SettingsTags.tsx b/src/components/atomic-crm/settings/SettingsTags.tsxnew file mode 100644index 00000000..499f88cf--- /dev/null+++ b/src/components/atomic-crm/settings/SettingsTags.tsx@@ -0,0 +1,62 @@+import { Pencil } from "lucide-react";+import { RecordContextProvider, useTranslate } from "ra-core";+import { useState } from "react";+import { DeleteButton } from "@/components/admin/delete-button";+import { Button } from "@/components/ui/button";+import { Card, CardContent } from "@/components/ui/card";++import { TagEditModal } from "../tags/TagEditModal";+import { useTags } from "../tags/useTags";+import type { Tag } from "../types";++export const SettingsTags = () => {+ const translate = useTranslate();+ const { data: tags } = useTags();+ const [editedTag, setEditedTag] = useState<Tag>();++ return (+ <Card id="tags">+ <CardContent className="space-y-4">+ <h2 className="text-xl font-semibold text-muted-foreground">+ {translate("resources.tags.name", { smart_count: 2 })}+ </h2>+ {tags?.length === 0 && (+ <p className="text-sm text-muted-foreground">+ {translate("resources.tags.empty")}+ </p>+ )}+ {tags?.map((tag) => (+ <div key={tag.id} className="flex items-center gap-2">+ <span+ className="text-black px-2 py-1 text-sm rounded-md"+ style={{ backgroundColor: tag.color }}+ >+ {tag.name}+ </span>+ <div className="ml-auto flex gap-2">+ <Button+ type="button"+ variant="outline"+ size="sm"+ onClick={() => setEditedTag(tag)}+ >+ <Pencil />+ {translate("ra.action.edit")}+ </Button>+ <RecordContextProvider value={tag}>+ <DeleteButton resource="tags" redirect={false} size="sm" />+ </RecordContextProvider>+ </div>+ </div>+ ))}+ {editedTag && (+ <TagEditModal+ tag={editedTag}+ open+ onClose={() => setEditedTag(undefined)}+ />+ )}+ </CardContent>+ </Card>+ );+};diff --git a/src/components/atomic-crm/tags/TagForm.tsx b/src/components/atomic-crm/tags/TagForm.tsxindex 4dd8c7ab..5e8050bc 100644--- a/src/components/atomic-crm/tags/TagForm.tsx+++ b/src/components/atomic-crm/tags/TagForm.tsx@@ -36,6 +36,7 @@ export function TagForm({const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {event.preventDefault();+ event.stopPropagation();setIsSubmitting(true);try {diff --git a/supabase/migrations/20260924120000_remove_deleted_tag_from_contacts.sql b/supabase/migrations/20260924120000_remove_deleted_tag_from_contacts.sqlnew file mode 100644index 00000000..117ef05a--- /dev/null+++ b/supabase/migrations/20260924120000_remove_deleted_tag_from_contacts.sql@@ -0,0 +1,15 @@+set check_function_bodies = off;++CREATE OR REPLACE FUNCTION public.handle_tag_deleted()+ RETURNS trigger+ LANGUAGE plpgsql+ SET search_path TO ''+AS $function$+begin+ update public.contacts set tags = array_remove(tags, old.id) where tags @> array[old.id];+ return old;+end;+$function$+;++CREATE TRIGGER on_tag_deleted AFTER DELETE ON public.tags FOR EACH ROW EXECUTE FUNCTION public.handle_tag_deleted();diff --git a/supabase/schemas/02_functions.sql b/supabase/schemas/02_functions.sqlindex d5a0de63..9ae99ea0 100644--- a/supabase/schemas/02_functions.sql+++ b/supabase/schemas/02_functions.sql@@ -246,6 +246,16 @@ beginend;$$;+CREATE OR REPLACE FUNCTION "public"."handle_tag_deleted"() RETURNS "trigger"+ LANGUAGE "plpgsql"+ SET "search_path" TO ''+ AS $$+begin+ update public.contacts set tags = array_remove(tags, old.id) where tags @> array[old.id];+ return old;+end;+$$;+CREATE OR REPLACE FUNCTION "public"."handle_update_user"() RETURNS "trigger"LANGUAGE "plpgsql" SECURITY DEFINERSET "search_path" TO ''diff --git a/supabase/schemas/04_triggers.sql b/supabase/schemas/04_triggers.sqlindex 65009cab..9f0c54da 100644--- a/supabase/schemas/04_triggers.sql+++ b/supabase/schemas/04_triggers.sql@@ -70,6 +70,11 @@ create or replace trigger on_deal_notes_deleted_delete_note_attachmentsafter delete on public.deal_notesfor each row execute function public.cleanup_note_attachments();+-- Remove a deleted tag from every contact that references it+create or replace trigger on_tag_deleted+ after delete on public.tags+ for each row execute function public.handle_tag_deleted();+-- Auth triggers: sync auth.users to public.salescreate or replace trigger on_auth_user_createdafter insert on auth.users