Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / configurationApp / src / handlers / valueSelectorHandler.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 { IActionHandler } from '../../../../framework/src/flux/action';
20 import { ViewSpecification } from '../models/uiModels';
21 import { EnableValueSelector, SetSelectedValue, UpdateDeviceDescription, SetCollectingSelectionData, UpdateViewDescription, UpdateOutputData } from '../actions/deviceActions';
22
23 export interface IValueSelectorState {
24   collectingData: boolean;
25   keyProperty: string | undefined;
26   listSpecification: ViewSpecification | null;
27   listData: any[];
28   onValueSelected: (value: any) => void;
29 }
30
31 const dummyFunc = () => { };
32 const valueSelectorStateInit: IValueSelectorState = {
33   collectingData: false,
34   keyProperty: undefined,
35   listSpecification: null,
36   listData: [],
37   onValueSelected: dummyFunc,
38 };
39
40 export const valueSelectorHandler: IActionHandler<IValueSelectorState> = (state = valueSelectorStateInit, action) => {
41   if (action instanceof SetCollectingSelectionData) {
42     state = {
43       ...state,
44       collectingData: action.busy,
45     };
46   } else if (action instanceof EnableValueSelector) {
47     state = {
48       ...state,
49       collectingData: false,
50       keyProperty: action.keyProperty,
51       listSpecification: action.listSpecification,
52       onValueSelected: action.onValueSelected,
53       listData: action.listData,
54     };
55   } else if (action instanceof SetSelectedValue) {
56     if (state.keyProperty) {
57       state.onValueSelected(action.value[state.keyProperty]);
58     }
59     state = {
60       ...state,
61       collectingData: false,
62       keyProperty: undefined,
63       listSpecification: null,
64       onValueSelected: dummyFunc,
65       listData: [],
66     };
67   } else if (action instanceof UpdateDeviceDescription || action instanceof UpdateViewDescription || action instanceof UpdateOutputData) {
68     state = {
69       ...state,
70       collectingData: false,
71       keyProperty: undefined,
72       listSpecification: null,
73       onValueSelected: dummyFunc,
74       listData: [],
75     };
76   }
77   return state;
78 };