import { find } from 'lodash' ; import { actions } from '../actions' ; import { AutoforwardConfig } from '../types' ; import { AsyncThunkAction , Dispatch , GetState } from '../../types/redux' ; import { ErrorWithCode } from '../../utils/error-with-code' ; import { createLogger } from '../../log' ; const log = createLogger ( __filename ) ; export const autoforwardCreate = ( id , fwdId , config ) : AsyncThunkAction => async ( dispatch , getState ) => { const state = getState ( ) ; const host = find ( state . hosts , { id } ) ; if ( ! host ) { log . info ( `host not found: ${ id }` ) ; return ; } const autoforward = find ( state . autoforwards , { id , fwdId } ) ; if ( autoforward ) { throw new ErrorWithCode ( 409 , `autoforward already exists: ${ id }/${ fwdId }` ) ; } dispatch ( actions . autoforwardCreate ( id , fwdId , config ) ) ; } ; export const autoforwardEdit = ( id , fwdId , config ) : AsyncThunkAction => async ( dispatch , getState ) => { const state = getState ( ) ; const host = find ( state . hosts , { id } ) ; if ( ! host ) { log . info ( `host not found: ${ id }` ) ; return ; } const autoforward = find ( state . autoforwards , { id , fwdId } ) ; if ( ! autoforward ) { throw new ErrorWithCode ( 404 , `autoforward not found: ${ id }/${ fwdId }` ) ; } dispatch ( actions . autoforwardEdit ( id , fwdId , config ) ) ; } ; export const autoforwardDelete = ( id , fwdId ) : AsyncThunkAction => async ( dispatch , getState ) => { const state = getState ( ) ; const autoforward = find ( state . autoforwards , { id , fwdId } ) ; if ( ! autoforward ) { log . info ( `autoforward not found: ${ id }/${ fwdId }` ) ; return ; } dispatch ( actions . autoforwardDelete ( id , fwdId ) ) ; } ;