From f94d058d1db16055152eeb82609a274a44ffda40 Mon Sep 17 00:00:00 2001 From: WangQianliang Date: Sun, 21 Jul 2019 18:45:06 +0800 Subject: [PATCH] fix(code/frontend): should update search results if search options change (#41232) --- .../query_bar/components/query_bar.tsx | 4 +++- .../components/search_bar/search_bar.tsx | 19 +++++++-------- .../plugins/code/public/reducers/search.ts | 24 +++++++++++++++++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/x-pack/legacy/plugins/code/public/components/query_bar/components/query_bar.tsx b/x-pack/legacy/plugins/code/public/components/query_bar/components/query_bar.tsx index e192ddb0d5370..d267d6d73e7fc 100644 --- a/x-pack/legacy/plugins/code/public/components/query_bar/components/query_bar.tsx +++ b/x-pack/legacy/plugins/code/public/components/query_bar/components/query_bar.tsx @@ -468,7 +468,9 @@ export class CodeQueryBar extends Component { /> { // Update the url and push to history as well. const previousQueries = querystring.parse(history.location.search.replace('?', '')); - const queries: any = - repoScopes.length === 0 - ? { - ...previousQueries, - q: query, - } - : { - ...previousQueries, - q: query, - repoScope: repoScopes, - }; + const queries: any = { + ...previousQueries, + repoScope: repoScopes, + q: query, + }; + if (repoScopes.length === 0) { + delete queries.repoScope; + } history.push( url.format({ pathname: '/search', diff --git a/x-pack/legacy/plugins/code/public/reducers/search.ts b/x-pack/legacy/plugins/code/public/reducers/search.ts index fb1343cb866d6..6f693e0c430bf 100644 --- a/x-pack/legacy/plugins/code/public/reducers/search.ts +++ b/x-pack/legacy/plugins/code/public/reducers/search.ts @@ -5,8 +5,9 @@ */ import produce from 'immer'; - +import querystring from 'querystring'; import { Action, handleActions } from 'redux-actions'; +import { history } from '../utils/url'; import { DocumentSearchResult, @@ -33,6 +34,7 @@ import { turnOffDefaultRepoScope, turnOnDefaultRepoScope, } from '../actions'; +import { RepositoryUtils } from '../../common/repository_utils'; export interface SearchState { scope: SearchScope; @@ -51,12 +53,30 @@ export interface SearchState { const repositories: Repository[] = []; +const getRepoScopeFromUrl = () => { + const { repoScope } = querystring.parse(history.location.search.replace('?', '')); + if (repoScope) { + return String(repoScope) + .split(',') + .map(r => ({ + uri: r, + org: RepositoryUtils.orgNameFromUri(r), + name: RepositoryUtils.repoNameFromUri(r), + })) as Repository[]; + } else { + return []; + } +}; + const initialState: SearchState = { query: '', isLoading: false, isScopeSearchLoading: false, scope: SearchScope.DEFAULT, - searchOptions: { repoScope: [], defaultRepoScopeOn: false }, + searchOptions: { + repoScope: getRepoScopeFromUrl(), + defaultRepoScopeOn: false, + }, scopeSearchResults: { repositories, total: 0, took: 0 }, };