From e179a267aaf7dd92226261fbacadc68ea123a9ff Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Mon, 15 Sep 2025 13:05:03 +0530 Subject: [PATCH] Directory - Contacts Exported to Excel does not format properly --- src/pages/Directory/ContactsPage.jsx | 99 +++++++++++++++------------ src/pages/Directory/DirectoryPage.jsx | 37 +++++++--- src/pages/Directory/NotesPage.jsx | 39 +++++++++-- src/utils/exportUtils.js | 27 ++++++++ 4 files changed, 145 insertions(+), 57 deletions(-) create mode 100644 src/utils/exportUtils.js diff --git a/src/pages/Directory/ContactsPage.jsx b/src/pages/Directory/ContactsPage.jsx index 6dd0dfc0..d3e84e6e 100644 --- a/src/pages/Directory/ContactsPage.jsx +++ b/src/pages/Directory/ContactsPage.jsx @@ -11,8 +11,21 @@ import Pagination from "../../components/common/Pagination"; import ListViewContact from "../../components/Directory/ListViewContact"; import { CardViewContactSkeleton, ListViewContactSkeleton } from "../../components/Directory/DirectoryPageSkeleton"; +// Utility function to format contacts for CSV export +const formatExportData = (contacts) => { + return contacts.map(contact => ({ + Email: contact.contactEmails?.map(e => e.emailAddress).join(", ") || "", + Phone: contact.contactPhones?.map(p => p.phoneNumber).join(", ") || "", + Created: contact.createdAt ? new Date(contact.createdAt).toLocaleString() : "", + Location: contact.address || "", + Organization: contact.organization || "", + Category: contact.contactCategory?.name || "", + Tags: contact.tags?.map(t => t.name).join(", ") || "", + Buckets: contact.bucketIds?.join(", ") || "", + })); +}; -const ContactsPage = ({projectId ,searchText }) => { +const ContactsPage = ({ projectId, searchText, onExport }) => { const [currentPage, setCurrentPage] = useState(1); const [filters, setFilter] = useState(defaultContactFilter); const debouncedSearch = useDebounce(searchText, 500); @@ -26,9 +39,9 @@ const ContactsPage = ({projectId ,searchText }) => { debouncedSearch ); const { setOffcanvasContent, setShowTrigger } = useFab(); - const clearFilter = () => { - setFilter(defaultContactFilter); - }; + + const clearFilter = () => setFilter(defaultContactFilter); + useEffect(() => { setShowTrigger(true); setOffcanvasContent( @@ -42,6 +55,13 @@ const ContactsPage = ({projectId ,searchText }) => { }; }, []); + // 🔹 Format contacts for export + useEffect(() => { + if (data?.data && onExport) { + onExport(formatExportData(data.data)); + } + }, [data?.data]); + const paginate = (page) => { if (page >= 1 && page <= (data?.totalPages ?? 1)) { setCurrentPage(page); @@ -49,49 +69,44 @@ const ContactsPage = ({projectId ,searchText }) => { }; if (isError) return
{error.message}
; - if (isLoading) return ; - return ( -
- {gridView ? ( - <> - {data?.data?.map((contact) => ( -
- -
- ))} + if (isLoading) return gridView ? : ; - {data?.data?.length > 0 && ( -
- + {gridView ? ( + <> + {data?.data?.map((contact) => ( +
+ +
+ ))} + + {data?.data?.length > 0 && ( +
+ +
+ )} + + ) : ( +
+ + } />
)} - - ) : ( -
- - } - />
- )} -
- - - -) + ); }; export default ContactsPage; diff --git a/src/pages/Directory/DirectoryPage.jsx b/src/pages/Directory/DirectoryPage.jsx index e70c79b4..56e702ce 100644 --- a/src/pages/Directory/DirectoryPage.jsx +++ b/src/pages/Directory/DirectoryPage.jsx @@ -15,6 +15,7 @@ import BucketList from "../../components/Directory/BucketList"; import { MainDirectoryPageSkeleton } from "../../components/Directory/DirectoryPageSkeleton"; import ContactProfile from "../../components/Directory/ContactProfile"; import GlobalModel from "../../components/common/GlobalModel"; +import { exportToCSV } from "../../utils/exportUtils"; const NotesPage = lazy(() => import("./NotesPage")); const ContactsPage = lazy(() => import("./ContactsPage")); @@ -32,7 +33,7 @@ export const useDirectoryContext = () => { } return context; }; -export default function DirectoryPage({ IsPage = true, projectId=null }) { +export default function DirectoryPage({ IsPage = true, projectId = null }) { const [searchContact, setsearchContact] = useState(""); const [searchNote, setSearchNote] = useState(""); const [activeTab, setActiveTab] = useState("notes"); @@ -49,6 +50,18 @@ export default function DirectoryPage({ IsPage = true, projectId=null }) { Open: false, }); + const [notesData, setNotesData] = useState([]); + const [ContactData, setContactData] = useState([]); + + const handleExport = (type) => { + if (activeTab === "notes" && type === "csv") { + exportToCSV(notesData, "notes.csv"); + } + if (activeTab === "contacts" && type === "csv") { + exportToCSV(ContactData, "contact.csv"); + } + }; + const { data, isLoading, isError, error } = useBucketList(); const handleTabClick = (tab, e) => { @@ -94,8 +107,8 @@ export default function DirectoryPage({ IsPage = true, projectId=null }) { return ( <> -
- {IsPage && ( + {IsPage && ( handleTabClick("notes", e)} > @@ -119,7 +132,7 @@ export default function DirectoryPage({ IsPage = true, projectId=null }) { handleTabClick("contacts", e)} > @@ -139,7 +152,11 @@ export default function DirectoryPage({ IsPage = true, projectId=null }) {
  • - + {/* */} + handleExport("csv")} + > CSV
  • @@ -173,7 +190,7 @@ export default function DirectoryPage({ IsPage = true, projectId=null }) {