import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { ActivityState , VocabularySortType , VocabularyStatus , } from '@ulangi/ulangi-common/enums' ; import { VocabularyFilterCondition } from '@ulangi/ulangi-common/types' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import { ObservableConverter , ObservableSetStore , ObservableVocabulary , ObservableVocabularyListState , mergeList , } from '@ulangi/ulangi-observable' ; export class VocabularyListDelegate { private eventBus : EventBus ; private setStore : ObservableSetStore ; private observableConverter : ObservableConverter ; private vocabularyListState : ObservableVocabularyListState ; public constructor ( eventBus , setStore , observableConverter , vocabularyListState ) { this . eventBus = eventBus ; this . setStore = setStore ; this . observableConverter = observableConverter ; this . vocabularyListState = vocabularyListState ; } public prepareAndFetch ( vocabularyStatus , sortType , categoryNames ? ) { this . eventBus . pubsub ( createAction ( ActionType . VOCABULARY__PREPARE_FETCH , { filterCondition : this . createFilterCondition ( vocabularyStatus , categoryNames ) , sortType , } ) , group ( on ( ActionType . VOCABULARY__PREPARING_FETCH , ( ) : void => { this . vocabularyListState . fetchState . set ( ActivityState . ACTIVE ) ; } ) , once ( ActionType . VOCABULARY__PREPARE_FETCH_SUCCEEDED , ( ) : void => { this . vocabularyListState . fetchState . set ( ActivityState . INACTIVE ) ; this . fetch ( ) ; } ) , once ( ActionType . VOCABULARY__PREPARE_FETCH_FAILED , ( ) : void => { this . vocabularyListState . fetchState . set ( ActivityState . ERROR ) ; } ) ) ) ; } public refresh ( vocabularyStatus , sortType , categoryNames ? ) { this . vocabularyListState . isRefreshing . set ( true ) ; this . vocabularyListState . shouldShowRefreshNotice . set ( false ) ; this . clearFetch ( ) ; this . prepareAndFetch ( vocabularyStatus , sortType , categoryNames ) ; } public fetch ( ) { if ( this . vocabularyListState . noMore === false && this . vocabularyListState . fetchState . get ( ) === ActivityState . INACTIVE ) { this . vocabularyListState . fetchState . set ( ActivityState . ACTIVE ) ; this . eventBus . pubsub ( createAction ( ActionType . VOCABULARY__FETCH , null ) , group ( once ( ActionType . VOCABULARY__FETCH_SUCCEEDED , ( { vocabularyList , noMore } ) : void => { this . vocabularyListState . fetchState . set ( ActivityState . INACTIVE ) ; this . vocabularyListState . isRefreshing . set ( false ) ; this . vocabularyListState . noMore = noMore ; this . vocabularyListState . vocabularyList = mergeList ( this . vocabularyListState . vocabularyList , vocabularyList . map ( ( vocabulary ) : [ string , ObservableVocabulary ] => [ vocabulary . vocabularyId , this . observableConverter . convertToObservableVocabulary ( vocabulary ) , ] ) ) ; } ) , once ( ActionType . VOCABULARY__FETCH_FAILED , ( ) : void => { this . vocabularyListState . fetchState . set ( ActivityState . ERROR ) ; this . vocabularyListState . isRefreshing . set ( false ) ; } ) ) ) ; } } public clearFetch ( ) { this . vocabularyListState . fetchState . set ( ActivityState . INACTIVE ) ; this . vocabularyListState . vocabularyList = null ; this . vocabularyListState . noMore = false ; this . eventBus . publish ( createAction ( ActionType . VOCABULARY__CLEAR_FETCH , null ) ) ; } public refreshIfEmpty ( vocabularyStatus , sortType , categoryNames ? ) { if ( this . vocabularyListState . vocabularyList !== null && this . vocabularyListState . vocabularyList . size === 0 ) { this . refresh ( vocabularyStatus , sortType , categoryNames ) ; } } private createFilterCondition ( vocabularyStatus , categoryNames ? ) { return { filterBy : 'VocabularyStatus' , setId : this . setStore . existingCurrentSetId , vocabularyStatus : vocabularyStatus , categoryNames , } ; } }