/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import { ActionObject , TrainDialogState } from '../types' ; import { AT } from '../types/ActionTypes' ; import { Reducer } from 'redux' ; import { replace } from '../Utils/util' ; import produce from 'immer' ; const initialState = [ ] ; const trainDialogsReducer = produce ( ( state , action ) => { switch ( action . type ) { case AT . USER_LOGOUT : return [ ... initialState ] ; case AT . FETCH_TRAIN_DIALOGS_FULFILLED : return action . allTrainDialogs ; case AT . FETCH_APPSOURCE_FULFILLED : return action . appDefinition . trainDialogs ; case AT . REGENERATE_DISPATCH_DIALOGS_FULFILLED : return action . trainDialogs ; case AT . SOURCE_PROMOTE_UPDATED_APP_DEFINITION : return action . updatedAppDefinition . trainDialogs ; case AT . CREATE_APPLICATION_FULFILLED : return [ ... initialState ] ; case AT . CREATE_TRAIN_DIALOG_FULFILLED : state . push ( action . trainDialog ) ; return ; case AT . DELETE_TEACH_SESSION_FULFILLED : const filteredDialogs = state . filter ( dialog => dialog . trainDialogId !== action . sourceTrainDialogId ) ; if ( action . newTrainDialog ) { filteredDialogs . push ( action . newTrainDialog ) ; } return filteredDialogs ; case AT . DELETE_TRAIN_DIALOG_ASYNC : return state . filter ( dialog => dialog . trainDialogId !== action . trainDialogId ) ; case AT . EDIT_TRAINDIALOG_FULFILLED : { const trainDialogIndex = state . findIndex ( td => td . trainDialogId === action . trainDialog . trainDialogId ) ; if ( trainDialogIndex < 0 ) { return state ; } const trainDialog = state [ trainDialogIndex ] ; return [ ... state . slice ( 0 , trainDialogIndex ) , { ... trainDialog , ... action . trainDialog } , ... state . slice ( trainDialogIndex + 1 ) ] ; } case AT . FETCH_TRAIN_DIALOG_FULFILLED : if ( action . replaceLocal ) { return replace ( state , action . trainDialog , a => a . trainDialogId ) ; } else { return ; } case AT . EDIT_TRAINDIALOG_REPLACE_FULFILLED : const trainDialogs = state . filter ( dialog => dialog . trainDialogId !== action . deletedTrainDialogId ) ; return replace ( trainDialogs , action . updatedTrainDialog , a => a . trainDialogId ) ; default : return ; } } , initialState ) ; export default trainDialogsReducer ;