Skip to content

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

Refine bulk tag follow-up

Anibal Svarcasf4c43d06d377
diff --git a/e2e/bulkContactTags.spec.ts b/e2e/bulkContactTags.spec.ts
new file mode 100644
index 00000000..b662c6a4
--- /dev/null
+++ b/e2e/bulkContactTags.spec.ts
@@ -0,0 +1,53 @@
+import { test, expect } from "./fixtures";
+
+test("user adds a tag to several contacts", async ({
+ page,
+ isMobile,
+ createContact,
+ createSales,
+ menu,
+ dismissToast,
+}) => {
+ test.skip(isMobile, "Bulk tag is only available on desktop");
+
+ const sales = await createSales({
+ email: "[email protected]",
+ first_name: "John",
+ last_name: "Doe",
+ password: "password",
+ });
+
+ await createContact({
+ first_name: "Ada",
+ last_name: "Lovelace",
+ sales_id: sales.id,
+ });
+ await createContact({
+ first_name: "Grace",
+ last_name: "Hopper",
+ sales_id: sales.id,
+ });
+
+ await page.goto("http://localhost:5175/");
+
+ await page.getByLabel("Email").fill("[email protected]");
+ await page.getByLabel("Password").fill("password");
+ await page.getByRole("button", { name: "Sign in" }).click();
+
+ await expect(page.getByText("Latest Activity")).toBeVisible();
+
+ await menu.goToContacts();
+
+ const checkboxes = page.getByRole("checkbox");
+ await checkboxes.nth(1).click();
+ await checkboxes.nth(2).click();
+
+ await page.getByRole("button", { name: /^Tag$/ }).click();
+ await page.getByRole("button", { name: "Create new tag" }).click();
+ await page.getByLabel("Tag name").fill("Prospect");
+ await page.getByRole("button", { name: "Save" }).click();
+
+ await dismissToast("Tag added to 2 contacts");
+
+ await expect(page.getByText("Prospect")).toHaveCount(2);
+});
diff --git a/src/components/atomic-crm/contacts/BulkTagButton.tsx b/src/components/atomic-crm/contacts/BulkTagButton.tsx
index 506ad088..8be2ed4f 100644
--- a/src/components/atomic-crm/contacts/BulkTagButton.tsx
+++ b/src/components/atomic-crm/contacts/BulkTagButton.tsx
@@ -2,7 +2,6 @@ import { Plus, Tag as TagIcon } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import {
useDataProvider,
- useGetList,
useGetMany,
useListContext,
useNotify,
@@ -20,6 +19,8 @@ import {
} from "@/components/ui/dialog";
import { TagForm } from "../tags/TagForm";
+import { useCreateTag } from "../tags/useCreateTag";
+import { useTags } from "../tags/useTags";
import type { Contact, Tag } from "../types";
type BulkTagDialogMode = "select" | "create";
@@ -29,6 +30,7 @@ export function BulkTagButton() {
const notify = useNotify();
const refresh = useRefresh();
const dataProvider = useDataProvider();
+ const createTag = useCreateTag();
const { onUnselectItems, selectedIds = [] } = useListContext<Contact>();
const [open, setOpen] = useState(false);
const [mode, setMode] = useState<BulkTagDialogMode>("select");
@@ -40,14 +42,9 @@ export function BulkTagButton() {
{ ids: selectedIds },
{ enabled: open && selectedIds.length > 0 },
);
- const { data: tags = [], isPending: isPendingTags } = useGetList<Tag>(
- "tags",
- {
- pagination: { page: 1, perPage: 1000 },
- sort: { field: "name", order: "ASC" },
- },
- { enabled: open },
- );
+ const { data: tags = [], isPending: isPendingTags } = useTags({
+ enabled: open,
+ });
const closeDialog = useCallback(() => {
setOpen(false);
@@ -113,8 +110,8 @@ export function BulkTagButton() {
);
const handleCreateTag = async (data: Pick<Tag, "name" | "color">) => {
- const response = await dataProvider.create<Tag>("tags", { data });
- await applyTagToSelection(response.data);
+ const tag = await createTag(data);
+ await applyTagToSelection(tag);
};
if (!selectedIds.length) {
diff --git a/src/components/atomic-crm/contacts/TagsListEdit.tsx b/src/components/atomic-crm/contacts/TagsListEdit.tsx
index d6c1ddf2..d5dd513c 100644
--- a/src/components/atomic-crm/contacts/TagsListEdit.tsx
+++ b/src/components/atomic-crm/contacts/TagsListEdit.tsx
@@ -1,6 +1,5 @@
import { Edit, Plus } from "lucide-react";
import {
- useGetList,
useGetMany,
useRecordContext,
useTranslate,
@@ -19,6 +18,7 @@ import {
import { TagChip } from "../tags/TagChip";
import { TagCreateModal } from "../tags/TagCreateModal";
+import { useTags } from "../tags/useTags";
import type { Contact, Tag } from "../types";
export const TagsListEdit = () => {
@@ -26,13 +26,9 @@ export const TagsListEdit = () => {
const [open, setOpen] = useState(false);
const translate = useTranslate();
- const { data: allTags, isPending: isPendingAllTags } = useGetList<Tag>(
- "tags",
- {
- pagination: { page: 1, perPage: 10 },
- sort: { field: "name", order: "ASC" },
- },
- );
+ const { data: allTags, isPending: isPendingAllTags } = useTags({
+ perPage: 10,
+ });
const { data: tags, isPending: isPendingRecordTags } = useGetMany<Tag>(
"tags",
{ ids: record?.tags },
diff --git a/src/components/atomic-crm/tags/TagCreateModal.tsx b/src/components/atomic-crm/tags/TagCreateModal.tsx
index 4cff9f5d..469dc342 100644
--- a/src/components/atomic-crm/tags/TagCreateModal.tsx
+++ b/src/components/atomic-crm/tags/TagCreateModal.tsx
@@ -1,7 +1,8 @@
-import { useCreate, useTranslate } from "ra-core";
+import { useTranslate } from "ra-core";
import type { Tag } from "../types";
import { TagDialog } from "./TagDialog";
+import { useCreateTag } from "./useCreateTag";
type TagCreateModalProps = {
open: boolean;
@@ -14,19 +15,12 @@ export function TagCreateModal({
onClose,
onSuccess,
}: TagCreateModalProps) {
- const [create] = useCreate<Tag>();
+ const createTag = useCreateTag();
const translate = useTranslate();
const handleCreateTag = async (data: Pick<Tag, "name" | "color">) => {
- await create(
- "tags",
- { data },
- {
- onSuccess: async (tag) => {
- await onSuccess?.(tag);
- },
- },
- );
+ const tag = await createTag(data);
+ await onSuccess?.(tag);
};
return (
diff --git a/src/components/atomic-crm/tags/useCreateTag.ts b/src/components/atomic-crm/tags/useCreateTag.ts
new file mode 100644
index 00000000..357e820b
--- /dev/null
+++ b/src/components/atomic-crm/tags/useCreateTag.ts
@@ -0,0 +1,16 @@
+import { useCallback } from "react";
+import { useDataProvider } from "ra-core";
+
+import type { Tag } from "../types";
+
+export function useCreateTag() {
+ const dataProvider = useDataProvider();
+
+ return useCallback(
+ async (data: Pick<Tag, "name" | "color">) => {
+ const response = await dataProvider.create<Tag>("tags", { data });
+ return response.data;
+ },
+ [dataProvider],
+ );
+}
diff --git a/src/components/atomic-crm/tags/useTags.ts b/src/components/atomic-crm/tags/useTags.ts
new file mode 100644
index 00000000..3580f4bd
--- /dev/null
+++ b/src/components/atomic-crm/tags/useTags.ts
@@ -0,0 +1,19 @@
+import { useGetList } from "ra-core";
+
+import type { Tag } from "../types";
+
+type UseTagsOptions = {
+ enabled?: boolean;
+ perPage?: number;
+};
+
+export function useTags({ enabled, perPage = 1000 }: UseTagsOptions = {}) {
+ return useGetList<Tag>(
+ "tags",
+ {
+ pagination: { page: 1, perPage },
+ sort: { field: "name", order: "ASC" },
+ },
+ { enabled },
+ );
+}
diff --git a/vitest.config.ts b/vitest.config.ts
index 883562e1..903994d4 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -5,18 +5,6 @@ import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
- optimizeDeps: {
- include: [
- "@storybook/react-vite",
- "faker/locale/en",
- "faker/locale/en_US",
- "jsonexport/dist",
- "lodash",
- "papaparse",
- "ra-supabase-language-english",
- ],
- exclude: ["playwright", "playwright-core"],
- },
test: {
globals: true,
browser: {
@@ -46,6 +34,9 @@ export default defineConfig({
},
},
},
+ optimizeDeps: {
+ exclude: ["playwright", "playwright-core"],
+ },
resolve: {
preserveSymlinks: true,
alias: {