fix(ui): persist pagination on explore page when user hit back on browser (#8760)

* fix the reset of the explore page once back for details page

* fix page got reset when back from details page

* fix unit tests

* fix the reset info on sort change

Co-authored-by: Chirag Madlani <12962843+chirag-madlani@users.noreply.github.com>
This commit is contained in:
Ashish Gupta 2022-11-17 12:26:42 +05:30 committed by GitHub
parent e74ec6f7e2
commit 7e7abac3a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 58 deletions

View File

@ -18,7 +18,6 @@ import {
Query, Query,
Utils as QbUtils, Utils as QbUtils,
} from 'react-awesome-query-builder'; } from 'react-awesome-query-builder';
import 'react-awesome-query-builder/css/antd.less';
import { elasticSearchFormat } from '../../utils/QueryBuilderElasticsearchFormatUtils'; import { elasticSearchFormat } from '../../utils/QueryBuilderElasticsearchFormatUtils';
import { emptyJsonTree, getQbConfigs } from './AdvancedSearch.constants'; import { emptyJsonTree, getQbConfigs } from './AdvancedSearch.constants';
import { AdvancedSearchProps } from './AdvancedSearch.interface'; import { AdvancedSearchProps } from './AdvancedSearch.interface';

View File

@ -20,7 +20,6 @@ import { Card, Col, Row, Tabs } from 'antd';
import { AxiosError } from 'axios'; import { AxiosError } from 'axios';
import unique from 'fork-ts-checker-webpack-plugin/lib/utils/array/unique'; import unique from 'fork-ts-checker-webpack-plugin/lib/utils/array/unique';
import { import {
isEmpty,
isNil, isNil,
isNumber, isNumber,
lowerCase, lowerCase,
@ -290,14 +289,6 @@ const Explore: React.FC<ExploreProps> = ({
term[filter.key] = filter.value; term[filter.key] = filter.value;
} }
}); });
onChangeAdvancedSearchQueryFilter(
isEmpty(term)
? {}
: {
query: { bool: { must: [{ term }] } },
}
);
}, [selectedQuickFilters]); }, [selectedQuickFilters]);
return ( return (

View File

@ -72,36 +72,6 @@ const ExplorePage: FunctionComponent = () => {
[location.search] [location.search]
); );
const handleSearchIndexChange: (nSearchIndex: ExploreSearchIndex) => void = (
nSearchIndex
) =>
history.push({
pathname: `/explore/${tabsInfo[nSearchIndex].path}/${searchQueryParam}`,
});
const handleQueryFilterChange: ExploreProps['onChangeAdvancedSearchJsonTree'] =
(queryFilter) =>
history.push({
search: Qs.stringify({
...parsedSearch,
queryFilter: JSON.stringify(queryFilter),
}),
});
const handlePostFilterChange: ExploreProps['onChangePostFilter'] = (
postFilter
) =>
history.push({
search: Qs.stringify({ ...parsedSearch, postFilter }),
});
const handlePageChange: ExploreProps['onChangePage'] = (page) =>
history.push({ search: Qs.stringify({ ...parsedSearch, page }) });
const handleShowDeletedChange: ExploreProps['onChangeShowDeleted'] = (
showDeleted
) => history.push({ search: Qs.stringify({ ...parsedSearch, showDeleted }) });
const postFilter = useMemo( const postFilter = useMemo(
() => () =>
isFilterObject(parsedSearch.postFilter) isFilterObject(parsedSearch.postFilter)
@ -115,6 +85,49 @@ const ExplorePage: FunctionComponent = () => {
[postFilter] [postFilter]
); );
const handlePageChange: ExploreProps['onChangePage'] = (page) => {
history.push({ search: Qs.stringify({ ...parsedSearch, page }) });
};
const handleSearchIndexChange: (nSearchIndex: ExploreSearchIndex) => void = (
nSearchIndex
) => {
history.push({
pathname: `/explore/${tabsInfo[nSearchIndex].path}/${searchQueryParam}`,
search: Qs.stringify({ page: 1 }),
});
};
const handleQueryFilterChange: ExploreProps['onChangeAdvancedSearchJsonTree'] =
(queryFilter) => {
history.push({
pathname: history.location.pathname,
search: Qs.stringify({
...parsedSearch,
queryFilter: JSON.stringify(queryFilter),
page: 1,
}),
});
};
const handlePostFilterChange: ExploreProps['onChangePostFilter'] = (
postFilter
) => {
history.push({
pathname: history.location.pathname,
search: Qs.stringify({ ...parsedSearch, postFilter, page: 1 }),
});
};
const handleShowDeletedChange: ExploreProps['onChangeShowDeleted'] = (
showDeleted
) => {
history.push({
pathname: history.location.pathname,
search: Qs.stringify({ ...parsedSearch, showDeleted, page: 1 }),
});
};
const queryFilter = useMemo(() => { const queryFilter = useMemo(() => {
if (!isString(parsedSearch.queryFilter)) { if (!isString(parsedSearch.queryFilter)) {
return undefined; return undefined;
@ -150,7 +163,7 @@ const ExplorePage: FunctionComponent = () => {
useEffect(() => { useEffect(() => {
handleSearchIndexChange(searchIndex); handleSearchIndexChange(searchIndex);
}, [searchIndex]); }, [searchIndex, searchQueryParam]);
const page = useMemo(() => { const page = useMemo(() => {
const pageParam = parsedSearch.page; const pageParam = parsedSearch.page;
@ -238,19 +251,12 @@ const ExplorePage: FunctionComponent = () => {
page, page,
]); ]);
// Return to first page on filter change const handleAdvanceSearchQueryFilterChange = (
useDeepCompareEffect( filter?: Record<string, unknown>
() => handlePageChange(1), ) => {
[ handlePageChange(1);
searchIndex, setAdvancedSearchQueryFilter(filter);
searchQueryParam, };
sortValue,
sortOrder,
showDeleted,
advancesSearchQueryFilter,
elasticsearchPostFilterQuery,
]
);
useEffect(() => { useEffect(() => {
AppState.updateExplorePageTab(tab); AppState.updateExplorePageTab(tab);
@ -270,13 +276,19 @@ const ExplorePage: FunctionComponent = () => {
sortValue={sortValue} sortValue={sortValue}
tabCounts={searchHitCounts} tabCounts={searchHitCounts}
onChangeAdvancedSearchJsonTree={handleQueryFilterChange} onChangeAdvancedSearchJsonTree={handleQueryFilterChange}
onChangeAdvancedSearchQueryFilter={setAdvancedSearchQueryFilter} onChangeAdvancedSearchQueryFilter={handleAdvanceSearchQueryFilterChange}
onChangePage={handlePageChange} onChangePage={handlePageChange}
onChangePostFilter={handlePostFilterChange} onChangePostFilter={handlePostFilterChange}
onChangeSearchIndex={handleSearchIndexChange} onChangeSearchIndex={handleSearchIndexChange}
onChangeShowDeleted={handleShowDeletedChange} onChangeShowDeleted={handleShowDeletedChange}
onChangeSortOder={setSortOrder} onChangeSortOder={(sort) => {
onChangeSortValue={setSortValue} handlePageChange(1);
setSortOrder(sort);
}}
onChangeSortValue={(sort) => {
handlePageChange(1);
setSortValue(sort);
}}
/> />
</PageContainerV1> </PageContainerV1>
); );

View File

@ -283,6 +283,9 @@ jest.mock('react-router-dom', () => ({
useParams: jest.fn().mockImplementation(() => ({ searchQuery: '' })), useParams: jest.fn().mockImplementation(() => ({ searchQuery: '' })),
useHistory: () => ({ useHistory: () => ({
push: jest.fn(), push: jest.fn(),
location: {
pathname: '',
},
}), }),
useLocation: jest useLocation: jest
.fn() .fn()

View File

@ -35,7 +35,7 @@ export const filterObjectToElasticsearchQuery: (
f: FilterObject | undefined f: FilterObject | undefined
) => Record<string, unknown> | undefined = (f) => { ) => Record<string, unknown> | undefined = (f) => {
if (isNil(f)) { if (isNil(f)) {
return { query: { bool: {} } }; return {};
} }
if (!Object.entries(f).length) { if (!Object.entries(f).length) {
return undefined; return undefined;