Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@tanstack/react-pacer": ">= 0.15.0",
"@tanstack/react-query": ">= 5.83.0",
"p-all": ">= 4.0.0",
"qs": ">= 6.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"use-deep-compare": "^1.3.0"
Expand Down
28 changes: 27 additions & 1 deletion src/components/TableToolsTable/TableToolsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import TableToolbar from '@redhat-cloud-services/frontend-components/TableToolbar';

import useTableTools from '~/hooks/useTableTools';
import useTableSearchParams from '~/hooks/useTableSearchParams';
import { TableStateProvider, FilterModal, TableViewToggle } from '~/components';

const TableToolsTable = ({
Expand All @@ -34,6 +35,7 @@
paginationProps,
...tablePropsRest
}) => {
const searchParamsState = useTableSearchParams(options);
const {
view,
loading,
Expand All @@ -49,10 +51,34 @@
externalTotal,
{
treeTable,
filters,
filters: {
...(filters || {}),
activeFilters: {
...filters?.activeFilters,
...(searchParamsState?.filters || {}),
},
},
columns,
toolbarProps: toolbarPropsProp,
tableProps: tablePropsRest,
...(searchParamsState
? {
...(searchParamsState.page
? { page: parseInt(searchParamsState.page) }
: {}),
...(searchParamsState.perPage
? { perPage: parseInt(searchParamsState.perPage) }
: {}),
...(searchParamsState.sort
? {
sortBy: {
...searchParamsState.sort,
index: parseInt(searchParamsState.sort.index),
},
}
: {}),
}
: {}),
...options,
},
);
Expand Down Expand Up @@ -125,7 +151,7 @@

/**
* This component is a wrapper around the Patternfly Table component(s), the FEC PrimaryToolbar and combines them with the `useTableTools` hook
* If no {@link TableContext}

Check warning on line 154 in src/components/TableToolsTable/TableToolsTable.js

View workflow job for this annotation

GitHub Actions / build (21.x)

The type 'TableContext' is undefined

Check warning on line 154 in src/components/TableToolsTable/TableToolsTable.js

View workflow job for this annotation

GitHub Actions / build (22.x)

The type 'TableContext' is undefined
*
*
* @param {object} props Component Props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import {
Card,
CardBody,
Content,
ContentVariants,

Check failure on line 8 in src/components/TableToolsTable/TableToolsTableExperiments.stories.js

View workflow job for this annotation

GitHub Actions / build (21.x)

'ContentVariants' is defined but never used

Check failure on line 8 in src/components/TableToolsTable/TableToolsTableExperiments.stories.js

View workflow job for this annotation

GitHub Actions / build (21.x)

'ContentVariants' is defined but never used

Check failure on line 8 in src/components/TableToolsTable/TableToolsTableExperiments.stories.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'ContentVariants' is defined but never used

Check failure on line 8 in src/components/TableToolsTable/TableToolsTableExperiments.stories.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'ContentVariants' is defined but never used
Spinner,
Button,
Label,
Expand Down Expand Up @@ -536,4 +538,62 @@
render: (args) => <AccessItemsExample {...args} />,
};

const UrlParamsExample = () => {
const exampleUrl =
'perPage=20&page=1&filters[title][0]=ds&filters[rating-above][0]=4&sort[index]=1&sort[direction]=desc';
const searchParams = new URLSearchParams(exampleUrl);
const [searchParamsState, setSearchParamsState] = useState();
const setSearchParams = useCallback((params) => {
setSearchParamsState(params);
}, []);

const {
loading,
result: { data, meta: { total } = {} } = {},
error,
} = useExampleDataQuery({
endpoint: '/api',
useTableState: true,
});

return (
<>
<Content component="p">
<strong>URL would be:</strong>{' '}
{decodeURI(searchParamsState?.toString() || '')}
</Content>
<TableToolsTable
loading={loading}
items={data}
total={total}
error={error}
columns={columns}
filters={{
filterConfig: filters,
}}
options={{
debug: true,
...defaultOptions,
onSelect: true,
searchParams,
setSearchParams,
}}
/>
</>
);
};

export const UrlParamsStory = {
decorators: [
(Story) => (
<QueryClientProvider client={queryClient}>
<TableStateProvider>
<Story />
</TableStateProvider>
</QueryClientProvider>
),
],
render: (args) => <UrlParamsExample {...args} />,
};

export default meta;
2 changes: 2 additions & 0 deletions src/hooks/useFilterConfig/useFilterConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ const useFilterConfig = (options) => {
: {},
);

// TODO with URL params it can (for some reason) happen that initial values get debounced
// and the first request won't include filters.
const debouncedSetState = useDebouncedCallback(setTableState, { wait: 500 });

useEffect(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/usePagination/hooks/usePaginationState.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import useTableState from '~/hooks/useTableState';
import { TABLE_STATE_NAMESPACE } from '../constants';

const usePaginationState = (options) => {
const { perPage = 10, serialisers } = options;
const { perPage = 10, page = 1, serialisers } = options;
const defaultState = useMemo(() => {
return {
perPage,
page: 1,
page,
};
}, [perPage]);
}, [perPage, page]);
const resetPage = useCallback(
(currentState) => {
return {
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useTableSearchParams/hooks/useSearchParamsState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useCallback, useRef } from 'react';
import { parse, stringify } from 'qs';

const useSearchParamsState = ({ searchParams, setSearchParams }) => {
const searchParamsState = useRef(
searchParams ? parse(searchParams.toString()) : undefined,
);

const setSearchParamsState = useCallback(
(params) => {
setSearchParams(new URLSearchParams(stringify(params)));
},
[setSearchParams],
);

return [
searchParams && searchParamsState.current,
setSearchParams && setSearchParamsState,
];
};

export default useSearchParamsState;
1 change: 1 addition & 0 deletions src/hooks/useTableSearchParams/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './useTableSearchParams';
30 changes: 30 additions & 0 deletions src/hooks/useTableSearchParams/useTableSearchParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useDeepCompareEffect } from 'use-deep-compare';
import { useFullTableState } from '~/hooks';

import useSearchParamsState from './hooks/useSearchParamsState';

const useTableSearchParams = ({ searchParams, setSearchParams }) => {
const fullTableState = useFullTableState();
const {
tableState: { pagination: { state: pagination } = {}, filters, sort } = {},
} = fullTableState || {};

const [searchParamsState, setSearchParamsState] = useSearchParamsState({
searchParams,
setSearchParams,
});

useDeepCompareEffect(() => {
if (setSearchParamsState) {
setSearchParamsState({
...(pagination || {}),
filters,
...(sort ? { sort } : {}),
});
}
}, [pagination, filters, sort, setSearchParamsState]);

return searchParamsState;
};

export default useTableSearchParams;
16 changes: 10 additions & 6 deletions src/hooks/useTableSort/useTableSort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, useMemo } from 'react';
import { useDeepCompareMemo } from 'use-deep-compare';

import useTableState, { useRawTableState } from '~/hooks/useTableState';
Expand Down Expand Up @@ -41,7 +41,14 @@ const useTableSort = (columns, options = {}) => {
serialisers: { sort: serialiser } = {},
onSort: onSortOption,
} = options;

const defaultState = useMemo(
() =>
initialSortBy || {
index: 0,
direction: 'asc',
},
[initialSortBy],
);
const { tableView } = useRawTableState() || {};
const offset = columnOffset({ ...options, tableView });

Expand All @@ -57,10 +64,7 @@ const useTableSort = (columns, options = {}) => {
);
const [sortBy, setSortBy] = useTableState(
TABLE_STATE_NAMESPACE,
initialSortBy || {
index: 0,
direction: 'asc',
},
defaultState,
stateOptions,
);

Expand Down
2 changes: 1 addition & 1 deletion src/support/factories/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const rating = {
label: 'Rating above',
items: [...new Array(5)].map((_, idx) => ({
label: [...new Array(idx + 1)].map(() => '★'),
value: idx + 1,
value: `${idx + 1}`,
})),
filterSerialiser: (_config, value) => `.rating >= ${value}`,
};
Expand Down
Loading