import { ButtonSize , ErrorCode , ScreenName } from '@ulangi/ulangi-common/enums' ; import { ButtonStyles , ErrorBag , PublicVocabulary , } from '@ulangi/ulangi-common/interfaces' ; import { ObservablePublicSet } from '@ulangi/ulangi-observable' ; import { boundClass } from 'autobind-decorator' ; import * as _ from 'lodash' ; import { LightBoxDialogIds } from '../../constants/ids/LightBoxDialogIds' ; import { PublicSetDetailScreenIds } from '../../constants/ids/PublicSetDetailScreenIds' ; import { fullRoundedButtonStyles } from '../../styles/FullRoundedButtonStyles' ; import { DialogDelegate } from '../dialog/DialogDelegate' ; import { NavigatorDelegate } from '../navigator/NavigatorDelegate' ; import { AddVocabularyDelegate } from './AddVocabularyDelegate' ; import { PublicVocabularyActionMenuDelegate } from './PublicVocabularyActionMenuDelegate' ; @ boundClass export class PublicSetDetailScreenDelegate { private publicSet : ObservablePublicSet ; private addVocabularyDelegate : AddVocabularyDelegate ; private publicVocabularyActionMenuDelegate : PublicVocabularyActionMenuDelegate ; private dialogDelegate : DialogDelegate ; private navigatorDelegate : NavigatorDelegate ; public constructor ( publicSet , addVocabularyDelegate , publicVocabularyActionMenuDelegate , dialogDelegate , navigatorDelegate ) { this . publicSet = publicSet ; this . addVocabularyDelegate = addVocabularyDelegate ; this . publicVocabularyActionMenuDelegate = publicVocabularyActionMenuDelegate ; this . dialogDelegate = dialogDelegate ; this . navigatorDelegate = navigatorDelegate ; } public addVocabulary ( publicVocabulary , checkDuplicate = true ) { this . addVocabularyDelegate . addVocabularyFromPublicVocabulary ( publicVocabulary , this . publicSet . title , checkDuplicate , { onAdding : this . showAddingDialog , onAddSucceeded : this . showAddSucceededDialog , onAddFailed : ( errorBag ) : void => { this . showAddFailedDialogWithRetry ( errorBag , ( shouldCheckDuplicate ) : void => { this . addVocabulary ( publicVocabulary , shouldCheckDuplicate ) ; } ) ; } , } ) ; } public showAddAllDialog ( ignoreDuplicates = true ) { this . dialogDelegate . show ( { message : ignoreDuplicates ? 'To prevent duplicates, any added terms will be ignored. Do you want to continue?' : 'Do you want to add all terms?' , closeOnTouchOutside : true , onBackgroundPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; } , buttonList : [ { testID : PublicSetDetailScreenIds . CANCEL_BTN , text : 'NO' , onPress : ( ) : void => { this . dialogDelegate . dismiss ( ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidGreyBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , { testID : PublicSetDetailScreenIds . CONFIRM_ADD_ALL_BTN , text : 'YES' , onPress : ( ) : void => { this . showAddingDialog ( ) ; _ . delay ( ( ) : void => { this . addVocabularyDelegate . addVocabularyFromPublicVocabularyList ( this . publicSet . vocabularyList , this . publicSet . title , true , { onAddingAll : this . showAddingDialog , onAddAllSucceeded : this . showAddSucceededDialog , onAddAllFailed : this . showAddFailedDialog , } ) ; } , 500 ) ; } , styles : ( theme , layout ) : ButtonStyles => fullRoundedButtonStyles . getSolidPrimaryBackgroundStyles ( ButtonSize . SMALL , theme , layout ) , } , ] , } ) ; } public showLink ( link , screenTitle ) { this . navigatorDelegate . showModal ( ScreenName . BROWSER_SCREEN , { link , screenTitle , } ) ; } public showPublicVocabularyDetail ( vocabulary ) { this . navigatorDelegate . showModal ( ScreenName . PUBLIC_VOCABULARY_DETAIL_SCREEN , { vocabulary , } ) ; } public showPublicVocabularyActionMenu ( vocabulary ) { this . publicVocabularyActionMenuDelegate . show ( vocabulary ) ; } private showAddingDialog ( ) { this . dialogDelegate . show ( { message : 'Adding vocabulary. Please wait...' , } ) ; } private showAddSucceededDialog ( ) { this . dialogDelegate . showSuccessDialog ( { message : 'Added successfully.' , } ) ; } private showAddFailedDialog ( errorBag ) { this . dialogDelegate . showFailedDialog ( errorBag , { title : 'ADD FAILED' , } ) ; } private showAddFailedDialogWithRetry ( 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 . dialogDelegate . showFailedDialog ( errorBag , { title : 'ADD FAILED' , } ) ; } } }