Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Address reverting unsaved changes issue to unpin filters #172063

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BehaviorSubject } from 'rxjs';
import type { SearchSourceFields } from '@kbn/data-plugin/common';
import type { DataView } from '@kbn/data-views-plugin/common';
import { SavedObjectSaveOpts } from '@kbn/saved-objects-plugin/public';
import { isEqual } from 'lodash';
import { isEqual, cloneDeep } from 'lodash';
import { restoreStateFromSavedSearch } from '../../../services/saved_searches/restore_from_saved_search';
import { updateSavedSearch } from '../utils/update_saved_search';
import { addLog } from '../../../utils/add_log';
Expand Down Expand Up @@ -114,7 +114,7 @@ export function getSavedSearchContainer({
globalStateContainer: DiscoverGlobalStateContainer;
}): DiscoverSavedSearchContainer {
const initialSavedSearch = services.savedSearch.getNew();
const savedSearchInitial$ = new BehaviorSubject(initialSavedSearch);
const savedSearchInitial$ = new BehaviorSubject(copySavedSearch(initialSavedSearch));
const savedSearchCurrent$ = new BehaviorSubject(copySavedSearch(initialSavedSearch));
const hasChanged$ = new BehaviorSubject(false);
const set = (savedSearch: SavedSearch) => {
Expand Down Expand Up @@ -223,10 +223,15 @@ export function getSavedSearchContainer({
* @param savedSearch
*/
export function copySavedSearch(savedSearch: SavedSearch): SavedSearch {
return {
const savedSearchCopy = {
...savedSearch,
...{ searchSource: savedSearch.searchSource.createCopy() },
};
savedSearchCopy.searchSource.setField(
'filter',
cloneDeep(savedSearchCopy.searchSource.getField('filter'))
);
return savedSearchCopy;
}

export function getDefaultAppState(savedSearch: SavedSearch, services: DiscoverServices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import type { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/common';
import { cloneDeep } from 'lodash';
import { cloneDeep, isEqual } from 'lodash';
import { isTextBasedQuery } from './is_text_based_query';
import type { DiscoverAppState } from '../services/discover_app_state_container';
import type { DiscoverServices } from '../../../build_services';
Expand Down Expand Up @@ -48,11 +48,20 @@ export function updateSavedSearch({
.setField('filter', services.data.query.filterManager.getFilters());
} else if (state) {
const appFilters = state.filters ? cloneDeep(state.filters) : [];
const globalFilters = globalStateContainer.get()?.filters ?? [];
const globalFilters = cloneDeep(globalStateContainer.get()?.filters ?? []);
// remove filters which are also present in appState
const cleanedGlobalFilters = globalFilters.filter((globalFilter) => {
const filterInAppState = appFilters.find(
(appFilter) =>
isEqual(appFilter.meta, globalFilter.meta) && isEqual(appFilter.query, globalFilter.query)
);

return !filterInAppState;
});

savedSearch.searchSource
.setField('query', state.query ?? undefined)
.setField('filter', [...appFilters, ...globalFilters]);
.setField('filter', [...appFilters, ...cleanedGlobalFilters]);

This comment was marked as outdated.

}
if (state) {
savedSearch.columns = state.columns || [];
Expand Down