import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { ActivityState } from '@ulangi/ulangi-common/enums' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import { ObservableConverter , ObservablePublicVocabulary , ObservablePublicVocabularyListState , ObservableSetStore , mergeList , } from '@ulangi/ulangi-observable' ; import { runInAction } from 'mobx' ; import { RemoteLogger } from '../../RemoteLogger' ; export class PublicVocabularyListDelegate { private eventBus : EventBus ; private observableConverter : ObservableConverter ; private setStore : ObservableSetStore ; private vocabularyListState : ObservablePublicVocabularyListState ; public constructor ( eventBus , observableConverter , setStore , vocabularyListState ) { this . eventBus = eventBus ; this . observableConverter = observableConverter ; this . setStore = setStore ; this . vocabularyListState = vocabularyListState ; } public prepareAndSearch ( searchTerm ) { RemoteLogger . logEvent ( 'search_dictionary' ) ; if ( this . setStore . existingCurrentSet . learningLanguageCode !== 'any' && this . setStore . existingCurrentSet . translatedToLanguageCode !== 'any' ) { this . eventBus . pubsub ( createAction ( ActionType . LIBRARY__PREPARE_SEARCH_PUBLIC_VOCABULARY , { languageCodePair : this . setStore . existingCurrentSet . languageCodePair , searchTerm , } ) , group ( on ( ActionType . LIBRARY__PREPARING_SEARCH_PUBLIC_VOCABULARY , ( ) : void => { this . vocabularyListState . searchState . set ( ActivityState . ACTIVE ) ; } ) , once ( ActionType . LIBRARY__PREPARE_SEARCH_PUBLIC_VOCABULARY_SUCCEEDED , ( ) : void => { this . vocabularyListState . searchState . set ( ActivityState . INACTIVE ) ; this . search ( ) ; } ) , once ( ActionType . LIBRARY__PREPARE_SEARCH_PUBLIC_SETS_FAILED , ( ) : void => { this . vocabularyListState . isRefreshing . set ( false ) ; this . vocabularyListState . searchState . set ( ActivityState . ERROR ) ; } ) ) ) ; } } public search ( ) { if ( ! this . vocabularyListState . noMore && this . vocabularyListState . searchState . get ( ) === ActivityState . INACTIVE ) { this . eventBus . pubsub ( createAction ( ActionType . LIBRARY__SEARCH_PUBLIC_VOCABULARY , null ) , group ( on ( ActionType . LIBRARY__SEARCHING_PUBLIC_VOCABULARY , ( ) : void => { this . vocabularyListState . searchState . set ( ActivityState . ACTIVE ) ; } ) , once ( ActionType . LIBRARY__SEARCH_PUBLIC_VOCABULARY_SUCCEEDED , ( { vocabularyList , noMore } ) : void => { this . vocabularyListState . searchState . set ( ActivityState . INACTIVE ) ; this . vocabularyListState . isRefreshing . set ( false ) ; this . vocabularyListState . noMore = noMore ; this . vocabularyListState . publicVocabularyList = mergeList ( this . vocabularyListState . publicVocabularyList , vocabularyList . map ( ( vocabulary ) : [ string , ObservablePublicVocabulary ] => { return [ vocabulary . publicVocabularyId , this . observableConverter . convertToObservablePublicVocabulary ( vocabulary ) , ] ; } ) ) ; } ) , once ( ActionType . LIBRARY__SEARCH_PUBLIC_VOCABULARY_FAILED , ( ) : void => { this . vocabularyListState . isRefreshing . set ( false ) ; this . vocabularyListState . searchState . set ( ActivityState . ERROR ) ; } ) ) ) ; } } public clearSearch ( ) { this . vocabularyListState . publicVocabularyList = null ; this . vocabularyListState . noMore = false ; this . vocabularyListState . searchState . set ( ActivityState . INACTIVE ) ; this . vocabularyListState . isRefreshing . set ( false ) ; this . eventBus . publish ( createAction ( ActionType . LIBRARY__CLEAR_SEARCH_PUBLIC_VOCABULARY , null ) ) ; } public refresh ( searchTerm ) { runInAction ( ( ) : void => { this . clearSearch ( ) ; this . vocabularyListState . isRefreshing . set ( true ) ; this . prepareAndSearch ( searchTerm ) ; } ) ; } }