Skip to content

Commit 4ade83f

Browse files
authored
fix: contacts refresh button (formbricks#7066)
1 parent f1fc9fe commit 4ade83f

3 files changed

Lines changed: 35 additions & 31 deletions

File tree

apps/web/modules/ee/contacts/components/contact-data-view.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { debounce } from "lodash";
44
import dynamic from "next/dynamic";
5-
import { useEffect, useMemo, useRef, useState } from "react";
5+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
66
import toast from "react-hot-toast";
77
import { TContactAttributeKey } from "@formbricks/types/contact-attribute-key";
88
import { TEnvironment } from "@formbricks/types/environment";
@@ -48,40 +48,40 @@ export const ContactDataView = ({
4848
);
4949
}, [contactAttributeKeys]);
5050

51+
// Fetch contacts from offset 0 with current search value
52+
const fetchContactsFromStart = useCallback(async () => {
53+
setIsDataLoaded(false);
54+
try {
55+
setHasMore(true);
56+
const contactsResponse = await getContactsAction({
57+
environmentId: environment.id,
58+
offset: 0,
59+
searchValue,
60+
});
61+
if (contactsResponse?.data) {
62+
setContacts(contactsResponse.data);
63+
}
64+
if (contactsResponse?.data && contactsResponse.data.length < itemsPerPage) {
65+
setHasMore(false);
66+
}
67+
} catch (error) {
68+
console.error("Error fetching contacts:", error);
69+
toast.error("Error fetching contacts. Please try again.");
70+
} finally {
71+
setIsDataLoaded(true);
72+
}
73+
}, [environment.id, itemsPerPage, searchValue]);
74+
5175
useEffect(() => {
5276
if (!isFirstRender.current) {
53-
const fetchData = async () => {
54-
setIsDataLoaded(false);
55-
try {
56-
setHasMore(true);
57-
const getPersonActionData = await getContactsAction({
58-
environmentId: environment.id,
59-
offset: 0,
60-
searchValue,
61-
});
62-
const personData = getPersonActionData?.data;
63-
if (getPersonActionData?.data) {
64-
setContacts(getPersonActionData.data);
65-
}
66-
if (personData && personData.length < itemsPerPage) {
67-
setHasMore(false);
68-
}
69-
} catch (error) {
70-
console.error("Error fetching people data:", error);
71-
toast.error("Error fetching people data. Please try again.");
72-
} finally {
73-
setIsDataLoaded(true);
74-
}
75-
};
76-
77-
const debouncedFetchData = debounce(fetchData, 300);
77+
const debouncedFetchData = debounce(fetchContactsFromStart, 300);
7878
debouncedFetchData();
7979

8080
return () => {
8181
debouncedFetchData.cancel();
8282
};
8383
}
84-
}, [environment.id, itemsPerPage, searchValue]);
84+
}, [fetchContactsFromStart]);
8585

8686
useEffect(() => {
8787
if (isFirstRender.current) {
@@ -147,6 +147,7 @@ export const ContactDataView = ({
147147
setSearchValue={setSearchValue}
148148
isReadOnly={isReadOnly}
149149
isQuotasAllowed={isQuotasAllowed}
150+
refreshContacts={fetchContactsFromStart}
150151
/>
151152
);
152153
};

apps/web/modules/ee/contacts/components/contacts-table.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface ContactsTableProps {
4343
setSearchValue: (value: string) => void;
4444
isReadOnly: boolean;
4545
isQuotasAllowed: boolean;
46+
refreshContacts: () => Promise<void>;
4647
}
4748

4849
export const ContactsTable = ({
@@ -56,6 +57,7 @@ export const ContactsTable = ({
5657
setSearchValue,
5758
isReadOnly,
5859
isQuotasAllowed,
60+
refreshContacts,
5961
}: ContactsTableProps) => {
6062
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
6163
const [columnOrder, setColumnOrder] = useState<string[]>([]);
@@ -235,6 +237,7 @@ export const ContactsTable = ({
235237
type="contact"
236238
deleteAction={deleteContact}
237239
isQuotasAllowed={isQuotasAllowed}
240+
onRefresh={refreshContacts}
238241
leftContent={
239242
<div className="w-64">
240243
<SearchBar

apps/web/modules/ui/components/data-table/components/data-table-toolbar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { Table } from "@tanstack/react-table";
44
import { MoveVerticalIcon, RefreshCcwIcon, SettingsIcon } from "lucide-react";
5-
import { useRouter } from "next/navigation";
65
import toast from "react-hot-toast";
76
import { useTranslation } from "react-i18next";
87
import { cn } from "@/lib/cn";
@@ -20,6 +19,7 @@ interface DataTableToolbarProps<T> {
2019
downloadRowsAction?: (rowIds: string[], format: string) => Promise<void>;
2120
isQuotasAllowed: boolean;
2221
leftContent?: React.ReactNode;
22+
onRefresh?: () => Promise<void>;
2323
}
2424

2525
export const DataTableToolbar = <T,>({
@@ -33,9 +33,9 @@ export const DataTableToolbar = <T,>({
3333
downloadRowsAction,
3434
isQuotasAllowed,
3535
leftContent,
36+
onRefresh,
3637
}: DataTableToolbarProps<T>) => {
3738
const { t } = useTranslation();
38-
const router = useRouter();
3939

4040
return (
4141
<div className="sticky top-0 z-30 flex w-full items-center justify-between bg-slate-50 py-2">
@@ -52,13 +52,13 @@ export const DataTableToolbar = <T,>({
5252
<div>{leftContent}</div>
5353
)}
5454
<div className="flex space-x-2">
55-
{type === "contact" ? (
55+
{type === "contact" && onRefresh ? (
5656
<TooltipRenderer
5757
tooltipContent={t("environments.contacts.contacts_table_refresh")}
5858
shouldRender={true}>
5959
<button
6060
onClick={async () => {
61-
router.refresh();
61+
await onRefresh();
6262
toast.success(t("environments.contacts.contacts_table_refresh_success"));
6363
}}
6464
className="cursor-pointer rounded-md border bg-white hover:border-slate-400">

0 commit comments

Comments
 (0)