import { kea , MakeLogicType } from 'kea' ; import { i18n } from '@kbn/i18n' ; import { flashAPIErrors } from '../../../../shared/flash_messages' ; import { HttpLogic } from '../../../../shared/http' ; import { EngineLogic } from '../../engine' ; import { FieldValue } from '../../result/types' ; import { SampleSearchResponse , ServerFieldResultSettingObject } from '../types' ; const NO_RESULTS_MESSAGE = i18n . translate ( 'xpack.enterpriseSearch.appSearch.engine.resultSettings.sampleResponse.noResultsMessage' , { defaultMessage : 'No results.' } ) ; const ERROR_MESSAGE = i18n . translate ( 'xpack.enterpriseSearch.appSearch.engine.resultSettings.sampleResponse.errorMessage' , { defaultMessage : 'An error occured.' } ) ; interface SampleResponseValues { query : string ; response : SampleSearchResponse | string | null ; } interface SampleResponseActions { queryChanged : ( query : string ) => { query : string ; } ; getSearchResultsSuccess : ( response : SampleSearchResponse | string ) => { response : SampleSearchResponse | string ; } ; getSearchResultsFailure : ( response : string ) => { response : string ; } ; getSearchResults : ( query : string , resultFields : ServerFieldResultSettingObject ) => { query : string ; resultFields : ServerFieldResultSettingObject ; } ; } export const SampleResponseLogic = kea < MakeLogicType < SampleResponseValues , SampleResponseActions > > ( { path : [ 'enterprise_search' , 'app_search' , 'sample_response_logic' ] , actions : { queryChanged : ( query ) => ( { query } ) , getSearchResultsSuccess : ( response ) => ( { response } ) , getSearchResultsFailure : ( response ) => ( { response } ) , getSearchResults : ( query , resultFields ) => ( { query , resultFields } ) , } , reducers : { query : [ '' , { queryChanged : ( _ , { query } ) => query } ] , response : [ null , { getSearchResultsSuccess : ( _ , { response } ) => response , getSearchResultsFailure : ( _ , { response } ) => response , } , ] , } , listeners : ( { actions } ) => ( { getSearchResults : async ( { query , resultFields } , breakpoint ) => { await breakpoint ( 250 ) ; const { http } = HttpLogic . values ; const { engineName } = EngineLogic . values ; const url = `/internal/app_search/engines/${ engineName }/search` ; try { const response = await http . post < { results : Array < Record < string , FieldValue > > ; } > ( url , { query : { query } , body : JSON . stringify ( { page : { size : 1 , current : 1 , } , result_fields : resultFields , } ) , } ) ; const result = response . results ?. [ 0 ] ; actions . getSearchResultsSuccess ( result ? { ... result , _meta : undefined } : NO_RESULTS_MESSAGE ) ; } catch ( e ) { if ( e . response . status >= 500 ) { flashAPIErrors ( e ) ; actions . getSearchResultsFailure ( ERROR_MESSAGE ) ; } else { actions . getSearchResultsFailure ( e . body ?. attributes || ERROR_MESSAGE ) ; } } } , } ) , } ) ;