1 import { Action } from '../../../../framework/src/flux/action';
2 import { Dispatch } from '../../../../framework/src/flux/store';
4 import { MountedNetworkElementType } from '../models/mountedNetworkElements';
5 import { RequiredNetworkElementType } from '../models/requiredNetworkElements';
7 import { connectService } from '../services/connectService';
8 import { AddSnackbarNotification } from '../../../../framework/src/actions/snackbarActions';
10 /** Represents the base action. */
11 export class BaseAction extends Action { }
13 /** Represents an action causing the store to load all mounted network elements. */
14 export class LoadAllMountedNetworkElementsAction extends BaseAction { }
16 /** Represents an action causing the store to update all mounted network elements. */
17 export class AllMountedNetworkElementsLoadedAction extends BaseAction {
18 constructor(public mountedNetworkElements: MountedNetworkElementType[] | null, public error?: string) {
23 /** Represents an action causing the store to update all mounted network elements. */
24 export class AddOrUpdateMountedNetworkElement extends BaseAction {
25 constructor(public mountedNetworkElement: MountedNetworkElementType | null, public error?: string) {
30 export class RemoveMountedNetworkElement extends BaseAction {
31 constructor(public mountId: string) {
36 export class UpdateConnectionStateMountedNetworkElement extends BaseAction {
37 constructor(public mountId: string, connectionState: string) {
43 export class UpdateRequiredMountedNetworkElement extends BaseAction {
44 constructor(public mountId: string, public required: boolean) {
50 * Represents an action crator for a async thunk action to add an allready mounted element to the state of this app.
51 * Note: Use this action to add created object notified by the websocket.
53 export const addMountedNetworkElementAsyncActionCreator = (mountId: string) => async (dispatch: Dispatch) => {
54 return connectService.getMountedNetworkElementByMountId(mountId).then(mountedNetworkElement => {
55 mountedNetworkElement && dispatch(new AddOrUpdateMountedNetworkElement(mountedNetworkElement));
57 dispatch(new AddOrUpdateMountedNetworkElement(null, error));
61 export const updateMountedNetworkElementAsyncActionCreator = (mountId: string) => async (dispatch: Dispatch) => {
62 return connectService.getMountedNetworkElementByMountId(mountId).then(mountedNetworkElement => {
63 if (mountedNetworkElement) {
64 dispatch(new AddOrUpdateMountedNetworkElement(mountedNetworkElement));
66 dispatch(new RemoveMountedNetworkElement(mountId));
69 dispatch(new AddOrUpdateMountedNetworkElement(null, error));
73 /** Represents an async thunk action to load all mounted network elements. */
74 export const loadAllMountedNetworkElementsAsync = (dispatch: Dispatch) => {
75 dispatch(new LoadAllMountedNetworkElementsAction());
76 return connectService.getMountedNetworkElementsList().then(mountedNetworkElements => {
77 mountedNetworkElements && dispatch(new AllMountedNetworkElementsLoadedAction(mountedNetworkElements));
79 dispatch(new AllMountedNetworkElementsLoadedAction(null, error));
83 /** Represents an action crator for a async thunk action to mount a network element. */
84 export const mountNetworkElementAsyncActionCreator = (networkElement: RequiredNetworkElementType) => (dispatch: Dispatch) => {
85 return connectService.mountNetworkElement(networkElement).then((success) => {
87 dispatch(addMountedNetworkElementAsyncActionCreator(networkElement.mountId)) &&
88 dispatch(new AddSnackbarNotification({ message: `Requesting mount [${networkElement.mountId}]`, options: { variant: 'info' } }))
89 ) || dispatch(new AddSnackbarNotification({ message: `Failed to mount [${ networkElement.mountId }]`, options: { variant: 'warning' } }));
91 dispatch(new AddOrUpdateMountedNetworkElement(null, error));
95 /** Represents an action crator for a async thunk action to unmount a network element. */
96 export const unmountNetworkElementAsyncActionCreator = (mountId: string) => (dispatch: Dispatch) => {
97 return connectService.unmountNetworkElement(mountId).then((success) => {
98 success && dispatch(new AddSnackbarNotification({ message: `Requesting unmount [${ mountId }]`, options: { variant: 'info' } }))
99 || dispatch(new AddSnackbarNotification({ message: `Failed to unmount [${ mountId }]`, options: { variant: 'warning' } }));
101 dispatch(new AddOrUpdateMountedNetworkElement(null, error));