import { DefinitionBuilder } from '@ulangi/ulangi-common/builders' ; import { ButtonSize , ErrorCode } from '@ulangi/ulangi-common/enums' ; import { ButtonStyles , ErrorBag } from '@ulangi/ulangi-common/interfaces' ; import { EventBus } from '@ulangi/ulangi-event' ; import { ObservableConverter , ObservableVocabulary , ObservableVocabularyFormState , } from '@ulangi/ulangi-observable' ; import { boundClass } from 'autobind-decorator' ; import { runInAction } from 'mobx' ; import { RemoteLogger } from '../../RemoteLogger' ; import { AddVocabularyScreenIds } from '../../constants/ids/AddVocabularyScreenIds' ; import { LightBoxDialogIds } from '../../constants/ids/LightBoxDialogIds' ; import { fullRoundedButtonStyles } from '../../styles/FullRoundedButtonStyles' ; import { DialogDelegate } from '../dialog/DialogDelegate' ; import { NavigatorDelegate } from '../navigator/NavigatorDelegate' ; import { AddEditVocabularyScreenDelegate } from './AddEditVocabularyScreenDelegate' ; import { AddVocabularyDelegate } from './AddVocabularyDelegate' ; import { VocabularyFormDelegate } from './VocabularyFormDelegate' ; @ boundClass export class AddVocabularyScreenDelegate extends AddEditVocabularyScreenDelegate { private observableConverter : ObservableConverter ; private addVocabularyDelegate : AddVocabularyDelegate ; private vocabularyFormState : ObservableVocabularyFormState ; public constructor ( eventBus , observableConverter , vocabularyFormState , vocabularyInputDelegate , addVocabularyDelegate , dialogDelegate , navigatorDelegate ) { super ( eventBus , vocabularyInputDelegate , dialogDelegate , navigatorDelegate ) ; this . observableConverter = observableConverter ; this . addVocabularyDelegate = addVocabularyDelegate ; this . vocabularyFormState = vocabularyFormState ; } public saveAdd ( checkDuplicate , closeOnSaveSucceeded ) { RemoteLogger . logEvent ( 'add_vocabulary' ) ; this . addVocabularyDelegate . saveAdd ( checkDuplicate , { onSaving : this . showSavingDialog , onSaveSucceeded : closeOnSaveSucceeded ? this . showSaveSucceededDialog : this . showWhatToDoNextDialog , onSaveFailed : ( errorBag ) : void => { this . showSaveFailedDialogWithRetry ( errorBag , ( shouldCheckDuplicate ) : void => { this . saveAdd ( shouldCheckDuplicate , closeOnSaveSucceeded ) ; } ) ; } , } ) ; } public createPreview ( ) { return this . addVocabularyDelegate . createPreview ( ) ; } private showSaveFailedDialogWithRetry ( errorBag , retry ) { if ( errorBag . errorCode === ErrorCode . VOCABULARY__DUPLICATE_TERM ) { this . dialogDelegate . show ( { testID : LightBoxDialogIds . SUCCESS_DIALOG , message : 'You have added this term before. Do you want to add it again?' , onBackgroundPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; } , buttonList : [ { testID : LightBoxDialogIds . CANCEL_BTN , text : 'NO' , onPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidPrimaryBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , { testID : LightBoxDialogIds . OKAY_BTN , text : 'YES' , onPress : ( ) : void => { retry ( false ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidGreyBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , ] , } ) ; } else { this . showSaveFailedDialog ( errorBag ) ; } } private showWhatToDoNextDialog ( ) { this . dialogDelegate . show ( { testID : LightBoxDialogIds . SUCCESS_DIALOG , message : 'Saved successfully. What do you want to do next?' , onBackgroundPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; this . navigatorDelegate . dismissScreen ( ) ; } , buttonList : [ { testID : LightBoxDialogIds . CLOSE_DIALOG_BTN , text : 'CLOSE' , onPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; this . navigatorDelegate . dismissScreen ( ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidGreyBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , { testID : AddVocabularyScreenIds . ADD_MORE_BTN , text : 'ADD MORE' , onPress : ( ) : void => { this . resetForms ( ) ; this . dialogDelegate . dismiss ( ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidPrimaryBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , ] , } ) ; } private resetForms ( ) { runInAction ( ( ) : void => { this . vocabularyFormState . reset ( ) ; this . vocabularyFormState . definitions . push ( this . observableConverter . convertToObservableDefinition ( new DefinitionBuilder ( ) . build ( { source : 'N/A' } ) ) ) ; } ) ; } }