Merge "fix oauth code"
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / performanceHistoryApp / src / actions / ltpAction.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 import { Action } from '../../../../framework/src/flux/action';
19 import { Dispatch } from '../../../../framework/src/flux/store';
20
21 import { LtpIds } from '../models/availableLtps';
22 import { PerformanceHistoryService } from '../services/performanceHistoryService';
23
24 /** 
25  * Represents the base action. 
26  */
27 export class BaseAction extends Action { }
28
29 /** 
30  * Represents an action causing the store to load available ltps. 
31  */
32 export class LoadAllAvailableLtpsAction extends BaseAction { }
33
34 /** 
35  * Represents an action causing the store to update available ltps. 
36  */
37 export class AllAvailableLtpsLoadedAction extends BaseAction {
38   /**
39    * Initialize this instance.
40    * @param availableLtps The available ltps which are returned from the database.
41    */
42   constructor(public availableLtps: LtpIds[] | null, public error?: string) {
43     super();
44   }
45 }
46
47 export class SetInitialLoadedAction extends BaseAction {
48   constructor(public initialLoaded: boolean) {
49     super();
50   }
51 }
52
53 export class NoLtpsFoundAction extends BaseAction { }
54
55 export class ResetLtpsAction extends BaseAction { }
56
57 const getDistinctLtps = (distinctLtps: LtpIds[], selectedLtp: string, selectFirstLtp?: Function, resetLtp?: Function) => {
58   let ltpNotSelected: boolean = true;
59   // eslint-disable-next-line @typescript-eslint/no-unused-expressions
60   selectFirstLtp && selectFirstLtp(distinctLtps[0].key);
61   distinctLtps.forEach((value: LtpIds) => {
62     if (value.key === selectedLtp) {
63       ltpNotSelected = false;
64     }
65   });
66   // eslint-disable-next-line @typescript-eslint/no-unused-expressions
67   resetLtp && resetLtp(ltpNotSelected);
68   return distinctLtps;
69 };
70
71 /** 
72  * Represents an asynchronous thunk action to load available distinctLtps by networkElement from the database and set the returned first Ltp as default. 
73  * @param networkElement The network element sent to database to get its available distinct Ltps.
74  * @param selectedTimePeriod The time period selected sent to database to get the distinct Ltps of the selected network element.
75  * @param selectedLtp The Ltp which is selected in the dropdown.
76  * @param selectFirstLtp The function to get the first ltp returned from the database to be selected as default on selection upon network element.
77  * @param resetLtp The function to verify if the selected ltp is also available in the selected time period database else reset the Ltp dropdown to select.
78  */
79 export const loadDistinctLtpsbyNetworkElementAsync = (networkElement: string, selectedTimePeriod: string, selectedLtp: string, selectFirstLtp?: Function, resetLtp?: Function) => (dispatch: Dispatch) => {
80   dispatch(new LoadAllAvailableLtpsAction());
81   PerformanceHistoryService.getDistinctLtpsFromDatabase(networkElement, selectedTimePeriod).then(distinctLtps => {
82     if (distinctLtps) {
83       const ltps = getDistinctLtps(distinctLtps, selectedLtp, selectFirstLtp, resetLtp);
84       dispatch(new AllAvailableLtpsLoadedAction(ltps));
85     } else {
86       if (resetLtp)
87         resetLtp();
88       dispatch(new NoLtpsFoundAction());
89     }
90   }).catch(error => {
91     dispatch(new AllAvailableLtpsLoadedAction(null, error));
92   });
93 };
94