From 6d44e65331d53678e46a1161ac4c7f627b966260 Mon Sep 17 00:00:00 2001 From: Amit Amrutiya Date: Tue, 19 Nov 2024 09:50:08 +0530 Subject: [PATCH] fix: table date and pagination issue Signed-off-by: Amit Amrutiya --- .../CatalogDesignTable/CatalogDesignTable.tsx | 135 ++++++++++++------ src/custom/TooltipIcon.tsx | 30 ++-- 2 files changed, 108 insertions(+), 57 deletions(-) diff --git a/src/custom/CatalogDesignTable/CatalogDesignTable.tsx b/src/custom/CatalogDesignTable/CatalogDesignTable.tsx index 8808b42d..74f4e441 100644 --- a/src/custom/CatalogDesignTable/CatalogDesignTable.tsx +++ b/src/custom/CatalogDesignTable/CatalogDesignTable.tsx @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import _ from 'lodash'; -import { useEffect, useRef, useState } from 'react'; +import { useCallback, useMemo, useRef } from 'react'; import { PublishIcon } from '../../icons'; import { CHARCOAL, useTheme } from '../../theme'; import { Pattern } from '../CustomCatalog/CustomCard'; @@ -47,28 +47,54 @@ export const CatalogDesignsTable: React.FC = ({ handleBulkDeleteModal, handleBulkpatternsDataUnpublishModal }) => { - const [tableCols, updateCols] = useState>([]); const { width } = useWindowDimensions(); const smallScreen = width <= 360; const theme = useTheme(); const modalRef = useRef(null); - useEffect(() => { - if (Array.isArray(columns) && columns.length > 0) { - updateCols(columns); - } - }, [columns]); + const formatDate = useCallback((date: string | Date): string => { + const dateOptions: Intl.DateTimeFormatOptions = { + weekday: 'short', + day: 'numeric', + month: 'long', + year: 'numeric' + }; + return new Date(date).toLocaleDateString('en-US', dateOptions); + }, []); + + const processedColumns = useMemo(() => { + return columns.map((col) => { + const newCol = { ...col }; + if (!newCol.options) newCol.options = {}; + newCol.options.display = columnVisibility[col.name]; + if ( + [ + 'updated_at', + 'created_at', + 'deleted_at', + 'last_login_time', + 'joined_at', + 'last_run', + 'next_run' + ].includes(col.name) + ) { + newCol.options.customBodyRender = (value: any) => { + if (!value || value === 'NA') return <>NA; + if (typeof value === 'object' && 'Valid' in value) { + if (value.Valid && value.Time) { + return <>{formatDate(value.Time)}; + } + return <>NA; + } + return <>{formatDate(value)}; + }; + } + return newCol; + }); + }, [columns, columnVisibility, formatDate]); - const options: any = { - selectableRows: _.isNil(filter) ? 'none' : 'multiple', - serverSide: true, - filterType: 'multiselect', - responsive: smallScreen ? 'vertical' : 'standard', - count: totalCount, - rowsPerPage: pageSize, - page, - elevation: 0, - onTableChange: (action: string, tableState: any) => { + const handleTableChange = useCallback( + (action: string, tableState: any) => { const sortInfo = tableState.announceText ? tableState.announceText.split(' : ') : []; let order = ''; if (tableState.activeColumn) { @@ -98,29 +124,57 @@ export const CatalogDesignsTable: React.FC = ({ } break; } - } - }; + }, + [columns, setPage, setPageSize, setSortOrder, sortOrder] + ); - if (_.isNil(filter)) { - options.customToolbarSelect = (selected: any) => ( - handleBulkpatternsDataUnpublishModal(selected, patterns, modalRef)} - iconType="publish" - id={'unpublish-button'} - > - - - ); - } else { - options.onRowsDelete = (rowsDeleted: any) => { - const selectedPatterns = rowsDeleted.data.map(({ dataIndex }: any) => patterns[dataIndex]); - handleBulkDeleteModal(selectedPatterns, modalRef); - return false; - }; - } + const options = useMemo( + () => ({ + selectableRows: _.isNil(filter) ? 'none' : 'multiple', + serverSide: true, + filterType: 'multiselect', + responsive: smallScreen ? 'vertical' : 'standard', + count: totalCount, + rowsPerPage: pageSize, + page, + elevation: 0, + onTableChange: handleTableChange, + customToolbarSelect: _.isNil(filter) + ? (selected: any) => ( + handleBulkpatternsDataUnpublishModal(selected, patterns, modalRef)} + iconType="publish" + id={'unpublish-button'} + > + + + ) + : undefined, + onRowsDelete: !_.isNil(filter) + ? (rowsDeleted: any) => { + const selectedPatterns = rowsDeleted.data.map( + ({ dataIndex }: any) => patterns[dataIndex] + ); + handleBulkDeleteModal(selectedPatterns, modalRef); + return false; + } + : undefined + }), + [ + filter, + smallScreen, + totalCount, + pageSize, + page, + handleTableChange, + patterns, + handleBulkDeleteModal, + handleBulkpatternsDataUnpublishModal + ] + ); - if (!Array.isArray(tableCols) || tableCols.length === 0) { + if (!processedColumns.length) { return null; } @@ -128,14 +182,13 @@ export const CatalogDesignsTable: React.FC = ({ <> -
- - {icon} - -
+ borderRadius: '4px' + }, + ...(style as SxProps) + }} + disableRipple + > + {icon} + ); }