/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import * as CLM from '@conversationlearner/models' ; import * as ClientFactory from '../services/clientFactory' ; import * as HttpStatus from 'http-status-codes' ; import { AT , ActionObject , ErrorType } from '../types' ; import { Dispatch } from 'redux' ; import { setErrorDisplay } from './displayActions' ; import { AxiosError } from 'axios' ; export const MAX_FETCH_LOG_PAGE_SIZE = 100 ; const deleteLogDialogAsync = ( appId , logDialogId ) : ActionObject => { return { type : AT . DELETE_LOG_DIALOG_ASYNC , appId , logDialogId } ; } ; const deleteLogDialogFulfilled = ( logDialogId ) : ActionObject => { return { type : AT . DELETE_LOG_DIALOG_FULFILLED , logDialogId } ; } ; const deleteLogDialogRejected = ( logDialogId ) : ActionObject => { return { type : AT . DELETE_LOG_DIALOG_REJECTED , logDialogId } ; } ; export const deleteLogDialogThunkAsync = ( app , logDialogId , packageId ) => { return async ( dispatch ) => { dispatch ( deleteLogDialogAsync ( app . appId , logDialogId ) ) ; const clClient = ClientFactory . getInstance ( AT . DELETE_LOG_DIALOG_ASYNC ) ; try { await clClient . logDialogsDelete ( app . appId , logDialogId ) ; dispatch ( deleteLogDialogFulfilled ( logDialogId ) ) ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . DELETE_LOG_DIALOG_ASYNC ) ) ; dispatch ( deleteLogDialogRejected ( logDialogId ) ) ; void dispatch ( fetchLogDialogThunkAsync ( app . appId , logDialogId , true ) ) ; } } ; } ; const deleteLogDialogsAsync = ( appId , logDialogIds ) : ActionObject => { return { type : AT . DELETE_LOG_DIALOGS_ASYNC , appId , logDialogIds , } ; } ; const deleteLogDialogsFulfilled = ( logDialogIds ) : ActionObject => { return { type : AT . DELETE_LOG_DIALOGS_FULFILLED , logDialogIds } ; } ; const deleteLogDialogsRejected = ( logDialogIds ) : ActionObject => { return { type : AT . DELETE_LOG_DIALOGS_REJECTED , logDialogIds } ; } ; export const deleteLogDialogsThunkAsync = ( app , logDialogIds , packageId ) => { return async ( dispatch ) => { dispatch ( deleteLogDialogsAsync ( app . appId , logDialogIds ) ) ; const clClient = ClientFactory . getInstance ( AT . DELETE_LOG_DIALOGS_ASYNC ) ; try { await clClient . logDialogsDeleteMany ( app . appId , logDialogIds ) ; dispatch ( deleteLogDialogsFulfilled ( logDialogIds ) ) ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . DELETE_LOG_DIALOGS_ASYNC ) ) ; dispatch ( deleteLogDialogsRejected ( logDialogIds ) ) ; } } ; } ; const fetchLogDialogAsync = ( noSpinnerDisplay ) : ActionObject => { return { type : AT . FETCH_LOG_DIALOG_ASYNC , noSpinnerDisplay } ; } ; const fetchLogDialogFulfilled = ( logDialog , replaceLocal ) : ActionObject => { return { type : AT . FETCH_LOG_DIALOG_FULFILLED , logDialog , replaceLocal } ; } ; const fetchLogDialogNotFound = ( ) : ActionObject => { return { type : AT . FETCH_LOG_DIALOG_NOTFOUND } ; } ; param appId param logDialogId param replaceLocal param nullOnNotFound param noSpinnerDisplay export const fetchLogDialogThunkAsync = ( appId , logDialogId , replaceLocal , nullOnNotFound = false , noSpinnerDisplay = false ) => { return async ( dispatch ) => { const clClient = ClientFactory . getInstance ( AT . FETCH_LOG_DIALOG_ASYNC ) ; dispatch ( fetchLogDialogAsync ( noSpinnerDisplay ) ) ; try { const logDialog = await clClient . logDialog ( appId , logDialogId ) ; dispatch ( fetchLogDialogFulfilled ( logDialog , replaceLocal ) ) ; return logDialog ; } catch ( e ) { const error = e as AxiosError ; if ( error . response ?. status === HttpStatus . NOT_FOUND && nullOnNotFound ) { dispatch ( fetchLogDialogNotFound ( ) ) ; return null ; } dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . FETCH_LOG_DIALOG_ASYNC ) ) ; throw e ; } } ; } ; const fetchLogDialogsAsync = ( appId , packageIds , noSpinnerDisplay ) : ActionObject => { return { type : AT . FETCH_LOG_DIALOGS_ASYNC , appId , packageIds , noSpinnerDisplay } ; } ; const fetchLogDialogsFulfilled = ( logQueryResult , clear ) : ActionObject => { return { type : AT . FETCH_LOG_DIALOGS_FULFILLED , logDialogs : logQueryResult . logDialogs , continuationToken : logQueryResult . continuationToken , clear } ; } ; export const fetchLogDialogsThunkAsync = ( app , packageId , clear = true , continuationToken ? , noSpinnerDisplay = true , maxPageSize = MAX_FETCH_LOG_PAGE_SIZE ) => { return async ( dispatch ) => { const packageIds = ( packageId === app . devPackageId ) ? ( app . packageVersions ?? [ ] ) . map ( pv => pv . packageId ) . concat ( packageId ) : [ packageId ] ; const clClient = ClientFactory . getInstance ( AT . FETCH_LOG_DIALOGS_ASYNC ) ; dispatch ( fetchLogDialogsAsync ( app . appId , packageIds , noSpinnerDisplay ) ) ; try { const logQueryResult = await clClient . logDialogs ( app . appId , packageIds , maxPageSize , continuationToken ) ; dispatch ( fetchLogDialogsFulfilled ( logQueryResult , clear ) ) ; return logQueryResult ; } catch ( e ) { const error = e as AxiosError ; dispatch ( setErrorDisplay ( ErrorType . Error , error . message , error . response ? JSON . stringify ( error . response , null , '  ' ) : "" , AT . FETCH_LOG_DIALOGS_ASYNC ) ) ; return null ; } } ; } ;