/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import { ActionObject , LogDialogState } from '../types' ; import { AT } from '../types/ActionTypes' ; import { replace } from '../Utils/util' ; import produce from 'immer' ; const initialState = { logDialogs : [ ] , continuationToken : undefined , pendingDelete : [ ] } ; const logDialogsReducer = produce ( ( state , action ) => { switch ( action . type ) { case AT . USER_LOGOUT : return { ... initialState } ; case AT . FETCH_LOG_DIALOGS_FULFILLED : if ( action . clear ) { state . logDialogs = action . logDialogs ; state . continuationToken = action . continuationToken ; } else { state . logDialogs = [ ... state . logDialogs , ... action . logDialogs ] ; state . continuationToken = action . continuationToken ; } return ; case AT . FETCH_LOG_DIALOG_FULFILLED : if ( action . replaceLocal ) { const existingLogDialog = state . logDialogs . find ( d => d . logDialogId === action . logDialog . logDialogId ) ; if ( existingLogDialog ) { replace ( state . logDialogs , action . logDialog , a => a . logDialogId ) ; } else { state . logDialogs . push ( action . logDialog ) ; } } return ; case AT . CREATE_APPLICATION_FULFILLED : return { ... initialState } ; case AT . CREATE_LOG_DIALOG : state . logDialogs . push ( action . logDialog ) ; return ; case AT . DELETE_LOG_DIALOG_ASYNC : state . pendingDelete = state . logDialogs . filter ( ld => ld . logDialogId === action . logDialogId ) ; state . logDialogs = state . logDialogs . filter ( ld => ld . logDialogId !== action . logDialogId ) ; return ; case AT . DELETE_LOG_DIALOG_FULFILLED : state . pendingDelete = state . pendingDelete . filter ( ld => ld . logDialogId !== action . logDialogId ) ; return ; case AT . DELETE_LOG_DIALOG_REJECTED : const restoreItem = state . pendingDelete . filter ( ld => ld . logDialogId === action . logDialogId ) ; state . pendingDelete = state . pendingDelete . filter ( ld => ld . logDialogId !== action . logDialogId ) ; state . logDialogs = [ ... state . logDialogs , ... restoreItem ] ; return ; case AT . DELETE_LOG_DIALOGS_ASYNC : state . pendingDelete = state . logDialogs . filter ( ld => action . logDialogIds . some ( ldId => ldId === ld . logDialogId ) ) ; state . logDialogs = state . logDialogs . filter ( ld => ! action . logDialogIds . some ( ldId => ldId === ld . logDialogId ) ) ; return ; case AT . DELETE_LOG_DIALOGS_FULFILLED : state . pendingDelete = state . pendingDelete . filter ( dialog => ! action . logDialogIds . some ( ldId => ldId === dialog . logDialogId ) ) ; return ; case AT . DELETE_LOG_DIALOGS_REJECTED : const restoreItems = state . pendingDelete . filter ( dialog => action . logDialogIds . some ( ldId => ldId === dialog . logDialogId ) ) ; state . pendingDelete = state . pendingDelete . filter ( dialog => ! action . logDialogIds . some ( ldId => ldId === dialog . logDialogId ) ) ; state . logDialogs = [ ... state . logDialogs , ... restoreItems ] ; return ; case AT . UPDATE_SOURCE_LOG_DIALOG : const sourceLogDialog = state . logDialogs . find ( d => d . logDialogId === action . sourceLogDialogId ) ; if ( sourceLogDialog ) { sourceLogDialog . targetTrainDialogIds = [ action . trainDialogId ] ; } return ; default : return ; } } , initialState ) ; export default logDialogsReducer ;