Update export data action to work with new table component

This commit is contained in:
Andrew Kingston 2024-04-08 16:17:22 +01:00
parent e1a9762d21
commit 53bbaac751
5 changed files with 74 additions and 49 deletions

View File

@ -49,26 +49,34 @@
}, },
] ]
$: tables = findAllMatchingComponents($selectedScreen?.props, component => $: components = findAllMatchingComponents(
component._component.endsWith("table")
)
$: tableBlocks = findAllMatchingComponents(
$selectedScreen?.props, $selectedScreen?.props,
component => component._component.endsWith("tableblock") component => {
const type = component._component
return (
type.endsWith("/table") ||
type.endsWith("/tableblock") ||
type.endsWith("/gridblock")
) )
$: components = tables.concat(tableBlocks) }
$: componentOptions = components.map(table => ({ )
label: table._instanceName, $: componentOptions = components.map(component => {
value: table._component.includes("tableblock") let value = component._id
? `${table._id}-table` if (component._component.endsWith("/tableblock")) {
: table._id, value = `${component._id}-table`
})) }
return {
label: component._instanceName,
value,
}
})
$: selectedTableId = parameters.tableComponentId?.includes("-") $: selectedTableId = parameters.tableComponentId?.includes("-")
? parameters.tableComponentId.split("-")[0] ? parameters.tableComponentId.split("-")[0]
: parameters.tableComponentId : parameters.tableComponentId
$: selectedTable = components.find( $: selectedTable = components.find(
component => component._id === selectedTableId component => component._id === selectedTableId
) )
$: parameters.rows = `{{ literal [${parameters.tableComponentId}].[selectedRows] }}`
onMount(() => { onMount(() => {
if (!parameters.type) { if (!parameters.type) {

View File

@ -36,12 +36,10 @@
let grid let grid
let gridContext let gridContext
$: columnWhitelist = parsedColumns $: parsedColumns = getParsedColumns(columns)
?.filter(col => col.active) $: columnWhitelist = parsedColumns.filter(x => x.active).map(x => x.field)
?.map(col => col.field)
$: schemaOverrides = getSchemaOverrides(parsedColumns) $: schemaOverrides = getSchemaOverrides(parsedColumns)
$: enrichedButtons = enrichButtons(buttons) $: enrichedButtons = enrichButtons(buttons)
$: parsedColumns = getParsedColumns(columns)
$: selectedRows = deriveSelectedRows(gridContext) $: selectedRows = deriveSelectedRows(gridContext)
$: data = { selectedRows: $selectedRows } $: data = { selectedRows: $selectedRows }
$: actions = [ $: actions = [
@ -67,12 +65,14 @@
// Parses columns to fix older formats // Parses columns to fix older formats
const getParsedColumns = columns => { const getParsedColumns = columns => {
if (!columns?.length) {
return []
}
// If the first element has an active key all elements should be in the new format // If the first element has an active key all elements should be in the new format
if (columns?.length && columns[0]?.active !== undefined) { if (columns[0].active !== undefined) {
return columns return columns
} }
return columns.map(column => ({
return columns?.map(column => ({
label: column.displayName || column.name, label: column.displayName || column.name,
field: column.name, field: column.name,
active: true, active: true,
@ -81,7 +81,7 @@
const getSchemaOverrides = columns => { const getSchemaOverrides = columns => {
let overrides = {} let overrides = {}
columns?.forEach(column => { columns.forEach(column => {
overrides[column.field] = { overrides[column.field] = {
displayName: column.label, displayName: column.label,
} }
@ -115,11 +115,10 @@
return derived( return derived(
[gridContext.selectedRows, gridContext.rowLookupMap, gridContext.rows], [gridContext.selectedRows, gridContext.rowLookupMap, gridContext.rows],
([$selectedRows, $rowLookupMap, $rows]) => { ([$selectedRows, $rowLookupMap, $rows]) => {
const rowIds = Object.entries($selectedRows || {}) return Object.entries($selectedRows || {})
.filter(([_, selected]) => selected) .filter(([_, selected]) => selected)
.map(([rowId]) => rowId) .map(([rowId]) => {
return rowIds.map(id => { const idx = $rowLookupMap[rowId]
const idx = $rowLookupMap[id]
return gridContext.rows.actions.cleanRow($rows[idx]) return gridContext.rows.actions.cleanRow($rows[idx])
}) })
} }

View File

@ -15,7 +15,7 @@ const createRowSelectionStore = () => {
const componentId = Object.keys(selection).find( const componentId = Object.keys(selection).find(
componentId => componentId === tableComponentId componentId => componentId === tableComponentId
) )
return selection[componentId] || {} return selection[componentId]
} }
return { return {

View File

@ -332,31 +332,49 @@ const s3UploadHandler = async action => {
} }
const exportDataHandler = async action => { const exportDataHandler = async action => {
let selection = rowSelectionStore.actions.getSelection( let { tableComponentId, rows, type, columns, delimiter, customHeaders } =
action.parameters.tableComponentId action.parameters
// Handle legacy configs using the row selection store
if (!rows?.length) {
const selection = rowSelectionStore.actions.getSelection(tableComponentId)
if (selection?.rows?.length) {
rows = selection.selectedRows
}
}
// Get table ID from first row
const tableId = rows?.[0]?.tableId
// Handle no rows selected
if (!rows?.length) {
notificationStore.actions.error("Please select at least one row")
}
// Handle case where we're not using a DS+
else if (!tableId) {
notificationStore.actions.error(
"Exporting data only works for tables and views"
) )
console.log(selection) }
if (selection.selectedRows && selection.selectedRows.length > 0) { // Happy path when we have both rows and table ID
else {
try { try {
// Flatten rows if required
if (typeof rows[0] !== "string") {
rows = rows.map(row => row._id)
}
const data = await API.exportRows({ const data = await API.exportRows({
tableId: selection.tableId, tableId,
rows: selection.selectedRows, rows,
format: action.parameters.type, format: type,
columns: action.parameters.columns?.map( columns: columns?.map(column => column.name || column),
column => column.name || column delimiter,
), customHeaders,
delimiter: action.parameters.delimiter,
customHeaders: action.parameters.customHeaders,
}) })
download( download(new Blob([data], { type: "text/plain" }), `${tableId}.${type}`)
new Blob([data], { type: "text/plain" }),
`${selection.tableId}.${action.parameters.type}`
)
} catch (error) { } catch (error) {
notificationStore.actions.error("There was an error exporting the data") notificationStore.actions.error("There was an error exporting the data")
} }
} else {
notificationStore.actions.error("Please select at least one row")
} }
} }

View File

@ -113,9 +113,9 @@ export const createActions = context => {
// Callback when leaving the grid, deselecting all focussed or selected items // Callback when leaving the grid, deselecting all focussed or selected items
const blur = () => { const blur = () => {
focusedCellId.set(null) // focusedCellId.set(null)
selectedRows.set({}) // selectedRows.set({})
hoveredRowId.set(null) // hoveredRowId.set(null)
} }
return { return {