Skip to content

Commit

Permalink
remove query param from the URL only on subsequant state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Nov 3, 2020
1 parent 5f83611 commit 26146f3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 24 deletions.
40 changes: 17 additions & 23 deletions src/plugins/discover/public/application/angular/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import {
MODIFY_COLUMNS_ON_SWITCH,
} from '../../../common';
import { SEARCH_SESSION_ID_QUERY_PARAM } from '../../url_generator';
import { removeQueryParam } from '../../../../kibana_utils/public';
import { removeQueryParam, getQueryParams } from '../../../../kibana_utils/public';

const fetchStatuses = {
UNINITIALIZED: 'uninitialized',
Expand All @@ -92,6 +92,9 @@ const fetchStatuses = {
ERROR: 'error',
};

const getSearchSessionIdFromURL = (history) =>
getQueryParams(history.location)[SEARCH_SESSION_ID_QUERY_PARAM];

const app = getAngularModule();

app.config(($routeProvider) => {
Expand Down Expand Up @@ -193,16 +196,7 @@ app.directive('discoverApp', function () {
};
});

function discoverController(
$element,
$route,
$scope,
$timeout,
$window,
Promise,
uiCapabilities,
$routeParams
) {
function discoverController($element, $route, $scope, $timeout, $window, Promise, uiCapabilities) {
const { isDefault: isDefaultType } = indexPatternsUtils;
const subscriptions = new Subscription();
const refetch$ = new Subject();
Expand All @@ -218,11 +212,7 @@ function discoverController(
};

const history = getHistory();

let initialSearchSessionId = $routeParams[SEARCH_SESSION_ID_QUERY_PARAM];
if (initialSearchSessionId) {
removeQueryParam(history, SEARCH_SESSION_ID_QUERY_PARAM);
}
let isInitialSearch = true;

const {
appStateContainer,
Expand Down Expand Up @@ -814,14 +804,18 @@ function discoverController(
abortController = new AbortController();

const searchSessionId = (() => {
if (initialSearchSessionId) {
const searchSessionId = initialSearchSessionId;
data.search.session.restore(searchSessionId);
initialSearchSessionId = null; // reset to make further searches create new session
return searchSessionId;
} else {
return data.search.session.start();
const searchSessionIdFromURL = getSearchSessionIdFromURL(history);
if (searchSessionIdFromURL) {
if (isInitialSearch) {
data.search.session.restore(searchSessionIdFromURL);
isInitialSearch = false;
return searchSessionIdFromURL;
} else {
// navigating away from background search
removeQueryParam(history, SEARCH_SESSION_ID_QUERY_PARAM);
}
}
return data.search.session.start();
})();

$scope
Expand Down
41 changes: 41 additions & 0 deletions src/plugins/kibana_utils/public/history/get_query_params.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { getQueryParams } from './get_query_params';
import { Location } from 'history';

describe('getQueryParams', () => {
it('should getQueryParams', () => {
const location: Location<any> = {
pathname: '/dashboard/c3a76790-3134-11ea-b024-83a7b4783735',
search: "?_a=(description:'')&_b=3",
state: null,
hash: '',
};

const query = getQueryParams(location);

expect(query).toMatchInlineSnapshot(`
Object {
"_a": "(description:'')",
"_b": "3",
}
`);
});
});
27 changes: 27 additions & 0 deletions src/plugins/kibana_utils/public/history/get_query_params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { parse, ParsedQuery } from 'query-string';
import { Location } from 'history';

export function getQueryParams(location: Location): ParsedQuery {
const search = (location.search || '').replace(/^\?/, '');
const query = parse(search, { sort: false });
return query;
}
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/public/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

export { removeQueryParam } from './remove_query_param';
export { redirectWhenMissing } from './redirect_when_missing';
export { getQueryParams } from './get_query_params';
2 changes: 1 addition & 1 deletion src/plugins/kibana_utils/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export {
StopSyncStateFnType,
} from './state_sync';
export { Configurable, CollectConfigProps } from './ui';
export { removeQueryParam, redirectWhenMissing } from './history';
export { removeQueryParam, redirectWhenMissing, getQueryParams } from './history';
export { applyDiff } from './state_management/utils/diff_object';
export { createStartServicesGetter, StartServicesGetter } from './core/create_start_service_getter';

Expand Down

0 comments on commit 26146f3

Please sign in to comment.