From 596e9ab9d3c2b96f5703c0ca67515f330da8441e Mon Sep 17 00:00:00 2001 From: wangqianliang Date: Thu, 28 Mar 2019 17:12:58 +0800 Subject: [PATCH] fix(code/frontend): use different actions to handle repo scope search and repo search --- x-pack/plugins/code/public/actions/search.ts | 4 ++++ .../query_bar/components/query_bar.tsx | 15 ++++++++------- x-pack/plugins/code/public/reducers/search.ts | 15 +++++++++++++++ x-pack/plugins/code/public/sagas/index.ts | 8 +++++++- x-pack/plugins/code/public/sagas/search.ts | 18 +++++++++++++++++- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/code/public/actions/search.ts b/x-pack/plugins/code/public/actions/search.ts index 3e7210e09488c1..0c6aae66002384 100644 --- a/x-pack/plugins/code/public/actions/search.ts +++ b/x-pack/plugins/code/public/actions/search.ts @@ -43,3 +43,7 @@ export const repositoryTypeaheadSearchSuccess = createAction('REPOSITORY export const repositoryTypeaheadSearchFailed = createAction('REPOSITORY SEARCH FAILED'); export const saveSearchOptions = createAction('SAVE SEARCH OPTIONS'); + +export const searchReposForScope = createAction('SEARCH REPOS FOR SCOPE'); +export const searchReposForScopeSuccess = createAction('SEARCH REPOS FOR SCOPE SUCCESS'); +export const searchReposForScopeFailed = createAction('SEARCH REPOS FOR SCOPE FAILED'); diff --git a/x-pack/plugins/code/public/components/query_bar/components/query_bar.tsx b/x-pack/plugins/code/public/components/query_bar/components/query_bar.tsx index a3698d0c751114..56bd2394b08468 100644 --- a/x-pack/plugins/code/public/components/query_bar/components/query_bar.tsx +++ b/x-pack/plugins/code/public/components/query_bar/components/query_bar.tsx @@ -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 { @@ -485,16 +488,14 @@ export class CodeQueryBar extends Component { } 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, }; diff --git a/x-pack/plugins/code/public/reducers/search.ts b/x-pack/plugins/code/public/reducers/search.ts index 155aaaba7d9afc..17eefa3386dfa9 100644 --- a/x-pack/plugins/code/public/reducers/search.ts +++ b/x-pack/plugins/code/public/reducers/search.ts @@ -21,6 +21,8 @@ import { repositorySearchSuccess, saveSearchOptions, SearchOptions, + searchReposForScope, + searchReposForScopeSuccess, } from '../actions'; export interface SearchState { @@ -30,17 +32,21 @@ export interface SearchState { languages?: Set; repositories?: Set; 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( @@ -159,6 +165,15 @@ export const search = handleActions( produce(state, draft => { draft.searchOptions = action.payload; }), + [String(searchReposForScope)]: (state: SearchState, action: Action) => + produce(state, draft => { + draft.isScopeSearchLoading = true; + }), + [String(searchReposForScopeSuccess)]: (state: SearchState, action: Action) => + produce(state, draft => { + draft.scopeSearchResults = action.payload; + draft.isScopeSearchLoading = false; + }), }, initialState ); diff --git a/x-pack/plugins/code/public/sagas/index.ts b/x-pack/plugins/code/public/sagas/index.ts index ee777c0ab1a30b..e6b43cc3d4b7b6 100644 --- a/x-pack/plugins/code/public/sagas/index.ts +++ b/x-pack/plugins/code/public/sagas/index.ts @@ -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'; @@ -77,4 +82,5 @@ export function* rootSaga() { yield fork(watchRepoDeleteStatusPolling); yield fork(watchRepoIndexStatusPolling); yield fork(watchRepoCloneStatusPolling); + yield fork(watchRepoScopeSearch); } diff --git a/x-pack/plugins/code/public/sagas/search.ts b/x-pack/plugins/code/public/sagas/search.ts index 8e3b851ae97020..03dc71112ded96 100644 --- a/x-pack/plugins/code/public/sagas/search.ts +++ b/x-pack/plugins/code/public/sagas/search.ts @@ -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'; @@ -22,6 +22,9 @@ import { RepositorySearchPayload, repositorySearchQueryChanged, repositorySearchSuccess, + searchReposForScope, + searchReposForScopeFailed, + searchReposForScopeSuccess, } from '../actions'; import { searchRoutePattern } from './patterns'; @@ -123,3 +126,16 @@ function* handleSearchRouteChange(action: Action) { export function* watchSearchRouteChange() { yield takeLatest(searchRoutePattern, handleSearchRouteChange); } + +function* handleReposSearchForScope(action: Action) { + 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); +}