Advertisement
djbob2000

Untitled

Feb 26th, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. import { WithIdAndStringIndexer, withIdProps } from "@/fd/fd-toolbox/types/resource-with-id";
  2. import { metaQueryParams } from "@/fd/fd-toolbox/constants/meta-query-params";
  3. import { apiPost } from "@/fd/fd-toolbox/api/api-client";
  4. import { ApiResponse } from "@/fd/fd-toolbox/api/api-response";
  5. import { notify } from "@/fd/fd-toolbox/notifications";
  6. import { routes } from "@/fd/fd-toolbox/routing/routes";
  7. import { FdTableComponent } from "@/meta/table/table-models";
  8. import { getUiResourceMeta } from "@/meta/ui-meta-providers";
  9. import { isString, isObject } from "@/fd/fd-toolbox/types/ensure-type";
  10. import { WithIndexer } from "@/fd/fd-toolbox/types/with-indexer";
  11.  
  12. interface EmailObject {
  13. email: string;
  14. }
  15.  
  16. interface UiResourceMeta {
  17. resourceMeta: {
  18. properties: WithIndexer<{ displayName?: string }>;
  19. };
  20. }
  21.  
  22. const notifications = {
  23. noItemsSelected: "Please select at least one item to export",
  24. exportCompleted: "Items were exported successfully",
  25. };
  26.  
  27. const csvType = "text/csv";
  28. const defaultFileName = "Export";
  29. const emailProperty = "email";
  30.  
  31. export function createAndDownloadCsv(data: string, fileName?: string) {
  32. const blob = new Blob([data], { type: csvType });
  33. const url = URL.createObjectURL(blob);
  34. const downloadLink = document.createElement("a");
  35. downloadLink.href = url;
  36. downloadLink.download =
  37. fileName ||
  38. new URL(window.location.href).searchParams.get(metaQueryParams.resource) ||
  39. defaultFileName;
  40. document.body.appendChild(downloadLink);
  41. downloadLink.click();
  42. downloadLink.remove();
  43. }
  44.  
  45. function isEmailObject(obj: unknown): obj is EmailObject {
  46. return isObject(obj) && emailProperty in obj;
  47. }
  48.  
  49. function processArrayValue(value: unknown[]) {
  50. if (value.length > 0 && isObject(value[0]) && emailProperty in value[0]) {
  51. return value
  52. .map((obj) => {
  53. if (isEmailObject(obj)) {
  54. return obj.email;
  55. }
  56.  
  57. return String(obj);
  58. })
  59. .join(", ");
  60. }
  61.  
  62. return value.join(", ");
  63. }
  64.  
  65. function processArrayProperties(item: WithIdAndStringIndexer) {
  66. const processedItem: WithIdAndStringIndexer = { id: item.id || "" };
  67.  
  68. for (const [key, value] of Object.entries(item)) {
  69. if (Array.isArray(value)) {
  70. processedItem[key] = processArrayValue(value);
  71. } else {
  72. processedItem[key] = value;
  73. }
  74. }
  75.  
  76. return processedItem;
  77. }
  78.  
  79. function formatItemForExport(item: WithIdAndStringIndexer, uiResourceMeta: UiResourceMeta) {
  80. const processedItem = processArrayProperties(item);
  81. const result: WithIdAndStringIndexer = { id: "" };
  82. const idValue = isString(processedItem.id) ? processedItem.id : String(processedItem.id ?? "");
  83. let idDisplayName = withIdProps.id;
  84.  
  85. for (const [propertyName, value] of Object.entries(processedItem)) {
  86. const propertyMeta = uiResourceMeta.resourceMeta.properties[propertyName];
  87. const displayName = propertyMeta?.displayName || propertyName;
  88.  
  89. if (propertyName === withIdProps.id) {
  90. idDisplayName = displayName;
  91. continue;
  92. }
  93.  
  94. result[displayName] = String(value ?? "");
  95. }
  96.  
  97. result[idDisplayName] = idValue;
  98. return result;
  99. }
  100.  
  101. export async function exportItemsInCSVFile(table: FdTableComponent | undefined, resourceName: string) {
  102. if (!table) {
  103. return;
  104. }
  105.  
  106. const tableSelectedRows = table.getSelectedRows();
  107.  
  108. if (!tableSelectedRows || tableSelectedRows.length === 0) {
  109. notify(notifications.noItemsSelected);
  110. return;
  111. }
  112.  
  113. const uiResourceMeta = await getUiResourceMeta(resourceName);
  114. const items = tableSelectedRows.map(({ item }) => formatItemForExport(item, uiResourceMeta));
  115.  
  116. const response = await apiPost<ApiResponse<string>, { items: WithIdAndStringIndexer[] }>(routes.export, {
  117. items,
  118. });
  119.  
  120. if (response && response.data) {
  121. createAndDownloadCsv(response.data);
  122. notify(notifications.exportCompleted);
  123. }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement