import { DeepPartial } from '@ulangi/extended-types' ; import { Options } from '@ulangi/react-native-navigation' ; import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { LightBoxState , ScreenName , VocabularyStatus , } from '@ulangi/ulangi-common/enums' ; import { SelectionItem , Vocabulary } from '@ulangi/ulangi-common/interfaces' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import { ObservableLightBox , ObservableVocabularyListState , Observer , } from '@ulangi/ulangi-observable' ; import { runInAction } from 'mobx' ; import { VocabularyBulkActionMenuIds } from '../../constants/ids/VocabularyBulkActionMenuIds' ; import { DialogDelegate } from '../dialog/DialogDelegate' ; import { NavigatorDelegate } from '../navigator/NavigatorDelegate' ; import { SetSelectionMenuDelegate } from '../set/SetSelectionMenuDelegate' ; export class VocabularyBulkActionMenuDelegate { private eventBus : EventBus ; private observer : Observer ; private observableLightBox : ObservableLightBox ; private vocabularyListState : ObservableVocabularyListState ; private setSelectionMenuDelegate : SetSelectionMenuDelegate ; private dialogDelegate : DialogDelegate ; private navigatorDelegate : NavigatorDelegate ; private styles : { light : Options ; dark : Options ; } ; public constructor ( eventBus , observer , observableLightBox , vocabularyListState , setSelectionMenuDelegate , dialogDelegate , navigatorDelegate , styles ) { this . eventBus = eventBus ; this . observer = observer ; this . observableLightBox = observableLightBox ; this . vocabularyListState = vocabularyListState ; this . setSelectionMenuDelegate = setSelectionMenuDelegate ; this . dialogDelegate = dialogDelegate ; this . navigatorDelegate = navigatorDelegate ; this . styles = styles ; } public show ( ) { const items = [ this . getSelectAllFetchedTermsBtn ( ) , this . getCategorizeSelectedButton ( ) , this . getMoveSelectedButton ( ) , this . getRestoreSelectedButton ( ) , this . getArchiveSelectedButton ( ) , this . getDeleteSelectedButton ( ) , ] ; this . observableLightBox . actionMenu = { testID : VocabularyBulkActionMenuIds . ACTION_MENU , title : 'Action' , items , } ; this . navigatorDelegate . showLightBox ( ScreenName . LIGHT_BOX_ACTION_MENU_SCREEN , { } , this . styles ) ; } private getSelectAllFetchedTermsBtn ( ) { return { testID : VocabularyBulkActionMenuIds . SELECT_ALL_FETCHED_TERMS_BTN , text : 'Select all fetched terms' , onPress : ( ) : void => { runInAction ( ( ) : void => { if ( this . vocabularyListState . vocabularyList !== null ) { Array . from ( this . vocabularyListState . vocabularyList . values ( ) ) . forEach ( ( vocabulary ) : void => { vocabulary . isSelected . set ( true ) ; } ) ; } } ) ; this . navigatorDelegate . dismissLightBox ( ) ; } , } ; } private getCategorizeSelectedButton ( ) { return { testID : VocabularyBulkActionMenuIds . CATEGORIZE_SELECTED_BTN , text : 'Recategorize selected' , onPress : ( ) : void => { this . navigatorDelegate . dismissLightBox ( ) ; this . navigatorDelegate . showModal ( ScreenName . CATEGORIZE_SCREEN , { categoryName : '' , selectedVocabularyIds : this . vocabularyListState . selectedVocabularyIds . slice ( ) , } ) ; } , } ; } private getMoveSelectedButton ( ) { return { testID : VocabularyBulkActionMenuIds . MOVE_SELECTED_BTN , text : 'Move selected' , onPress : ( ) : void => { this . navigatorDelegate . dismissLightBox ( ) ; this . observer . when ( ( ) : boolean => this . observableLightBox . state === LightBoxState . UNMOUNTED , ( ) : void => { this . setSelectionMenuDelegate . showActiveSets ( ( selectedSetId ) : void => { const editedVocabularyList = this . vocabularyListState . selectedVocabularyIds . map ( ( vocabularyId ) : DeepPartial < Vocabulary > => { return { vocabularyId } ; } ) ; const vocabularyIdSetIdPairs = this . vocabularyListState . selectedVocabularyIds . map ( ( vocabularyId ) : [ string , string ] => { return [ vocabularyId , selectedSetId ] ; } ) ; this . navigatorDelegate . dismissLightBox ( ) ; this . observer . when ( ( ) : boolean => this . observableLightBox . state === LightBoxState . UNMOUNTED , ( ) : void => { this . saveMultipleEdit ( editedVocabularyList , vocabularyIdSetIdPairs ) ; } ) ; } , { title : 'Move to...' , hideLeftButton : true , hideRightButton : true , } ) ; } ) ; } , } ; } private getArchiveSelectedButton ( ) { return { testID : VocabularyBulkActionMenuIds . ARCHIVE_SELECTED_BTN , text : 'Archive selected' , onPress : ( ) : void => { const editedVocabularyList = this . vocabularyListState . selectedVocabularyIds . map ( ( vocabularyId ) : DeepPartial < Vocabulary > => { return { vocabularyId , vocabularyStatus : VocabularyStatus . ARCHIVED , } ; } ) ; this . navigatorDelegate . dismissLightBox ( ) ; this . observer . when ( ( ) : boolean => this . observableLightBox . state === LightBoxState . UNMOUNTED , ( ) : void => { this . saveMultipleEdit ( editedVocabularyList , [ ] ) ; } ) ; } , } ; } private getRestoreSelectedButton ( ) { return { testID : VocabularyBulkActionMenuIds . RESTORE_SELECTED_BTN , text : 'Restore selected' , onPress : ( ) : void => { const editedVocabularyList = this . vocabularyListState . selectedVocabularyIds . map ( ( vocabularyId ) : DeepPartial < Vocabulary > => { return { vocabularyId , vocabularyStatus : VocabularyStatus . ACTIVE , } ; } ) ; this . navigatorDelegate . dismissLightBox ( ) ; this . observer . when ( ( ) : boolean => this . observableLightBox . state === LightBoxState . UNMOUNTED , ( ) : void => { this . saveMultipleEdit ( editedVocabularyList , [ ] ) ; } ) ; } , } ; } private getDeleteSelectedButton ( ) { return { testID : VocabularyBulkActionMenuIds . DELETE_SELECTED_BTN , text : 'Delete selected' , textColor : 'red' , onPress : ( ) : void => { const editedVocabularyList = this . vocabularyListState . selectedVocabularyIds . map ( ( vocabularyId ) : DeepPartial < Vocabulary > => { return { vocabularyId , vocabularyStatus : VocabularyStatus . DELETED , } ; } ) ; this . navigatorDelegate . dismissLightBox ( ) ; this . observer . when ( ( ) : boolean => this . observableLightBox . state === 'unmounted' , ( ) : void => { this . saveMultipleEdit ( editedVocabularyList , [ ] ) ; } ) ; } , } ; } private saveMultipleEdit ( vocabularyList , vocabularyIdSetIdPairs ) { this . eventBus . pubsub ( createAction ( ActionType . VOCABULARY__EDIT_MULTIPLE , { vocabularyList , vocabularyIdSetIdPairs , } ) , group ( on ( ActionType . VOCABULARY__EDITING_MULTIPLE , ( ) : void => this . showPerformingBulkActionDialog ( ) ) , once ( ActionType . VOCABULARY__EDIT_MULTIPLE_SUCCEEDED , ( ) : void => this . showPerformedBulkActionSuccessfully ( ) ) , once ( ActionType . VOCABULARY__EDIT_MULTIPLE_FAILED , ( errorBag ) : void => this . dialogDelegate . showFailedDialog ( errorBag ) ) ) ) ; } private showPerformingBulkActionDialog ( ) { this . dialogDelegate . show ( { message : 'Performing bulk action. Please wait...' , } ) ; } private showPerformedBulkActionSuccessfully ( ) { this . dialogDelegate . showSuccessDialog ( { message : 'Performed bulk action successfully.' , } ) ; } }