UX extensions
[ccsdk/features.git] / sdnr / wt / odlux / apps / connectApp / src / actions / mountedNetworkElementsActions.ts
1 import { Action } from '../../../../framework/src/flux/action';
2 import { Dispatch } from '../../../../framework/src/flux/store';
3
4 import { MountedNetworkElementType } from '../models/mountedNetworkElements';
5 import { RequiredNetworkElementType } from '../models/requiredNetworkElements';
6
7 import { connectService } from '../services/connectService';
8 import { AddSnackbarNotification } from '../../../../framework/src/actions/snackbarActions';
9
10 /** Represents the base action. */
11 export class BaseAction extends Action { }
12
13 /** Represents an action causing the store to load all mounted network elements. */
14 export class LoadAllMountedNetworkElementsAction extends BaseAction { }
15
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) {
19     super();
20   }
21 }
22
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) {
26     super();
27   }
28 }
29
30 export class RemoveMountedNetworkElement extends BaseAction {
31   constructor(public mountId: string) {
32     super();
33   }
34 }
35
36 export class UpdateConnectionStateMountedNetworkElement extends BaseAction {
37   constructor(public mountId: string, connectionState: string) {
38     super();
39   }
40 }
41
42
43 export class UpdateRequiredMountedNetworkElement extends BaseAction {
44   constructor(public mountId: string, public required: boolean) {
45     super();
46   }
47 }
48
49 /**
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.
52 */
53 export const addMountedNetworkElementAsyncActionCreator = (mountId: string) => async (dispatch: Dispatch) => {
54   return connectService.getMountedNetworkElementByMountId(mountId).then(mountedNetworkElement => {
55     mountedNetworkElement && dispatch(new AddOrUpdateMountedNetworkElement(mountedNetworkElement));
56   }).catch(error => {
57     dispatch(new AddOrUpdateMountedNetworkElement(null, error));
58   });
59 };
60
61 export const updateMountedNetworkElementAsyncActionCreator = (mountId: string) => async (dispatch: Dispatch) => {
62   return connectService.getMountedNetworkElementByMountId(mountId).then(mountedNetworkElement => {
63     if (mountedNetworkElement) {
64       dispatch(new AddOrUpdateMountedNetworkElement(mountedNetworkElement));
65     } else {
66       dispatch(new RemoveMountedNetworkElement(mountId));
67     }
68   }).catch(error => {
69     dispatch(new AddOrUpdateMountedNetworkElement(null, error));
70   });
71 };
72
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));
78   }).catch(error => {
79     dispatch(new AllMountedNetworkElementsLoadedAction(null, error));
80   });
81 };
82
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) => {
86     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' } }));
90   }).catch(error => {
91     dispatch(new AddOrUpdateMountedNetworkElement(null, error));
92   });
93 };
94
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' } }));
100   }).catch(error => {
101     dispatch(new AddOrUpdateMountedNetworkElement(null, error));
102   });
103 };
104
105