Advertisement
djbob2000

Untitled

Feb 25th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import { apiPost } from "@/fd/fd-toolbox/api/api-client";
  2. import { ApiResponse } from "@/fd/fd-toolbox/api/api-response";
  3. import { FdTableComponent } from "@/meta/table/table-models";
  4. import { notify } from "@/fd/fd-toolbox/notifications";
  5. import { routes } from "@/fd/fd-toolbox/routing/routes";
  6. import { createAndDownloadCsv } from "@/lm/lead/actions/export-leads-action/create-and-download-csv";
  7. import { WithIdAndStringIndexer } from "@/fd/fd-toolbox/types/resource-with-id";
  8. import { leadProps } from "@/leads/props/lead-props";
  9. import { getUiResourceMeta } from "@/meta/ui-meta-providers";
  10. import { resourceNames } from "@/fd/fd-toolbox/resources/resource-names";
  11.  
  12. const notifications = {
  13. noItemsSelected: "Please select at least one item to export",
  14. exportCompleted: "Items were exported successfully",
  15. };
  16.  
  17. const leadEmailList = leadProps.leadEmailList;
  18.  
  19. export async function exportLeadInCSVFile(table: FdTableComponent | undefined) {
  20. if (table) {
  21. const tableSelectedRows = table.getSelectedRows();
  22.  
  23. if (!tableSelectedRows || tableSelectedRows.length === 0) {
  24. notify(notifications.noItemsSelected);
  25.  
  26. return;
  27. }
  28.  
  29. const uiResourceMeta = await getUiResourceMeta(resourceNames.lead);
  30.  
  31. const items = tableSelectedRows.map(({ item }: { item: WithIdAndStringIndexer }) => {
  32.  
  33. const processedItem = {
  34. ...item,
  35. ...(Array.isArray(item[leadEmailList])
  36. ? {
  37. [leadEmailList]: item[leadEmailList]
  38. .map((emailObj: { email: string }) => emailObj.email)
  39. .join(", "),
  40. }
  41. : {}),
  42. };
  43.  
  44. return Object.entries(processedItem).reduce<WithIdAndStringIndexer>(
  45. (acc, [propertyName, value]) => {
  46. const propertyMeta = uiResourceMeta.resourceMeta.properties[propertyName];
  47. const displayName = propertyMeta?.displayName || propertyName;
  48. acc[displayName] = value as string;
  49.  
  50. if (propertyName === "id") {
  51. acc.id = value as string;
  52. }
  53.  
  54. return acc;
  55. },
  56. { id: processedItem.id },
  57. );
  58. });
  59.  
  60. const response = await apiPost<ApiResponse<string>>(routes.export, {
  61. items: items,
  62. });
  63.  
  64. if (response && response.data) {
  65. createAndDownloadCsv(response.data);
  66. notify(notifications.exportCompleted);
  67. }
  68. }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement