Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { apiPost } from "@/fd/fd-toolbox/api/api-client";
- import { ApiResponse } from "@/fd/fd-toolbox/api/api-response";
- import { FdTableComponent } from "@/meta/table/table-models";
- import { notify } from "@/fd/fd-toolbox/notifications";
- import { routes } from "@/fd/fd-toolbox/routing/routes";
- import { createAndDownloadCsv } from "@/lm/lead/actions/export-leads-action/create-and-download-csv";
- import { WithIdAndStringIndexer } from "@/fd/fd-toolbox/types/resource-with-id";
- import { leadProps } from "@/leads/props/lead-props";
- import { getUiResourceMeta } from "@/meta/ui-meta-providers";
- import { resourceNames } from "@/fd/fd-toolbox/resources/resource-names";
- import { isString } from "@/fd/fd-toolbox/types/ensure-type";
- const notifications = {
- noItemsSelected: "Please select at least one item to export",
- exportCompleted: "Items were exported successfully",
- };
- const leadEmailList = leadProps.leadEmailList;
- export async function exportLeadInCSVFile(table: FdTableComponent | undefined) {
- if (!table) return;
- const tableSelectedRows = table.getSelectedRows();
- if (!tableSelectedRows?.length) {
- notify(notifications.noItemsSelected);
- return;
- }
- const uiResourceMeta = await getUiResourceMeta(resourceNames.lead);
- const items = tableSelectedRows.map(({ item }: { item: WithIdAndStringIndexer }) => {
- const processedItem = {
- ...item,
- ...(Array.isArray(item[leadEmailList])
- ? {
- [leadEmailList]: item[leadEmailList]
- .map((emailObj: { email: string }) => emailObj.email)
- .join(", "),
- }
- : {}),
- };
- const result: WithIdAndStringIndexer = {
- id: isString(processedItem.id) ? processedItem.id : String(processedItem.id ?? ""),
- };
- for (const [propertyName, value] of Object.entries(processedItem)) {
- const propertyMeta = uiResourceMeta.resourceMeta.properties[propertyName];
- const displayName = propertyMeta?.displayName || propertyName;
- result[displayName] = String(value ?? "");
- }
- return result;
- });
- const response = await apiPost<ApiResponse<string>>(routes.export, { items });
- if (response?.data) {
- createAndDownloadCsv(response.data);
- notify(notifications.exportCompleted);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement