import { DeepPartial } from '@ulangi/extended-types' ; import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { 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 QuizSettingsDelegate { private eventBus : EventBus ; private setStore : ObservableSetStore ; public constructor ( eventBus , setStore ) { this . eventBus = eventBus ; this . setStore = setStore ; } public getCurrentSettings ( ) { const vocabularyPool = typeof this . setStore . existingCurrentSet . quizVocabularyPool !== 'undefined' ? this . setStore . existingCurrentSet . quizVocabularyPool : config . quiz . defaultVocabularyPool ; const multipleChoiceQuizSize = typeof this . setStore . existingCurrentSet . quizMultipleChoiceMaxLimit !== 'undefined' ? this . setStore . existingCurrentSet . quizMultipleChoiceMaxLimit : config . quiz . multipleChoice . defaultQuizSize ; const writingQuizSize = typeof this . setStore . existingCurrentSet . quizWritingMaxLimit !== 'undefined' ? this . setStore . existingCurrentSet . quizWritingMaxLimit : config . quiz . writing . defaultQuizSize ; const writingAutoShowKeyboard = typeof this . setStore . existingCurrentSet . quizWritingAutoShowKeyboard !== 'undefined' ? this . setStore . existingCurrentSet . quizWritingAutoShowKeyboard : config . quiz . writing . defaultAutoShowKeyboard ; const writingHighlightOnError = typeof this . setStore . existingCurrentSet . quizWritingHighlightOnError !== 'undefined' ? this . setStore . existingCurrentSet . quizWritingHighlightOnError : config . quiz . writing . defaultHighlightOnError ; return { vocabularyPool , multipleChoiceQuizSize , writingQuizSize , writingAutoShowKeyboard , writingHighlightOnError , } ; } public saveSettings ( newSettings , callback ) { const currentSet = this . setStore . existingCurrentSet ; const originalSettings = this . getCurrentSettings ( ) ; const editedExtraData = [ ] ; if ( originalSettings . vocabularyPool !== newSettings . vocabularyPool ) { editedExtraData . push ( { dataName : SetExtraDataName . QUIZ_VOCABULARY_POOL , dataValue : newSettings . vocabularyPool , } ) ; } if ( originalSettings . multipleChoiceQuizSize !== newSettings . multipleChoiceQuizSize ) { editedExtraData . push ( { dataName : SetExtraDataName . QUIZ_MULTIPLE_CHOICE_MAX_LIMIT , dataValue : newSettings . multipleChoiceQuizSize , } ) ; } if ( originalSettings . writingQuizSize !== newSettings . writingQuizSize ) { editedExtraData . push ( { dataName : SetExtraDataName . QUIZ_WRITING_MAX_LIMIT , dataValue : newSettings . writingQuizSize , } ) ; } if ( originalSettings . writingAutoShowKeyboard !== newSettings . writingAutoShowKeyboard ) { editedExtraData . push ( { dataName : SetExtraDataName . QUIZ_WRITING_AUTO_SHOW_KEYBOARD , dataValue : newSettings . writingAutoShowKeyboard , } ) ; } if ( originalSettings . writingHighlightOnError !== newSettings . writingHighlightOnError ) { editedExtraData . push ( { dataName : SetExtraDataName . QUIZ_WRITING_HIGHLIGHT_ON_ERROR , dataValue : newSettings . writingHighlightOnError , } ) ; } 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 ) ) ) ) ; } }