Skip to content

Commit

Permalink
fix(code/frontend): use different actions to handle repo scope search…
Browse files Browse the repository at this point in the history
… and repo search (#34043)
  • Loading branch information
WangQianliang authored Apr 3, 2019
1 parent cb08eb9 commit 8132915
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/code/public/actions/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ export const repositoryTypeaheadSearchSuccess = createAction<string>('REPOSITORY
export const repositoryTypeaheadSearchFailed = createAction<string>('REPOSITORY SEARCH FAILED');

export const saveSearchOptions = createAction<SearchOptions>('SAVE SEARCH OPTIONS');

export const searchReposForScope = createAction<RepositorySearchPayload>('SEARCH REPOS FOR SCOPE');
export const searchReposForScopeSuccess = createAction<any>('SEARCH REPOS FOR SCOPE SUCCESS');
export const searchReposForScopeFailed = createAction<any>('SEARCH REPOS FOR SCOPE FAILED');
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import { debounce, isEqual } from 'lodash';
import React, { Component } from 'react';

import { SearchOptions as ISearchOptions } from '../../../actions';
import {
saveSearchOptions,
SearchOptions as ISearchOptions,
searchReposForScope,
} from '../../../actions';
import { matchPairs } from '../lib/match_pairs';
import { SuggestionsComponent } from './typeahead/suggestions_component';

import { EuiFieldText, EuiFlexGroup, EuiFlexItem, EuiOutsideClickDetector } from '@elastic/eui';
import { connect } from 'react-redux';
import { SearchScope } from '../../../../model';
import { repositorySearch, saveSearchOptions } from '../../../actions';
import { SearchScopePlaceholderText } from '../../../common/types';
import { RootState } from '../../../reducers';
import {
Expand Down Expand Up @@ -485,16 +488,14 @@ export class CodeQueryBar extends Component<Props, State> {
}

const mapStateToProps = (state: RootState) => ({
repoSearchResults: state.search.repositorySearchResults
? state.search.repositorySearchResults.repositories
: [],
searchLoading: state.search.isLoading,
repoSearchResults: state.search.scopeSearchResults.repositories,
searchLoading: state.search.isScopeSearchLoading,
searchScope: state.search.scope,
searchOptions: state.search.searchOptions,
});

const mapDispatchToProps = {
repositorySearch,
repositorySearch: searchReposForScope,
saveSearchOptions,
};

Expand Down
15 changes: 15 additions & 0 deletions x-pack/plugins/code/public/reducers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
repositorySearchSuccess,
saveSearchOptions,
SearchOptions,
searchReposForScope,
searchReposForScopeSuccess,
} from '../actions';

export interface SearchState {
Expand All @@ -30,17 +32,21 @@ export interface SearchState {
languages?: Set<string>;
repositories?: Set<RepositoryUri>;
isLoading: boolean;
isScopeSearchLoading: boolean;
error?: Error;
documentSearchResults?: DocumentSearchResult;
repositorySearchResults?: any;
searchOptions: SearchOptions;
scopeSearchResults: { repositories: any[] };
}

const initialState: SearchState = {
query: '',
isLoading: false,
isScopeSearchLoading: false,
scope: SearchScope.DEFAULT,
searchOptions: { repoScope: [] },
scopeSearchResults: { repositories: [] },
};

export const search = handleActions<SearchState, any>(
Expand Down Expand Up @@ -159,6 +165,15 @@ export const search = handleActions<SearchState, any>(
produce<SearchState>(state, draft => {
draft.searchOptions = action.payload;
}),
[String(searchReposForScope)]: (state: SearchState, action: Action<RepositorySearchPayload>) =>
produce<SearchState>(state, draft => {
draft.isScopeSearchLoading = true;
}),
[String(searchReposForScopeSuccess)]: (state: SearchState, action: Action<any>) =>
produce<SearchState>(state, draft => {
draft.scopeSearchResults = action.payload;
draft.isScopeSearchLoading = false;
}),
},
initialState
);
8 changes: 7 additions & 1 deletion x-pack/plugins/code/public/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ import {
watchIndexRepo,
watchInitRepoCmd,
} from './repository';
import { watchDocumentSearch, watchRepositorySearch, watchSearchRouteChange } from './search';
import {
watchDocumentSearch,
watchRepoScopeSearch,
watchRepositorySearch,
watchSearchRouteChange,
} from './search';
import { watchRootRoute } from './setup';
import { watchRepoCloneSuccess, watchRepoDeleteFinished } from './status';
import { watchLoadStructure } from './structure';
Expand Down Expand Up @@ -77,4 +82,5 @@ export function* rootSaga() {
yield fork(watchRepoDeleteStatusPolling);
yield fork(watchRepoIndexStatusPolling);
yield fork(watchRepoCloneStatusPolling);
yield fork(watchRepoScopeSearch);
}
18 changes: 17 additions & 1 deletion x-pack/plugins/code/public/sagas/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import queryString from 'querystring';
import { call, put, takeLatest } from 'redux-saga/effects';
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects';
import { kfetch } from 'ui/kfetch';

import { Action } from 'redux-actions';
Expand All @@ -22,6 +22,9 @@ import {
RepositorySearchPayload,
repositorySearchQueryChanged,
repositorySearchSuccess,
searchReposForScope,
searchReposForScopeFailed,
searchReposForScopeSuccess,
} from '../actions';
import { searchRoutePattern } from './patterns';

Expand Down Expand Up @@ -123,3 +126,16 @@ function* handleSearchRouteChange(action: Action<Match>) {
export function* watchSearchRouteChange() {
yield takeLatest(searchRoutePattern, handleSearchRouteChange);
}

function* handleReposSearchForScope(action: Action<RepositorySearchPayload>) {
try {
const data = yield call(requestRepositorySearch, action.payload!.query);
yield put(searchReposForScopeSuccess(data));
} catch (err) {
yield put(searchReposForScopeFailed(err));
}
}

export function* watchRepoScopeSearch() {
yield takeEvery(searchReposForScope, handleReposSearchForScope);
}

0 comments on commit 8132915

Please sign in to comment.