import { kea , MakeLogicType } from 'kea' ; import { flashAPIErrors } from '../../../../shared/flash_messages' ; import { HttpLogic } from '../../../../shared/http' ; import { EngineLogic } from '../../engine' ; import { ReindexJobApiResponse } from '../types' ; export interface ReindexJobValues { dataLoading : boolean ; fieldCoercionErrors : ReindexJobApiResponse [ 'fieldCoercionErrors' ] ; } export interface ReindexJobActions { loadReindexJob ( id ) : string ; onLoadSuccess ( response ) : ReindexJobApiResponse ; onLoadError ( ) : void ; } export const ReindexJobLogic = kea < MakeLogicType < ReindexJobValues , ReindexJobActions > > ( { path : [ 'enterprise_search' , 'app_search' , 'reindex_job_logic' ] , actions : { loadReindexJob : ( id ) => id , onLoadSuccess : ( response ) => response , onLoadError : true , } , reducers : { dataLoading : [ true , { loadReindexJob : ( ) => true , onLoadSuccess : ( ) => false , onLoadError : ( ) => false , } , ] , fieldCoercionErrors : [ { } , { onLoadSuccess : ( _ , { fieldCoercionErrors } ) => fieldCoercionErrors , } , ] , } , listeners : ( { actions } ) => ( { loadReindexJob : async ( id ) => { const { http } = HttpLogic . values ; const { engineName } = EngineLogic . values ; try { const response = await http . get < ReindexJobApiResponse > ( `/internal/app_search/engines/${ engineName }/reindex_job/${ id }` ) ; actions . onLoadSuccess ( response ) ; } catch ( e ) { flashAPIErrors ( e ) ; actions . onLoadError ( ) ; } } , } ) , } ) ;