import { ActionType } from '@ulangi/ulangi-action' ; import { ActivityState , SetStatus } from '@ulangi/ulangi-common/enums' ; import { EventBus , on } from '@ulangi/ulangi-event' ; import { ObservableConverter , ObservableSet , ObservableSetManagementScreen , } from '@ulangi/ulangi-observable' ; import { boundClass } from 'autobind-decorator' ; import { observable } from 'mobx' ; import { FetchSetDelegate } from './FetchSetDelegate' ; import { SetActionMenuDelegate } from './SetActionMenuDelegate' ; @ boundClass export class SetManagementScreenDelegate { private eventBus : EventBus ; private observableConverter : ObservableConverter ; private observableScreen : ObservableSetManagementScreen ; private fetchSetDelegate : FetchSetDelegate ; private setActionMenuDelegate : SetActionMenuDelegate ; public constructor ( eventBus , observableConverter , observableScreen , fetchSetDelegate , setActionMenuDelegate ) { this . eventBus = eventBus ; this . observableConverter = observableConverter ; this . observableScreen = observableScreen ; this . fetchSetDelegate = fetchSetDelegate ; this . setActionMenuDelegate = setActionMenuDelegate ; } public selectAndFetchSets ( setStatus ) { this . observableScreen . selectedSetStatus = setStatus ; this . fetchSets ( ) ; } public refresh ( ) { this . observableScreen . refreshing = true ; this . fetchSets ( ) ; } public showSetActionMenu ( set ) { this . setActionMenuDelegate . show ( set ) ; } public autoRefreshOnSetChange ( ) { this . eventBus . subscribe ( on ( [ ActionType . SET__ADD_SUCCEEDED , ActionType . SET__EDIT_SUCCEEDED , ActionType . SET__DOWNLOAD_SETS_SUCCEEDED , ] , ( ) : void => { this . refresh ( ) ; } ) ) ; } private fetchSets ( ) { this . fetchSetDelegate . fetchSets ( this . observableScreen . selectedSetStatus , { onFetching : ( ) : void => { this . observableScreen . fetchState = ActivityState . ACTIVE ; } , onFetchSucceeded : ( setList ) : void => { this . observableScreen . setList = observable . map ( setList . map ( ( set ) : [ string , ObservableSet ] => { return [ set . setId , this . observableConverter . convertToObservableSet ( set ) , ] ; } ) ) ; this . observableScreen . fetchState = ActivityState . INACTIVE ; this . observableScreen . refreshing = false ; } , onFetchFailed : ( ) : void => { this . observableScreen . fetchState = ActivityState . ERROR ; this . observableScreen . refreshing = false ; } , } ) ; } }