From 056bd638694b6bd788780e5d95333e9e89f8ef97 Mon Sep 17 00:00:00 2001 From: Kartik sharma Date: Fri, 20 Jun 2025 17:12:50 +0530 Subject: [PATCH] Adding Export button in Directory. --- src/pages/Directory/Directory.jsx | 1 + src/pages/Directory/DirectoryPageHeader.jsx | 123 +++++++++++++++++--- 2 files changed, 105 insertions(+), 19 deletions(-) diff --git a/src/pages/Directory/Directory.jsx b/src/pages/Directory/Directory.jsx index eb348e10..fe013661 100644 --- a/src/pages/Directory/Directory.jsx +++ b/src/pages/Directory/Directory.jsx @@ -345,6 +345,7 @@ const Directory = ({ IsPage = true, prefernceContacts }) => { loading={loading} IsActive={IsActive} setOpenBucketModal={setOpenBucketModal} + contactsToExport={contacts} /> diff --git a/src/pages/Directory/DirectoryPageHeader.jsx b/src/pages/Directory/DirectoryPageHeader.jsx index 7de25693..cec3147a 100644 --- a/src/pages/Directory/DirectoryPageHeader.jsx +++ b/src/pages/Directory/DirectoryPageHeader.jsx @@ -1,4 +1,6 @@ -import React, { useEffect, useState } from "react"; +import React, { useEffect, useState, useRef } from "react"; +import { ITEMS_PER_PAGE } from "../../utils/constants"; +import { exportToCSV, exportToExcel, printTable, exportToPDF } from "../../utils/tableExportUtils"; const DirectoryPageHeader = ({ searchText, @@ -17,33 +19,82 @@ const DirectoryPageHeader = ({ loading, IsActive, setOpenBucketModal, + contactsToExport, // This prop receives the paginated data (currentItems) }) => { - const [filtered, setFiltered] = useState(); + const [filtered, setFiltered] = useState(0); + + const handleExport = (type) => { + // Check if there's data to export + if (!contactsToExport || contactsToExport.length === 0) { + console.warn("No data to export. The current view is empty."); + // Optionally, you might want to show a user-friendly toast message here + // showToast("No data to export on the current page.", "info"); + return; + } + + // --- Core Change: Map contactsToExport to a simplified format --- + // const simplifiedContacts = contactsToExport.map(contact => ({ + // Name: contact.name || '', + // Organization: contact.organization || '', // Added Organization + // Email: contact.contactEmails && contact.contactEmails.length > 0 ? contact.contactEmails[0].emailAddress : '', + // Phone: contact.contactPhones && contact.contactPhones.length > 0 ? contact.contactPhones[0].phoneNumber : '', // Changed 'Contact' to 'Phone' for clarity + // Category: contact.contactCategory ? contact.contactCategory.name : '', // Changed 'Role' to 'Category' + // })); + + const simplifiedContacts = contactsToExport.map(contact => ({ + Name: contact.name || '', + Organization: contact.organization || '', + Email: contact.contactEmails && contact.contactEmails.length > 0 + ? contact.contactEmails.map(email => email.emailAddress).join(', ') + : '', + Phone: contact.contactPhones && contact.contactPhones.length > 0 + ? contact.contactPhones.map(phone => phone.phoneNumber).join(', ') + : '', + Category: contact.contactCategory ? contact.contactCategory.name : '', + })); + + console.log("Kaerik", simplifiedContacts) + switch (type) { + case "csv": + exportToCSV(simplifiedContacts, "directory_contacts"); + break; + case "excel": + exportToExcel(simplifiedContacts, "directory_contacts"); + break; + case "pdf": + exportToPDF(simplifiedContacts, "directory_contacts"); + break; + case "print": + printTable(simplifiedContacts, "directory_contacts"); + break; + default: + break; + } + }; useEffect(() => { setFiltered( tempSelectedBucketIds?.length + tempSelectedCategoryIds?.length ); }, [tempSelectedBucketIds, tempSelectedCategoryIds]); + return ( <> - {/*
vikas
*/}
setSearchText(e.target.value)} style={{ width: "200px" }} /> -
+
-
-
); }; -export default DirectoryPageHeader; +export default DirectoryPageHeader; \ No newline at end of file -- 2.43.0