Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / inventoryApp / src / actions / inventoryTreeActions.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 import { AddErrorInfoAction } from '../../../../framework/src/actions/errorActions';
20 import { NavigateToApplication } from '../../../../framework/src/actions/navigationActions';
21 import { Action } from '../../../../framework/src/flux/action';
22 import { Dispatch } from '../../../../framework/src/flux/store';
23
24 import { InventoryTreeNode, InventoryType, TreeDemoItem } from '../models/inventory';
25 import { inventoryService } from '../services/inventoryService';
26
27 /**
28  * Represents the base action.
29  */
30 export class BaseAction extends Action { }
31
32 export class SetBusyAction extends BaseAction {
33   constructor(public busy: boolean = true) {
34     super();
35
36   }
37 }
38
39 export class SetSearchTextAction extends BaseAction {
40   constructor(public searchTerm: string = '') {
41     super();
42
43   }
44 }
45
46 export class UpdateInventoryTreeAction extends BaseAction {
47   constructor(public rootNode: InventoryTreeNode) {
48     super();
49
50   }
51 }
52
53 export class UpdateSelectedNodeAction extends BaseAction {
54   constructor(public selectedNode?: InventoryType) {
55     super();
56
57   }
58 }
59
60 export class UpdateExpandedNodesAction extends BaseAction {
61   constructor(public expandedNodes?: TreeDemoItem[]) {
62     super();
63
64   }
65 }
66
67 export const setSearchTermAction = (searchTerm: string) => (dispatch: Dispatch) =>{
68   dispatch(new SetSearchTextAction(searchTerm));
69 };
70
71
72 export const updateInventoryTreeAsyncAction = (mountId: string, searchTerm?: string) => async (dispatch: Dispatch) => {
73   dispatch(new SetBusyAction(true));
74   dispatch(new SetSearchTextAction(searchTerm));
75   try {
76     const result = await inventoryService.getInventoryTree(mountId, searchTerm);
77     if (!result) {
78       dispatch(new AddErrorInfoAction({ title: 'Error', message: `Could not load inventory tree for [${mountId}]. Please check you connection to the server and try later.` }));
79       dispatch(new NavigateToApplication('inventory'));
80     } else {
81       dispatch(new UpdateInventoryTreeAction(result));
82     }
83   } catch (err) {
84     throw new Error('Could not load inventory tree from server.');
85   } finally {
86     dispatch(new SetBusyAction(false));
87   }
88 };
89
90 export const selectInventoryNodeAsyncAction = (nodeId: string) => async (dispatch: Dispatch) => {
91   dispatch(new SetBusyAction(true));
92   try {
93     const result = await inventoryService.getInventoryEntry(nodeId);
94     if (!result) throw new Error('Could not load inventory tree from server.');
95     dispatch(new UpdateSelectedNodeAction(result));
96   } catch (err) {
97     throw new Error('Could not load inventory tree from server.');
98   } finally {
99     dispatch(new SetBusyAction(false));
100   }
101 };