Skip to content

Commit

Permalink
Reset current page after data source change (#320) (#321)
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
(cherry picked from commit 7048a0e)

Co-authored-by: Lin Wang <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and wanglam authored Apr 24, 2024
1 parent 8b295e7 commit df4ab4d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
46 changes: 45 additions & 1 deletion public/components/monitoring/__tests__/use_monitoring.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ describe('useMonitoring', () => {
});
});

it('should reset connector filter after selected data source option change', async () => {
it('should reset connector filter and current page after data source change', async () => {
const {
renderHookResult: { result, waitFor },
setSelectedDataSourceOption,
Expand All @@ -518,17 +518,61 @@ describe('useMonitoring', () => {
});
act(() => {
result.current.searchByConnector(['connector-1']);
result.current.handleTableChange({
pagination: {
currentPage: 2,
pageSize: 50,
},
});
});
await waitFor(() => {
expect(Model.prototype.search).toHaveBeenCalledTimes(2);
expect(result.current.params).toEqual(
expect.objectContaining({
currentPage: 2,
connector: ['connector-1'],
})
);
});
act(() => {
setSelectedDataSourceOption({ id: 'bar' });
});
await waitFor(() => {
expect(Model.prototype.search).toHaveBeenCalledTimes(3);
expect(result.current.params.connector).toEqual([]);
expect(result.current.params.currentPage).toEqual(1);
});
});

it('should reset connectors, status, source and nameOrId filter after resetSearch called', async () => {
const {
renderHookResult: { result },
} = setup();
act(() => {
result.current.searchByNameOrId('foo');
result.current.searchByConnector(['connector-1']);
result.current.searchBySource(['local']);
result.current.searchByStatus(['responding']);
});
expect(result.current.params).toEqual(
expect.objectContaining({
nameOrId: 'foo',
connector: ['connector-1'],
source: ['local'],
status: ['responding'],
})
);
act(() => {
result.current.resetSearch();
});
expect(result.current.params).toEqual(
expect.objectContaining({
nameOrId: '',
connector: [],
source: [],
status: undefined,
})
);
});
});

Expand Down
5 changes: 5 additions & 0 deletions public/components/monitoring/use_monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export const useMonitoring = () => {
currentPage: previousValue.currentPage,
pageSize: previousValue.pageSize,
sort: previousValue.sort,
nameOrId: '',
source: [],
connector: [],
status: undefined,
Expand Down Expand Up @@ -280,13 +281,17 @@ export const useMonitoring = () => {
);

useEffect(() => {
if (!dataSourceEnabled) {
return;
}
setParams((previousParams) => {
const dataSourceId = getDataSourceId(dataSourceEnabled, selectedDataSourceOption);
if (previousParams.dataSourceId === dataSourceId) {
return previousParams;
}
return {
...previousParams,
currentPage: 1,
dataSourceId,
connector: [],
};
Expand Down

0 comments on commit df4ab4d

Please sign in to comment.