/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import { ActionObject } from '../types' ; import { AT } from '../types/ActionTypes' ; import { ErrorType } from '../types/const' ; import { AppBase , Banner } from '@conversationlearner/models' ; import { TipType } from '../components/ToolTips/ToolTips' ; import * as ClientFactory from '../services/clientFactory' ; import { Dispatch } from 'redux' ; import { AxiosError } from 'axios' ; const setCurrentApp = ( key , app ) : ActionObject => { return { type : AT . SET_CURRENT_APP_ASYNC , app } ; } ; const setCurrentAppFulfilled = ( app ) : ActionObject => { return { type : AT . SET_CURRENT_APP_FULFILLED , app } ; } ; export const setErrorDisplay = ( errorType , title , messages , actionType ? ) : ActionObject => { return { type : AT . SET_ERROR_DISPLAY , errorType , title , messages : ( typeof messages === "string" ) ? [ messages ] : messages , actionType } ; } ; export const setCurrentAppThunkAsync = ( key , application ) => { return async ( dispatch ) => { const clClient = ClientFactory . getInstance ( AT . SET_CURRENT_APP_ASYNC ) ; try { dispatch ( setCurrentApp ( key , application ) ) ; const newApp = await clClient . setApp ( application ) ; dispatch ( setCurrentAppFulfilled ( application ) ) ; return newApp ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . SET_CURRENT_APP_ASYNC ) ) ; throw error ; } } ; } ; export const setConversationId = ( userName , userId , conversationId ) : ActionObject => { return { type : AT . SET_CONVERSATION_ID_ASYNC , userName , userId , conversationId } ; } ; export const setConversationIdThunkAsync = ( userName , userId , conversationId ) => { return async ( dispatch ) => { const clClient = ClientFactory . getInstance ( AT . SET_CONVERSATION_ID_ASYNC ) ; try { dispatch ( setConversationId ( userName , userId , conversationId ) ) ; await clClient . setConversationId ( userName , userId , conversationId ) ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . SET_CONVERSATION_ID_ASYNC ) ) ; throw error ; } } ; } ; export const setTipType = ( tipType ) : ActionObject => { return { type : AT . SET_TIP_TYPE , tipType : tipType } ; } ; export const setErrorDismissCallback = ( closeCallback ) : ActionObject => { return { type : AT . SET_ERROR_DISMISS_CALLBACK , closeCallback } ; } ; export const setWebchatScrollPosition = ( position ) : ActionObject => { return { type : AT . SET_WEBCHAT_SCROLL_POSITION , position } ; } ; export const clearWebchatScrollPosition = ( ) : ActionObject => { return { type : AT . CLEAR_WEBCHAT_SCROLL_POSITION } ; } ; export const clearBanner = ( clearedBanner ) : ActionObject => { return { type : AT . CLEAR_BANNER , clearedBanner : clearedBanner } ; } ; export const clearErrorDisplay = ( ) : ActionObject => { return { type : AT . CLEAR_ERROR_DISPLAY } ; } ; export const spinnerAdd = ( ) : ActionObject => { return { type : AT . SPINNER_ADD } ; } ; export const spinnerRemove = ( ) : ActionObject => { return { type : AT . SPINNER_REMOVE } ; } ;