Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { WithIdAndStringIndexer, withIdProps } from "@/fd/fd-toolbox/types/resource-with-id";
- import { metaQueryParams } from "@/fd/fd-toolbox/constants/meta-query-params";
- import { apiPost } from "@/fd/fd-toolbox/api/api-client";
- import { ApiResponse } from "@/fd/fd-toolbox/api/api-response";
- import { notify } from "@/fd/fd-toolbox/notifications";
- import { routes } from "@/fd/fd-toolbox/routing/routes";
- import { FdTableComponent } from "@/meta/table/table-models";
- import { getUiResourceMeta } from "@/meta/ui-meta-providers";
- import { isString, isObject } from "@/fd/fd-toolbox/types/ensure-type";
- import { WithIndexer } from "@/fd/fd-toolbox/types/with-indexer";
- interface EmailObject {
- email: string;
- }
- interface UiResourceMeta {
- resourceMeta: {
- properties: WithIndexer<{ displayName?: string }>;
- };
- }
- const notifications = {
- noItemsSelected: "Please select at least one item to export",
- exportCompleted: "Items were exported successfully",
- };
- const csvType = "text/csv";
- const defaultFileName = "Export";
- const emailProperty = "email";
- export function createAndDownloadCsv(data: string, fileName?: string) {
- const blob = new Blob([data], { type: csvType });
- const url = URL.createObjectURL(blob);
- const downloadLink = document.createElement("a");
- downloadLink.href = url;
- downloadLink.download =
- fileName ||
- new URL(window.location.href).searchParams.get(metaQueryParams.resource) ||
- defaultFileName;
- document.body.appendChild(downloadLink);
- downloadLink.click();
- downloadLink.remove();
- }
- function isEmailObject(obj: unknown): obj is EmailObject {
- return isObject(obj) && emailProperty in obj;
- }
- function processArrayValue(value: unknown[]) {
- if (value.length > 0 && isObject(value[0]) && emailProperty in value[0]) {
- return value
- .map((obj) => {
- if (isEmailObject(obj)) {
- return obj.email;
- }
- return String(obj);
- })
- .join(", ");
- }
- return value.join(", ");
- }
- function processArrayProperties(item: WithIdAndStringIndexer) {
- const processedItem: WithIdAndStringIndexer = { id: item.id || "" };
- for (const [key, value] of Object.entries(item)) {
- if (Array.isArray(value)) {
- processedItem[key] = processArrayValue(value);
- } else {
- processedItem[key] = value;
- }
- }
- return processedItem;
- }
- function formatItemForExport(item: WithIdAndStringIndexer, uiResourceMeta: UiResourceMeta) {
- const processedItem = processArrayProperties(item);
- const result: WithIdAndStringIndexer = { id: "" };
- const idValue = isString(processedItem.id) ? processedItem.id : String(processedItem.id ?? "");
- let idDisplayName = withIdProps.id;
- for (const [propertyName, value] of Object.entries(processedItem)) {
- const propertyMeta = uiResourceMeta.resourceMeta.properties[propertyName];
- const displayName = propertyMeta?.displayName || propertyName;
- if (propertyName === withIdProps.id) {
- idDisplayName = displayName;
- continue;
- }
- result[displayName] = String(value ?? "");
- }
- result[idDisplayName] = idValue;
- return result;
- }
- export async function exportItemsInCSVFile(table: FdTableComponent | undefined, resourceName: string) {
- if (!table) {
- return;
- }
- const tableSelectedRows = table.getSelectedRows();
- if (!tableSelectedRows || tableSelectedRows.length === 0) {
- notify(notifications.noItemsSelected);
- return;
- }
- const uiResourceMeta = await getUiResourceMeta(resourceName);
- const items = tableSelectedRows.map(({ item }) => formatItemForExport(item, uiResourceMeta));
- const response = await apiPost<ApiResponse<string>, { items: WithIdAndStringIndexer[] }>(routes.export, {
- items,
- });
- if (response && response.data) {
- createAndDownloadCsv(response.data);
- notify(notifications.exportCompleted);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement