Skip to content

Commit

Permalink
fix(datagrid-web): fix not exporting dynamicText in xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmanunver authored and gjulivan committed Jan 19, 2024
1 parent 5a29566 commit 9d0235f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/pluggableWidgets/datagrid-web/src/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ function Container(props: Props): ReactElement {
const [state, actions] = useGridState(props.initParams, props.mappedColumns, props.onStateChange);

const [{ items, exporting, processedRows }, { abort }] = useDG2ExportApi({
columns: state.visibleColumns.map(column => props.columns[column.columnNumber]),
columns: useMemo(
() => state.visibleColumns.map(column => props.columns[column.columnNumber]),
[state.visibleColumns, props.columns]
),
hasMoreItems: props.datasource.hasMoreItems || false,
items: props.datasource.items,
name: props.name,
Expand Down
52 changes: 40 additions & 12 deletions packages/pluggableWidgets/datagrid-web/src/features/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,24 +219,49 @@ function exportColumns(columns: ColumnsType[]): Message {
return { type: "columns", payload: exportColumns };
}

function exportData(data: ObjectItem[], columns: ColumnsType[]): Message {
const items = data.map(item => {
return columns.map(column => {
let value = "";
type ExportDataResult =
| {
status: "pending";
}
| {
status: "ready";
message: Message;
};

function exportData(data: ObjectItem[], columns: ColumnsType[]): ExportDataResult {
const rows = [];
let hasLoadingItem = false;

for (const item of data) {
const row = [];
for (const column of columns) {
let value = "";
if (column.showContentAs === "attribute") {
value = column.attribute?.get(item)?.displayValue ?? "";
} else if (column.showContentAs === "dynamicText") {
value = column.dynamicText?.get(item)?.value ?? "";
const dynamicText = column.dynamicText?.get(item);
if (dynamicText?.status === "loading") {
hasLoadingItem = true;
} else if (dynamicText?.status === "unavailable") {
value = "n/a";
} else {
value = dynamicText?.value ?? "";
}
} else {
value = "n/a (custom content)";
}
row.push(value);
}
rows.push(row);
}

return value;
});
});
if (hasLoadingItem) {
return {
status: "pending"
};
}

return { type: "data", payload: items };
return { status: "ready", message: { type: "data", payload: rows } };
}

type CallbackFunction = (msg: Message) => Promise<void> | void;
Expand Down Expand Up @@ -397,7 +422,7 @@ function exportStateReducer(state: State, action: Action): State {
}

if (action.type === "ColumnsUpdate") {
if (state.phase === "readyToStart" || state.phase === "exportColumns") {
if (state.phase === "readyToStart" || state.phase === "exportColumns" || state.phase === "exportData") {
return {
...state,
columns: action.payload.columns
Expand Down Expand Up @@ -511,8 +536,11 @@ function createExportFlow(): [ExportFlowFn, ExportFlowCleanup] {
if (state.phase === "exportData") {
const { currentItems, callback, columns } = state;
if (currentItems.length > 0) {
await controller.exec(() => callback(exportData(currentItems, columns)));
controller.exec(() => dispatch({ type: "PageExported" }));
const result = exportData(currentItems, columns);
if (result.status === "ready") {
await controller.exec(() => callback(result.message));
controller.exec(() => dispatch({ type: "PageExported" }));
}
}
return;
}
Expand Down

0 comments on commit 9d0235f

Please sign in to comment.