|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import _ from 'lodash'; |
| 3 | +import { useEffect, useRef, useState } from 'react'; |
| 4 | +import { PublishIcon } from '../../icons'; |
| 5 | +import { CHARCOAL, useTheme } from '../../theme'; |
| 6 | +import { Pattern } from '../CustomCatalog/CustomCard'; |
| 7 | +import { useWindowDimensions } from '../Helpers/Dimension'; |
| 8 | +import PromptComponent from '../Prompt'; |
| 9 | +import { PromptRef } from '../Prompt/promt-component'; |
| 10 | +import ResponsiveDataTable from '../ResponsiveDataTable'; |
| 11 | +import UnpublishTooltipIcon from './UnpublishTooltipIcon'; |
| 12 | + |
| 13 | +interface CatalogDesignsTableProps { |
| 14 | + patterns: Pattern[]; |
| 15 | + filter: any; |
| 16 | + columns: Array<any>; |
| 17 | + totalCount: number; |
| 18 | + sortOrder: string; |
| 19 | + setSortOrder: (order: string) => void; |
| 20 | + pageSize: number; |
| 21 | + setPageSize: (size: number) => void; |
| 22 | + page: number; |
| 23 | + setPage: (page: number) => void; |
| 24 | + columnVisibility: Record<string, boolean>; |
| 25 | + colViews: Record<string, boolean> | undefined; |
| 26 | + handleBulkDeleteModal: (patterns: Pattern[], modalRef: React.RefObject<PromptRef>) => void; |
| 27 | + handleBulkpatternsDataUnpublishModal: ( |
| 28 | + selected: any, |
| 29 | + patterns: Pattern[], |
| 30 | + modalRef: React.RefObject<PromptRef> |
| 31 | + ) => void; |
| 32 | +} |
| 33 | + |
| 34 | +export const CatalogDesignsTable: React.FC<CatalogDesignsTableProps> = ({ |
| 35 | + patterns, |
| 36 | + filter, |
| 37 | + columns = [], |
| 38 | + totalCount = 0, |
| 39 | + sortOrder = '', |
| 40 | + setSortOrder, |
| 41 | + pageSize = 10, |
| 42 | + setPageSize, |
| 43 | + page = 0, |
| 44 | + setPage, |
| 45 | + columnVisibility = {}, |
| 46 | + colViews = {}, |
| 47 | + handleBulkDeleteModal, |
| 48 | + handleBulkpatternsDataUnpublishModal |
| 49 | +}) => { |
| 50 | + const [tableCols, updateCols] = useState<Array<any>>([]); |
| 51 | + const { width } = useWindowDimensions(); |
| 52 | + const smallScreen = width <= 360; |
| 53 | + const theme = useTheme(); |
| 54 | + const modalRef = useRef<PromptRef>(null); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (Array.isArray(columns) && columns.length > 0) { |
| 58 | + updateCols(columns); |
| 59 | + } |
| 60 | + }, [columns]); |
| 61 | + |
| 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) => { |
| 72 | + const sortInfo = tableState.announceText ? tableState.announceText.split(' : ') : []; |
| 73 | + let order = ''; |
| 74 | + if (tableState.activeColumn) { |
| 75 | + order = `${columns[tableState.activeColumn].name} desc`; |
| 76 | + } |
| 77 | + switch (action) { |
| 78 | + case 'changePage': |
| 79 | + setPage(tableState.page); |
| 80 | + break; |
| 81 | + case 'changeRowsPerPage': |
| 82 | + setPageSize(tableState.rowsPerPage); |
| 83 | + break; |
| 84 | + case 'sort': |
| 85 | + if ( |
| 86 | + sortInfo.length === 2 && |
| 87 | + tableState.activeColumn !== undefined && |
| 88 | + Array.isArray(columns) |
| 89 | + ) { |
| 90 | + if (sortInfo[1] === 'ascending') { |
| 91 | + order = `${columns[tableState.activeColumn].name} asc`; |
| 92 | + } else { |
| 93 | + order = `${columns[tableState.activeColumn].name} desc`; |
| 94 | + } |
| 95 | + } |
| 96 | + if (order !== sortOrder) { |
| 97 | + setSortOrder(order); |
| 98 | + } |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 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 | + } |
| 122 | + |
| 123 | + if (!Array.isArray(tableCols) || tableCols.length === 0) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + return ( |
| 128 | + <> |
| 129 | + <PromptComponent ref={modalRef} /> |
| 130 | + <ResponsiveDataTable |
| 131 | + columns={columns} |
| 132 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 133 | + //@ts-ignore |
| 134 | + data={patterns || []} |
| 135 | + options={options} |
| 136 | + colViews={colViews} |
| 137 | + tableCols={tableCols} |
| 138 | + updateCols={updateCols} |
| 139 | + columnVisibility={columnVisibility} |
| 140 | + backgroundColor={ |
| 141 | + theme.palette.mode === 'light' |
| 142 | + ? theme.palette.background.default |
| 143 | + : theme.palette.background.secondary |
| 144 | + } |
| 145 | + /> |
| 146 | + </> |
| 147 | + ); |
| 148 | +}; |
| 149 | + |
| 150 | +export default CatalogDesignsTable; |
0 commit comments