import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { ActivityState } from '@ulangi/ulangi-common/enums' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import { ObservableCategoryFormState , ObservableSetStore , Observer , } from '@ulangi/ulangi-observable' ; import * as _ from 'lodash' ; import { observable , runInAction } from 'mobx' ; export class CategoryFormDelegate { private eventBus : EventBus ; private observer : Observer ; private setStore : ObservableSetStore ; private categoryFormState : ObservableCategoryFormState ; public constructor ( eventBus , observer , setStore , categoryFormState ) { this . eventBus = eventBus ; this . observer = observer ; this . setStore = setStore ; this . categoryFormState = categoryFormState ; } public handleInputChange ( searchInput ) { this . categoryFormState . searchInput = searchInput ; } public clearFetchSuggestions ( ) { this . categoryFormState . fetchSuggestionsState . set ( ActivityState . INACTIVE ) ; this . categoryFormState . suggestions = null ; this . categoryFormState . noMoreSuggestions = false ; this . eventBus . publish ( createAction ( ActionType . CATEGORY__CLEAR_FETCH_SUGGESTIONS , null ) ) ; } public clear ( ) { this . categoryFormState . searchInput = '' ; this . clearFetchSuggestions ( ) ; this . prepareAndFetchSuggestions ( '' ) ; } public prepareAndFetchSuggestions ( term ) { this . eventBus . pubsub ( createAction ( ActionType . CATEGORY__PREPARE_FETCH_SUGGESTIONS , { setId : this . setStore . existingCurrentSetId , term , } ) , group ( on ( ActionType . CATEGORY__PREPARING_FETCH_SUGGESTIONS , ( ) : void => { this . categoryFormState . fetchSuggestionsState . set ( ActivityState . ACTIVE ) ; } ) , once ( ActionType . CATEGORY__PREPARE_FETCH_SUGGESTIONS_SUCCEEDED , ( ) : void => { this . categoryFormState . fetchSuggestionsState . set ( ActivityState . INACTIVE ) ; this . fetchSuggestions ( ) ; } ) , once ( ActionType . CATEGORY__PREPARE_FETCH_SUGGESTIONS_FAILED , ( ) : void => { this . categoryFormState . fetchSuggestionsState . set ( ActivityState . INACTIVE ) ; } ) , once ( ActionType . CATEGORY__CLEAR_FETCH_SUGGESTIONS , _ . noop ) ) ) ; } public fetchSuggestions ( ) { if ( this . categoryFormState . noMoreSuggestions === false && this . categoryFormState . fetchSuggestionsState . get ( ) === ActivityState . INACTIVE ) { this . categoryFormState . fetchSuggestionsState . set ( ActivityState . ACTIVE ) ; this . eventBus . pubsub ( createAction ( ActionType . CATEGORY__FETCH_SUGGESTIONS , null ) , group ( once ( ActionType . CATEGORY__FETCH_SUGGESTIONS_SUCCEEDED , ( { suggestions , noMore } ) : void => { runInAction ( ( ) : void => { this . categoryFormState . fetchSuggestionsState . set ( ActivityState . INACTIVE ) ; this . categoryFormState . noMoreSuggestions = noMore ; if ( this . categoryFormState . suggestions === null ) { this . categoryFormState . suggestions = observable . array ( ) ; } this . categoryFormState . suggestions . push ( ... suggestions . slice ( ) ) ; } ) ; } ) , once ( ActionType . CATEGORY__FETCH_SUGGESTIONS_FAILED , ( ) : void => { this . categoryFormState . fetchSuggestionsState . set ( ActivityState . INACTIVE ) ; } ) , once ( ActionType . CATEGORY__CLEAR_FETCH_SUGGESTIONS , _ . noop ) ) ) ; } } public autoRefreshCategorySuggestionsOnInputChange ( debounceTime ) { const debouncedPrepareAndFetchCategorySuggestions = _ . debounce ( ( ) : void => { this . prepareAndFetchSuggestions ( this . categoryFormState . searchInput ) ; } , debounceTime ) ; this . observer . reaction ( ( ) : undefined | string => this . categoryFormState . searchInput , ( ) : void => { this . clearFetchSuggestions ( ) ; debouncedPrepareAndFetchCategorySuggestions ( ) ; } ) ; } }