import { IWebPartContext } from "@microsoft/sp-webpart-base" ; import { SPHttpClient } from "@microsoft/sp-http" ; import { EventEmitter } from "fbemitter" ; import { ICells , ICellValue } from "../../utils/ISearchResults" ; import appDispatcher from "../dispatcher/appDispatcher" ; import searchActionIDs from "../actions/searchActionIDs" ; import SearchTokenHelper from "../helpers/SearchTokenHelper" ; const CHANGE_EVENT = "change" ; export class SearchStoreStatic extends EventEmitter { private _results : any [ ] = [ ] ; private _url : string ; private _response : any ; param { function } callback public addChangeListener ( callback ) { this . addListener ( CHANGE_EVENT , callback ) ; } param { function } callback public removeChangeListener ( callback ) { this . removeCurrentListener ( ) ; } public emitChange ( ) { this . emit ( CHANGE_EVENT ) ; } public getSearchResults ( ) { return this . _results ; } public setSearchResults ( crntResults , fields ) { if ( crntResults . length > 0 ) { const flds = fields . toLowerCase ( ) . split ( "," ) ; const temp = [ ] ; crntResults . forEach ( ( result ) => { var val = { } ; result . Cells . forEach ( ( cell ) => { if ( flds . indexOf ( cell . Key . toLowerCase ( ) ) !== - 1 ) { val [ cell . Key ] = cell . Value ; } } ) ; temp . push ( val ) ; } ) ; this . _results = temp ; } else { this . _results = [ ] ; } } param { IWebPartContext } context param { string } url public GetSearchData ( context , url ) { return context . spHttpClient . get ( url , SPHttpClient . configurations . v1 ) . then ( ( res ) => { return res . json ( ) ; } ) ; } param { string } value public isEmptyString ( value ) { return value === null || typeof value === "undefined" || ! value . length ; } param { any } value public isNull ( value ) { return value === null || typeof value === "undefined" ; } public setLoggingInfo ( url , response ) { this . _url = url ; this . _response = response ; } public getLoggingInfo ( ) { return { URL : this . _url , Response : this . _response } ; } } const searchStore = new SearchStoreStatic ( ) ; appDispatcher . register ( ( action ) => { switch ( action . actionType ) { case searchActionIDs . SEARCH_GET : debugger ; const tokenHelper = new SearchTokenHelper ( ) ; let url = action . context . pageContext . web . absoluteUrl + "/_api/search/query?querytext=" ; url += ! searchStore . isEmptyString ( action . query ) ? `'${ tokenHelper . replaceTokens ( action . query , action . context ) }'` : "'*'" ; url += '&selectproperties=' ; url += ! searchStore . isEmptyString ( action . fields ) ? `'${ action . fields }'` : "'path,title,ModifiedOWSDATE,EditorOWSUSER,Fileextension'" ; url += "&rowlimit=" ; url += ! searchStore . isNull ( action . maxResults ) ? action . maxResults : 10 ; url += "&clienttype='ContentSearchRegular'" ; searchStore . GetSearchData ( action . context , url ) . then ( ( res ) => { searchStore . setLoggingInfo ( url , res ) ; let resultsRetrieved = false ; if ( res !== null ) { if ( typeof res . PrimaryQueryResult !== 'undefined' ) { if ( typeof res . PrimaryQueryResult . RelevantResults !== 'undefined' ) { if ( typeof res . PrimaryQueryResult . RelevantResults !== 'undefined' ) { if ( typeof res . PrimaryQueryResult . RelevantResults . Table !== 'undefined' ) { if ( typeof res . PrimaryQueryResult . RelevantResults . Table . Rows !== 'undefined' ) { if ( res . PrimaryQueryResult . RelevantResults . Table . Rows . length > 0 ) { resultsRetrieved = true ; searchStore . setSearchResults ( res . PrimaryQueryResult . RelevantResults . Table . Rows , "path,title,ModifiedOWSDATE,EditorOWSUSER,Fileextension" ) ; } } } } } } } if ( ! resultsRetrieved ) { searchStore . setSearchResults ( [ ] , null ) ; } searchStore . emitChange ( ) ; } ) ; break ; } } ) ; export default searchStore ;