Skip to content

Commit e88d5f1

Browse files
authored
Merge pull request #814 from amitamrutiya/fix-table-date
fix: catalog table date and pagination issue
2 parents 860b404 + 6d44e65 commit e88d5f1

File tree

2 files changed

+108
-57
lines changed

2 files changed

+108
-57
lines changed

src/custom/CatalogDesignTable/CatalogDesignTable.tsx

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import _ from 'lodash';
3-
import { useEffect, useRef, useState } from 'react';
3+
import { useCallback, useMemo, useRef } from 'react';
44
import { PublishIcon } from '../../icons';
55
import { CHARCOAL, useTheme } from '../../theme';
66
import { Pattern } from '../CustomCatalog/CustomCard';
@@ -47,28 +47,54 @@ export const CatalogDesignsTable: React.FC<CatalogDesignsTableProps> = ({
4747
handleBulkDeleteModal,
4848
handleBulkpatternsDataUnpublishModal
4949
}) => {
50-
const [tableCols, updateCols] = useState<Array<any>>([]);
5150
const { width } = useWindowDimensions();
5251
const smallScreen = width <= 360;
5352
const theme = useTheme();
5453
const modalRef = useRef<PromptRef>(null);
5554

56-
useEffect(() => {
57-
if (Array.isArray(columns) && columns.length > 0) {
58-
updateCols(columns);
59-
}
60-
}, [columns]);
55+
const formatDate = useCallback((date: string | Date): string => {
56+
const dateOptions: Intl.DateTimeFormatOptions = {
57+
weekday: 'short',
58+
day: 'numeric',
59+
month: 'long',
60+
year: 'numeric'
61+
};
62+
return new Date(date).toLocaleDateString('en-US', dateOptions);
63+
}, []);
64+
65+
const processedColumns = useMemo(() => {
66+
return columns.map((col) => {
67+
const newCol = { ...col };
68+
if (!newCol.options) newCol.options = {};
69+
newCol.options.display = columnVisibility[col.name];
70+
if (
71+
[
72+
'updated_at',
73+
'created_at',
74+
'deleted_at',
75+
'last_login_time',
76+
'joined_at',
77+
'last_run',
78+
'next_run'
79+
].includes(col.name)
80+
) {
81+
newCol.options.customBodyRender = (value: any) => {
82+
if (!value || value === 'NA') return <>NA</>;
83+
if (typeof value === 'object' && 'Valid' in value) {
84+
if (value.Valid && value.Time) {
85+
return <>{formatDate(value.Time)}</>;
86+
}
87+
return <>NA</>;
88+
}
89+
return <>{formatDate(value)}</>;
90+
};
91+
}
92+
return newCol;
93+
});
94+
}, [columns, columnVisibility, formatDate]);
6195

62-
const options: any = {
63-
selectableRows: _.isNil(filter) ? 'none' : 'multiple',
64-
serverSide: true,
65-
filterType: 'multiselect',
66-
responsive: smallScreen ? 'vertical' : 'standard',
67-
count: totalCount,
68-
rowsPerPage: pageSize,
69-
page,
70-
elevation: 0,
71-
onTableChange: (action: string, tableState: any) => {
96+
const handleTableChange = useCallback(
97+
(action: string, tableState: any) => {
7298
const sortInfo = tableState.announceText ? tableState.announceText.split(' : ') : [];
7399
let order = '';
74100
if (tableState.activeColumn) {
@@ -98,44 +124,71 @@ export const CatalogDesignsTable: React.FC<CatalogDesignsTableProps> = ({
98124
}
99125
break;
100126
}
101-
}
102-
};
127+
},
128+
[columns, setPage, setPageSize, setSortOrder, sortOrder]
129+
);
103130

104-
if (_.isNil(filter)) {
105-
options.customToolbarSelect = (selected: any) => (
106-
<UnpublishTooltipIcon
107-
title="Unpublish"
108-
onClick={() => handleBulkpatternsDataUnpublishModal(selected, patterns, modalRef)}
109-
iconType="publish"
110-
id={'unpublish-button'}
111-
>
112-
<PublishIcon width={28.8} height={28.8} fill={CHARCOAL} />
113-
</UnpublishTooltipIcon>
114-
);
115-
} else {
116-
options.onRowsDelete = (rowsDeleted: any) => {
117-
const selectedPatterns = rowsDeleted.data.map(({ dataIndex }: any) => patterns[dataIndex]);
118-
handleBulkDeleteModal(selectedPatterns, modalRef);
119-
return false;
120-
};
121-
}
131+
const options = useMemo(
132+
() => ({
133+
selectableRows: _.isNil(filter) ? 'none' : 'multiple',
134+
serverSide: true,
135+
filterType: 'multiselect',
136+
responsive: smallScreen ? 'vertical' : 'standard',
137+
count: totalCount,
138+
rowsPerPage: pageSize,
139+
page,
140+
elevation: 0,
141+
onTableChange: handleTableChange,
142+
customToolbarSelect: _.isNil(filter)
143+
? (selected: any) => (
144+
<UnpublishTooltipIcon
145+
title="Unpublish"
146+
onClick={() => handleBulkpatternsDataUnpublishModal(selected, patterns, modalRef)}
147+
iconType="publish"
148+
id={'unpublish-button'}
149+
>
150+
<PublishIcon width={28.8} height={28.8} fill={CHARCOAL} />
151+
</UnpublishTooltipIcon>
152+
)
153+
: undefined,
154+
onRowsDelete: !_.isNil(filter)
155+
? (rowsDeleted: any) => {
156+
const selectedPatterns = rowsDeleted.data.map(
157+
({ dataIndex }: any) => patterns[dataIndex]
158+
);
159+
handleBulkDeleteModal(selectedPatterns, modalRef);
160+
return false;
161+
}
162+
: undefined
163+
}),
164+
[
165+
filter,
166+
smallScreen,
167+
totalCount,
168+
pageSize,
169+
page,
170+
handleTableChange,
171+
patterns,
172+
handleBulkDeleteModal,
173+
handleBulkpatternsDataUnpublishModal
174+
]
175+
);
122176

123-
if (!Array.isArray(tableCols) || tableCols.length === 0) {
177+
if (!processedColumns.length) {
124178
return null;
125179
}
126180

127181
return (
128182
<>
129183
<PromptComponent ref={modalRef} />
130184
<ResponsiveDataTable
131-
columns={columns}
185+
columns={processedColumns}
132186
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
133187
//@ts-ignore
134188
data={patterns || []}
135189
options={options}
136190
colViews={colViews}
137-
tableCols={tableCols}
138-
updateCols={updateCols}
191+
tableCols={processedColumns}
139192
columnVisibility={columnVisibility}
140193
backgroundColor={
141194
theme.palette.mode === 'light'

src/custom/TooltipIcon.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,21 @@ export function TooltipIcon({
2020
}: TooltipIconProps): JSX.Element {
2121
return (
2222
<CustomTooltip title={title} arrow={arrow}>
23-
<div>
24-
<IconButton
25-
onClick={onClick}
26-
sx={{
27-
'&:hover': {
28-
'& svg': {
29-
fill: '#00d3a9'
30-
},
31-
borderRadius: '4px'
23+
<IconButton
24+
onClick={onClick}
25+
sx={{
26+
'&:hover': {
27+
'& svg': {
28+
fill: '#00d3a9'
3229
},
33-
...(style as SxProps)
34-
}}
35-
disableRipple
36-
>
37-
{icon}
38-
</IconButton>
39-
</div>
30+
borderRadius: '4px'
31+
},
32+
...(style as SxProps)
33+
}}
34+
disableRipple
35+
>
36+
{icon}
37+
</IconButton>
4038
</CustomTooltip>
4139
);
4240
}

0 commit comments

Comments
 (0)