import { DeepPartial } from '@ulangi/extended-types' ; import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { ReviewPriority , SetExtraDataName } from '@ulangi/ulangi-common/enums' ; import { ErrorBag } from '@ulangi/ulangi-common/interfaces' ; import { SetExtraDataItem } from '@ulangi/ulangi-common/types' ; import { EventBus , group , on , once } from '@ulangi/ulangi-event' ; import { ObservableSetStore } from '@ulangi/ulangi-observable' ; import { config } from '../../constants/config' ; export class WritingSettingsDelegate { private eventBus : EventBus ; private setStore : ObservableSetStore ; public constructor ( eventBus , setStore ) { this . eventBus = eventBus ; this . setStore = setStore ; } public getCurrentSettings ( ) { const initialInterval = typeof this . setStore . existingCurrentSet . writingInitialInterval !== 'undefined' ? this . setStore . existingCurrentSet . writingInitialInterval : config . writing . defaultInitialInterval ; const limit = typeof this . setStore . existingCurrentSet . writingMaxLimit !== 'undefined' ? this . setStore . existingCurrentSet . writingMaxLimit : config . writing . maxPerLesson ; const feedbackButtons = typeof this . setStore . existingCurrentSet . writingFeedbackButtons !== 'undefined' ? this . setStore . existingCurrentSet . writingFeedbackButtons : config . writing . defaultFeedbackButtons ; const autoplayAudio = typeof this . setStore . existingCurrentSet . writingAutoplayAudio !== 'undefined' ? this . setStore . existingCurrentSet . writingAutoplayAudio : config . writing . defaultAutoplayAudio ; const autoShowKeyboard = typeof this . setStore . existingCurrentSet . writingAutoShowKeyboard !== 'undefined' ? this . setStore . existingCurrentSet . writingAutoShowKeyboard : config . writing . defaultAutoShowKeyboard ; const highlightOnError = typeof this . setStore . existingCurrentSet . writingHighlightOnError !== 'undefined' ? this . setStore . existingCurrentSet . writingHighlightOnError : config . writing . defaultHighlightOnError ; const reviewPriority = typeof this . setStore . existingCurrentSet . writingReviewPriority !== 'undefined' ? this . setStore . existingCurrentSet . writingReviewPriority : config . writing . defaultReviewPriority ; return { initialInterval , limit , feedbackButtons , autoplayAudio , autoShowKeyboard , highlightOnError , reviewPriority , } ; } public saveSettings ( newSettings , callback ) { const currentSet = this . setStore . existingCurrentSet ; const originalSettings = this . getCurrentSettings ( ) ; const editedExtraData = [ ] ; if ( originalSettings . initialInterval !== newSettings . initialInterval ) { editedExtraData . push ( { dataName : SetExtraDataName . WRITING_INITIAL_INTERVAL , dataValue : newSettings . initialInterval , } ) ; } if ( originalSettings . limit !== newSettings . limit ) { editedExtraData . push ( { dataName : SetExtraDataName . WRITING_MAX_LIMIT , dataValue : newSettings . limit , } ) ; } if ( originalSettings . feedbackButtons !== newSettings . feedbackButtons ) { editedExtraData . push ( { dataName : SetExtraDataName . WRITING_FEEDBACK_BUTTONS , dataValue : newSettings . feedbackButtons , } ) ; } if ( originalSettings . autoplayAudio !== newSettings . autoplayAudio ) { editedExtraData . push ( { dataName : SetExtraDataName . WRITING_AUTOPLAY_AUDIO , dataValue : newSettings . autoplayAudio , } ) ; } if ( originalSettings . autoShowKeyboard !== newSettings . autoShowKeyboard ) { editedExtraData . push ( { dataName : SetExtraDataName . WRITING_AUTO_SHOW_KEYBOARD , dataValue : newSettings . autoShowKeyboard , } ) ; } if ( originalSettings . highlightOnError !== newSettings . highlightOnError ) { editedExtraData . push ( { dataName : SetExtraDataName . WRITING_HIGHLIGHT_ON_ERROR , dataValue : newSettings . highlightOnError , } ) ; } if ( originalSettings . reviewPriority !== newSettings . reviewPriority ) { editedExtraData . push ( { dataName : SetExtraDataName . WRITING_REVIEW_PRIORITY , dataValue : newSettings . reviewPriority , } ) ; } const editedSet = { setId : currentSet . setId , extraData : editedExtraData , } ; this . eventBus . pubsub ( createAction ( ActionType . SET__EDIT , { set : editedSet } ) , group ( on ( ActionType . SET__EDITING , callback . onSaving ) , once ( ActionType . SET__EDIT_SUCCEEDED , callback . onSaveSucceeded ) , once ( ActionType . SET__EDIT_FAILED , ( errorBag ) : void => { callback . onSaveFailed ( errorBag ) ; } ) ) ) ; } }