Skip to content

Commit

Permalink
feat: preferences for pagination in table widget #1890
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandru-HM authored and tracy-french committed Sep 28, 2023
1 parent f2662cc commit 8072232
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
33 changes: 27 additions & 6 deletions packages/dashboard/src/customization/widgets/table/component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { useState } from 'react';
import { useSelector } from 'react-redux';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { CollectionPreferences, CollectionPreferencesProps } from '@cloudscape-design/components';

import { Table, TableColumnDefinition } from '@iot-app-kit/react-components';
import { SiteWiseAssetQuery } from '@iot-app-kit/source-iotsitewise';

import EmptyTableComponent from './emptyTableComponent';

Expand All @@ -10,9 +12,10 @@ import type { DashboardState } from '~/store/state';
import type { TableWidget } from '../types';
import { useQueries } from '~/components/dashboard/queryContext';
import { useChartSize } from '~/hooks/useChartSize';
import { DEFAULT_PREFERENCES } from './table-config';

import { DEFAULT_PREFERENCES, collectionPreferencesProps } from './table-config';
import { TABLE_OVERFLOW_HEIGHT, TABLE_WIDGET_MAX_HEIGHT } from '../constants';
import { SiteWiseAssetQuery } from '@iot-app-kit/source-iotsitewise';
import { onUpdateWidgetsAction } from '~/store/actions';

export const DEFAULT_TABLE_COLUMN_DEFINITIONS: TableColumnDefinition[] = [
{
Expand Down Expand Up @@ -52,8 +55,9 @@ const TableWidgetComponent: React.FC<TableWidget> = (widget) => {
const key = computeQueryConfigKey(viewport, widget.properties.queryConfig);

const significantDigits = widgetSignificantDigits ?? dashboardSignificantDigits;
const [preferences] = useState(DEFAULT_PREFERENCES); //TODO: setpreference will add once page preferences are added
const chartSize = useChartSize(widget);
const readOnly = useSelector((state: DashboardState) => state.readOnly);
const dispatch = useDispatch();

const handleMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
const target = e.target as HTMLDivElement;
Expand All @@ -65,6 +69,14 @@ const TableWidgetComponent: React.FC<TableWidget> = (widget) => {
}
};

const setPreferences = (detail: CollectionPreferencesProps.Preferences) => {
dispatch(
onUpdateWidgetsAction({
widgets: [{ ...widget, properties: { ...widget.properties, pageSize: detail.pageSize } }],
})
);
};

return (
<div
onMouseDown={handleMouseDown}
Expand All @@ -84,9 +96,18 @@ const TableWidgetComponent: React.FC<TableWidget> = (widget) => {
significantDigits={significantDigits}
sortingDisabled
stickyHeader
pageSize={preferences.pageSize}
pageSize={widget.properties.pageSize ?? DEFAULT_PREFERENCES.pageSize}
paginationEnabled
empty={<EmptyTableComponent />}
preferences={
!readOnly && (
<CollectionPreferences
{...collectionPreferencesProps}
preferences={{ pageSize: widget.properties.pageSize ?? DEFAULT_PREFERENCES.pageSize }}
onConfirm={({ detail }) => setPreferences(detail)}
/>
)
}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export const DEFAULT_PREFERENCES = {
pageSize: 20,
};

export const pageSizePreference = {
title: 'Select page size',
options: [
{ value: 10, label: '10 resources' },
{ value: 20, label: '20 resources' },
{ value: 30, label: '30 resources' },
],
};

export const collectionPreferencesProps = {
pageSizePreference,
cancelLabel: 'Cancel',
confirmLabel: 'Confirm',
title: 'Preferences',
};
1 change: 1 addition & 0 deletions packages/dashboard/src/customization/widgets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export type TableProperties = QueryProperties & {
items?: TableItem[];
columnDefinitions?: TableColumnDefinition[];
significantDigits?: number;
pageSize?: number;
};
export type TablePropertiesKeys = keyof TableProperties;

Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/src/components/table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const Table = ({
significantDigits?: number;
paginationEnabled?: boolean;
pageSize?: number;
} & Pick<TableBaseProps, 'resizableColumns' | 'sortingDisabled' | 'stickyHeader' | 'empty'>) => {
} & Pick<TableBaseProps, 'resizableColumns' | 'sortingDisabled' | 'stickyHeader' | 'empty' | 'preferences'>) => {
const { dataStreams, thresholds: queryThresholds } = useTimeSeriesData({
viewport: passedInViewport,
queries,
Expand Down

0 comments on commit 8072232

Please sign in to comment.