Skip to content

Commit

Permalink
[Discover] Dataset search on page load issues (#8871)
Browse files Browse the repository at this point in the history
* Dataset search on page load issues

Required a double click on search and then also potentially loading issue.

Issue
n/a

Signed-off-by: Kawika Avilla <[email protected]>

* Clean up dependencies

Signed-off-by: Kawika Avilla <[email protected]>

* Addresses issue

Signed-off-by: Kawika Avilla <[email protected]>

* add some tests

Signed-off-by: Kawika Avilla <[email protected]>

* Changeset file for PR #8871 created/updated

---------

Signed-off-by: Kawika Avilla <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
kavilla and opensearch-changeset-bot[bot] authored Nov 15, 2024
1 parent e993d24 commit 42df421
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8871.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Search on page load out of sync state when clicking submit. ([#8871](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8871))
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,48 @@ describe('useSearch', () => {
});
});

it('should initialize with uninitialized state when dataset type config search on page load is disabled', async () => {
const services = createMockServices();
(services.uiSettings.get as jest.Mock).mockReturnValueOnce(true);
(services.data.query.queryString.getDatasetService as jest.Mock).mockReturnValue({
meta: { searchOnLoad: false },
});
(services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({
pause: true,
value: 10,
});

const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper });
expect(result.current.data$.getValue()).toEqual(
expect.objectContaining({ status: ResultStatus.UNINITIALIZED })
);

await act(async () => {
await waitForNextUpdate();
});
});

it('should initialize with uninitialized state when dataset type config search on page load is enabled but the UI setting is disabled', async () => {
const services = createMockServices();
(services.uiSettings.get as jest.Mock).mockReturnValueOnce(false);
(services.data.query.queryString.getDatasetService as jest.Mock).mockReturnValue({
meta: { searchOnLoad: true },
});
(services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({
pause: true,
value: 10,
});

const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper });
expect(result.current.data$.getValue()).toEqual(
expect.objectContaining({ status: ResultStatus.UNINITIALIZED })
);

await act(async () => {
await waitForNextUpdate();
});
});

it('should update startTime when hook rerenders', async () => {
const services = createMockServices();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,23 @@ export const useSearch = (services: DiscoverViewServices) => {
requests: new RequestAdapter(),
};

const getDatasetAutoSearchOnPageLoadPreference = () => {
// Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to true
const datasetType = data.query.queryString.getQuery().dataset?.type;

const datasetService = data.query.queryString.getDatasetService();

return !datasetType || (datasetService?.getType(datasetType)?.meta?.searchOnLoad ?? true);
};

const shouldSearchOnPageLoad = useCallback(() => {
// Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to UI Settings
const { queryString } = data.query;
const { dataset } = queryString.getQuery();
const typeConfig = dataset ? queryString.getDatasetService().getType(dataset.type) : undefined;
const datasetPreference =
typeConfig?.meta?.searchOnLoad ?? uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING);

// A saved search is created on every page load, so we check the ID to see if we're loading a
// previously saved search or if it is just transient
return (
services.uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) ||
datasetPreference ||
uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) ||
savedSearch?.id !== undefined ||
timefilter.getRefreshInterval().pause === false
);
}, [savedSearch, services.uiSettings, timefilter]);
}, [data.query, savedSearch, uiSettings, timefilter]);

const startTime = Date.now();
const data$ = useMemo(
Expand Down Expand Up @@ -346,9 +345,6 @@ export const useSearch = (services: DiscoverViewServices) => {
]);

useEffect(() => {
if (!getDatasetAutoSearchOnPageLoadPreference()) {
skipInitialFetch.current = true;
}
const fetch$ = merge(
refetch$,
filterManager.getFetches$(),
Expand Down Expand Up @@ -379,8 +375,6 @@ export const useSearch = (services: DiscoverViewServices) => {
return () => {
subscription.unsubscribe();
};
// disabling the eslint since we are not adding getDatasetAutoSearchOnPageLoadPreference since this changes when dataset changes and these chnages are already part of data.query.queryString
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
data$,
data.query.queryString,
Expand Down

0 comments on commit 42df421

Please sign in to comment.