Skip to content

Commit

Permalink
fix(code/frontend): should update search results if search options ch…
Browse files Browse the repository at this point in the history
…ange (#41232) (#41646)
  • Loading branch information
WangQianliang authored Jul 22, 2019
1 parent c321764 commit 7573007
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ export class CodeQueryBar extends Component<Props, State> {
/>
<SearchOptions
defaultRepoOptions={this.props.defaultRepoOptions}
defaultSearchScope={this.props.currentRepository}
defaultSearchScope={
this.props.currentRepository || this.props.searchOptions.defaultRepoScope
}
repositorySearch={this.props.repositorySearch}
saveSearchOptions={this.props.saveSearchOptions}
repoSearchResults={this.props.repoSearchResults}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@ export class SearchBar extends React.PureComponent<Props> {

// 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',
Expand Down
24 changes: 22 additions & 2 deletions x-pack/legacy/plugins/code/public/reducers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,6 +34,7 @@ import {
turnOffDefaultRepoScope,
turnOnDefaultRepoScope,
} from '../actions';
import { RepositoryUtils } from '../../common/repository_utils';

export interface SearchState {
scope: SearchScope;
Expand All @@ -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 },
};

Expand Down

0 comments on commit 7573007

Please sign in to comment.