import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { PublicVocabularyConverter , TranslationConverter , } from '@ulangi/ulangi-common/converters' ; import { ErrorBag , PublicVocabulary , TranslationWithLanguages , Vocabulary , } from '@ulangi/ulangi-common/interfaces' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import { ObservableSetStore } from '@ulangi/ulangi-observable' ; import { RemoteLogger } from '../../RemoteLogger' ; export class AddVocabularyDelegate { private publicVocabularyConverter = new PublicVocabularyConverter ( ) ; private translationConverter = new TranslationConverter ( ) ; private eventBus : EventBus ; private setStore : ObservableSetStore ; public constructor ( eventBus , setStore ) { this . eventBus = eventBus ; this . setStore = setStore ; } public addVocabularyFromPublicVocabulary ( publicVocabulary , categoryName , checkDuplicate , callback ) { RemoteLogger . logEvent ( 'add_vocab_from_dictionary' ) ; const vocabulary = this . publicVocabularyConverter . convertToVocabulary ( publicVocabulary , categoryName ) ; this . eventBus . pubsub ( createAction ( ActionType . VOCABULARY__ADD , { vocabulary , setId : this . setStore . existingCurrentSetId , checkDuplicate , } ) , group ( on ( ActionType . VOCABULARY__ADDING , callback . onAdding ) , once ( ActionType . VOCABULARY__ADD_SUCCEEDED , callback . onAddSucceeded ) , once ( ActionType . VOCABULARY__ADD_FAILED , ( errorBag ) : void => callback . onAddFailed ( errorBag ) ) ) ) ; } public addVocabularyFromPublicVocabularyList ( vocabularyList , categoryName , ignoreDuplicates , callback ) { RemoteLogger . logEvent ( 'add_vocab_from_list' ) ; const newVocabularyList = vocabularyList . map ( ( vocabulary ) : Vocabulary => { return this . publicVocabularyConverter . convertToVocabulary ( { ... vocabulary , } , categoryName ) ; } ) ; this . eventBus . pubsub ( createAction ( ActionType . VOCABULARY__ADD_MULTIPLE , { vocabularyList : newVocabularyList , setId : this . setStore . existingCurrentSetId , ignoreDuplicates , } ) , group ( on ( ActionType . VOCABULARY__ADDING_MULTIPLE , callback . onAddingAll ) , once ( ActionType . VOCABULARY__ADD_MULTIPLE_SUCCEEDED , callback . onAddAllSucceeded ) , once ( ActionType . VOCABULARY__ADD_MULTIPLE_FAILED , ( errorBag ) : void => callback . onAddAllFailed ( errorBag ) ) ) ) ; } public addVocabularyFromTranslation ( translationWithLanguages , checkDuplicate , callback ) { RemoteLogger . logEvent ( 'add_vocab_from_translation' ) ; const translation = this . translationConverter . convertToTranslation ( translationWithLanguages , this . setStore . existingCurrentSet . learningLanguageCode , this . setStore . existingCurrentSet . translatedToLanguageCode ) ; const newVocabulary = this . translationConverter . convertToVocabulary ( translation ) ; this . eventBus . pubsub ( createAction ( ActionType . VOCABULARY__ADD , { vocabulary : newVocabulary , setId : this . setStore . existingCurrentSetId , checkDuplicate , } ) , group ( on ( ActionType . VOCABULARY__ADDING , callback . onAdding ) , once ( ActionType . VOCABULARY__ADD_SUCCEEDED , callback . onAddSucceeded ) , once ( ActionType . VOCABULARY__ADD_FAILED , ( errorBag ) : void => callback . onAddFailed ( errorBag ) ) ) ) ; } }