2 import { Action } from '../../../../framework/src/flux/action';
3 import { Dispatch } from '../../../../framework/src/flux/store';
5 import { AddSnackbarNotification } from '../../../../framework/src/actions/snackbarActions';
6 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
8 import mediatorService from '../services/mediatorService';
9 import { MediatorConfig, MediatorConfigResponse } from '../models/mediatorServer';
11 /** Represents the base action. */
12 export class BaseAction extends Action { }
14 export class SetMediatorBusyByName extends BaseAction {
15 constructor (public name: string, public isBusy: boolean) {
20 export class AddMediatorConfig extends BaseAction {
21 constructor (public mediatorConfig: MediatorConfigResponse) {
26 export class UpdateMediatorConfig extends BaseAction {
27 constructor (public name: string, public mediatorConfig: MediatorConfigResponse) {
32 export class RemoveMediatorConfig extends BaseAction {
33 constructor (public name: string) {
39 export const startMediatorByNameAsyncActionCreator = (name: string) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
40 dispatch(new SetMediatorBusyByName(name, true));
41 const { mediator: { mediatorServerState: { url } } } = getState();
43 mediatorService.startMediatorByName(url, name).then(msg => {
44 dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
45 // since there is no notification, a timeout will be need here
46 window.setTimeout(() => {
47 mediatorService.getMediatorServerConfigByName(url, name).then(config => {
49 dispatch(new UpdateMediatorConfig(name, config));
51 dispatch(new AddSnackbarNotification({ message: `Error: reading mediator config for ${name}.`, options: { variant: 'error' } }));
53 dispatch(new SetMediatorBusyByName(name, false));
58 dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
59 dispatch(new SetMediatorBusyByName(name, false));
63 export const stopMediatorByNameAsyncActionCreator = (name: string) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
64 dispatch(new SetMediatorBusyByName(name, true));
65 const { mediator: { mediatorServerState: { url } } } = getState();
67 mediatorService.stopMediatorByName(url, name).then(msg => {
68 dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
69 // since there is no notification, a timeout will be need here
70 window.setTimeout(() => {
71 mediatorService.getMediatorServerConfigByName(url, name).then(config => {
73 dispatch(new UpdateMediatorConfig(name, config));
75 dispatch(new AddSnackbarNotification({ message: `Error: reading mediator config for ${name}.`, options: { variant: 'error' } }));
77 dispatch(new SetMediatorBusyByName(name, false));
82 dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
83 dispatch(new SetMediatorBusyByName(name, false));
87 export const addMediatorConfigAsyncActionCreator = (config: MediatorConfig) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
88 const { Name: name } = config;
89 const { mediator: { mediatorServerState: { url } } } = getState();
91 mediatorService.createMediatorConfig(url, config).then(msg => {
92 dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
93 // since there is no notification, a timeout will be need here
94 window.setTimeout(() => {
95 mediatorService.getMediatorServerConfigByName(url, name).then(config => {
97 dispatch(new AddMediatorConfig(config));
99 dispatch(new AddSnackbarNotification({ message: `Error: reading mediator config for ${name}.`, options: { variant: 'error' } }));
105 dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
109 export const updateMediatorConfigAsyncActionCreator = (config: MediatorConfig) => (dispatch: Dispatch) => {
110 // currently not supported be backend
113 export const removeMediatorConfigAsyncActionCreator = (config: MediatorConfig) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
114 const { Name: name } = config;
115 const { mediator: { mediatorServerState: { url } } } = getState();
117 mediatorService.deleteMediatorConfigByName(url, name).then(msg => {
118 dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
119 // since there is no notification, a timeout will be need here
120 window.setTimeout(() => {
121 mediatorService.getMediatorServerConfigByName(url, config.Name).then(config => {
123 dispatch(new RemoveMediatorConfig(name));
125 dispatch(new AddSnackbarNotification({ message: `Error: deleting mediator config for ${name}.`, options: { variant: 'error' } }));
131 dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
132 dispatch(new SetMediatorBusyByName(name, false));