Skip to content

Commit

Permalink
Merge pull request #3848 from simson1/fix-table-card
Browse files Browse the repository at this point in the history
fix(tablecard): file download timestamp mismatch from ui
  • Loading branch information
kodiakhq[bot] authored Mar 18, 2024
2 parents 04e4965 + b45d2b2 commit edec480
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
42 changes: 25 additions & 17 deletions packages/react/src/components/TableCard/TableCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ const TableCard = ({
.filter((i) => !isNil(i)),
[columns]
);

const updatedTableData = useMemo(() => {
return tableData.map((row) => {
const { values } = row;
Object.keys(values).forEach((column) => {
if (filteredTimestampColumns.includes(column) && !isEditable) {
values[column] = values[column]
? dayjs(values[column]).format(defaultDateFormatPattern)
: '';
}
});
return {
...row,
values,
};
});
}, [defaultDateFormatPattern, filteredTimestampColumns, isEditable, tableData]);

const columnsToRender = useMemo(
() =>
newColumns
Expand All @@ -320,19 +338,13 @@ const TableCard = ({
{ value } // default render function is to handle timestamp
) =>
// if it's a timestamp column type make sure to format it
filteredTimestampColumns.includes(i.dataSourceId) && !isEditable
? dayjs(value).format(defaultDateFormatPattern)
: isNil(value)
? ''
: value.toString(),
isNil(value) ? '' : value.toString(),
}))
.concat(hasActionColumn ? actionColumn : []),
[
actionColumn,
defaultDateFormatPattern,
filteredTimestampColumns,
hasActionColumn,
isEditable,
mergedI18n.defaultFilterStringPlaceholdText,
newColumns,
newSize,
Expand Down Expand Up @@ -377,7 +389,7 @@ const TableCard = ({
isEditable
? generateTableSampleValues(id, columns)
: hasActionColumn || filteredPrecisionColumns.length || thresholds
? tableData.map((i) => {
? updatedTableData.map((i) => {
// if has custom action
const action = hasActionColumn ? { actionColumn: i.actions || [] } : null;

Expand Down Expand Up @@ -426,15 +438,15 @@ const TableCard = ({
isSelectable: false,
};
})
: tableData,
: updatedTableData,
[
isEditable,
id,
columns,
hasActionColumn,
filteredPrecisionColumns,
thresholds,
tableData,
updatedTableData,
thresholdSeverityLabelsMap,
locale,
]
Expand All @@ -444,7 +456,7 @@ const TableCard = ({
const expandedRowsFormatted = useMemo(
() =>
expandedRows && expandedRows.length
? tableData.map((dataItem) => {
? updatedTableData.map((dataItem) => {
// filter the data keys and find the expandaded row exist for that key
const expandedItem = Object.keys(dataItem.values)
.map((value) => expandedRows.filter((item) => item.id === value)[0])
Expand Down Expand Up @@ -489,11 +501,7 @@ const TableCard = ({
{item?.label ? item.label : '--'}
</p>
<span key={`${item.id}-value`}>
{item
? item.type === 'TIMESTAMP'
? dayjs(dataItem.values[item.id]).format(defaultDateFormatPattern)
: dataItem.values[item.id]
: null}
{item ? dataItem.values[item.id] : null}
</span>
</>
)}
Expand All @@ -513,7 +521,7 @@ const TableCard = ({
};
})
: [],
[defaultDateFormatPattern, expandedRows, others.cardVariables, tableData]
[expandedRows, others.cardVariables, updatedTableData]
);

// is columns recieved is different from the columnsToRender show card expand
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/TableCard/TableCard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('TableCard', () => {
row: {
alert: 'AHI005 Asset failure',
count: 1.2039201932,
hour: 1563877570000,
hour: '07/23/2019 05:26',
long_description: 'long description for a given event payload',
pressure: 0,
},
Expand Down Expand Up @@ -1008,7 +1008,7 @@ describe('TableCard', () => {

userEvent.click(screen.getByRole('button', { name: /download/i }));
expect(fileDownload).toHaveBeenCalledWith(
`alert,count,hour,long_description,pressure\nAHI005 Asset failure,1.2039201932,1563877570000,long description for a given event payload,0\nAHI003 process need to optimize adjust X variables,1.10329291,1563873970000,long description for a given event payload,2\n`,
`alert,count,hour,long_description,pressure\nAHI005 Asset failure,1.2039201932,07/23/2019 05:26,long description for a given event payload,0\nAHI003 process need to optimize adjust X variables,1.10329291,07/23/2019 04:26,long description for a given event payload,2\n`,
'Open Alerts.csv'
);
});
Expand Down

0 comments on commit edec480

Please sign in to comment.