Skip to content

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

Remove tag in bulk when all selected contacts already have it

fzaninotto01272bdd7c6b
diff --git a/src/components/atomic-crm/contacts/BulkTagButton.tsx b/src/components/atomic-crm/contacts/BulkTagButton.tsx
index 61c21693..ebaf3c01 100644
--- a/src/components/atomic-crm/contacts/BulkTagButton.tsx
+++ b/src/components/atomic-crm/contacts/BulkTagButton.tsx
@@ -61,9 +61,11 @@ export function BulkTagButton() {
const applyTagToSelection = useCallback(
async (tag: Tag) => {
- const contactsToUpdate = selectedContacts.filter(
- (contact) => !contact.tags.includes(tag.id),
+ const contactsToTag = selectedContacts.filter(
+ (contact) => !(contact.tags ?? []).includes(tag.id),
);
+ const isRemoving = contactsToTag.length === 0;
+ const contactsToUpdate = isRemoving ? selectedContacts : contactsToTag;
setIsApplying(true);
@@ -72,16 +74,20 @@ export function BulkTagButton() {
contactsToUpdate.map((contact) =>
update("contacts", {
id: contact.id,
- data: { tags: [...(contact.tags ?? []), tag.id] },
+ data: {
+ tags: isRemoving
+ ? contact.tags.filter((id) => id !== tag.id)
+ : [...(contact.tags ?? []), tag.id],
+ },
previousData: contact,
}),
),
);
notify(
- contactsToUpdate.length > 0
- ? "resources.contacts.bulk_tag.success"
- : "resources.contacts.bulk_tag.noop",
+ isRemoving
+ ? "resources.contacts.bulk_tag.removed"
+ : "resources.contacts.bulk_tag.success",
{
messageArgs: { smart_count: contactsToUpdate.length },
type: "success",
diff --git a/src/components/atomic-crm/contacts/ContactList.test.tsx b/src/components/atomic-crm/contacts/ContactList.test.tsx
index 8733b8bf..4671a70b 100644
--- a/src/components/atomic-crm/contacts/ContactList.test.tsx
+++ b/src/components/atomic-crm/contacts/ContactList.test.tsx
@@ -103,6 +103,37 @@ describe("ContactList", () => {
await screen.getByRole("button", { name: /close/i }).click();
});
+ it("removes a tag from selected contacts when they all already have it", async () => {
+ const screen = await render(<BulkTagButton />);
+
+ await expect
+ .poll(() => getSelectionCheckboxes(screen.container).length)
+ .toBe(2);
+
+ const checkboxes = getSelectionCheckboxes(screen.container);
+ await checkboxes[0].click();
+ await checkboxes[1].click();
+ await screen.getByRole("button", { name: /^tag$/i }).click();
+ await screen.getByRole("button", { name: "VIP" }).click();
+ await expect
+ .element(screen.getByText("Tag added to 1 contact"))
+ .toBeInTheDocument();
+ await screen.getByRole("button", { name: /close/i }).click();
+
+ await checkboxes[0].click();
+ await checkboxes[1].click();
+ await screen.getByRole("button", { name: /^tag$/i }).click();
+ await screen.getByRole("button", { name: "VIP" }).click();
+
+ await expect
+ .element(screen.getByText("Tag removed from 2 contacts"))
+ .toBeInTheDocument();
+ await expect
+ .poll(() => screen.getByRole("row").getByText("VIP").all().length)
+ .toBe(0);
+ await screen.getByRole("button", { name: /close/i }).click();
+ });
+
it("creates a new tag inline and applies it to the full selected list", async () => {
const screen = await render(<BulkTagButton />);
diff --git a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
index 02295356..7d7319bf 100644
--- a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
@@ -135,7 +135,8 @@ export const englishCrmMessages = {
"Choose an existing tag or create a new one for the selected contacts.",
empty: "No tags yet. Create one to tag the selected contacts.",
error: "Failed to add tag to contacts",
- noop: "Selected contacts already have this tag",
+ removed:
+ "Tag removed from %{smart_count} contact |||| Tag removed from %{smart_count} contacts",
success:
"Tag added to %{smart_count} contact |||| Tag added to %{smart_count} contacts",
title: "Add tag to contacts",
diff --git a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
index edfb7f2e..fadd37e7 100644
--- a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
@@ -139,7 +139,8 @@ export const frenchCrmMessages = {
empty:
"Aucune étiquette pour le moment. Créez-en une pour étiqueter les contacts sélectionnés.",
error: "Impossible d'ajouter l'étiquette aux contacts",
- noop: "Les contacts sélectionnés ont déjà cette étiquette",
+ removed:
+ "Étiquette retirée de %{smart_count} contact |||| Étiquette retirée de %{smart_count} contacts",
success:
"Étiquette ajoutée à %{smart_count} contact |||| Étiquette ajoutée à %{smart_count} contacts",
title: "Ajouter une étiquette aux contacts",