Merge "YANG Model update for A1 Adapter"
[ccsdk/features.git] / sdnr / wt / odlux / apps / configurationApp / src / handlers / valueSelectorHandler.ts
1 import { IActionHandler } from "../../../../framework/src/flux/action";
2 import { ViewSpecification } from "../models/uiModels";
3 import { EnableValueSelector, SetSelectedValue, UpdateDeviceDescription, SetCollectingSelectionData, UpdatViewDescription } from "../actions/deviceActions";
4
5 export interface IValueSelectorState {
6   collectingData: boolean;
7   keyProperty: string | undefined;
8   listSpecification: ViewSpecification | null;
9   listData: any[];
10   onValueSelected: (value: any) => void;
11 }
12
13 const nc = (val: React.SyntheticEvent) => { };
14 const valueSelectorStateInit: IValueSelectorState = {
15   collectingData: false,
16   keyProperty: undefined,
17   listSpecification: null,
18   listData: [],
19   onValueSelected: nc,
20 };
21
22 export const valueSelectorHandler: IActionHandler<IValueSelectorState> = (state = valueSelectorStateInit, action) => {
23   if (action instanceof SetCollectingSelectionData) {
24     state = {
25       ...state,
26      collectingData: action.busy,
27     };
28   } else if (action instanceof EnableValueSelector) {
29     state = {
30       ...state,
31       collectingData: false,
32       keyProperty: action.keyProperty,
33       listSpecification: action.listSpecification,
34       onValueSelected: action.onValueSelected,
35       listData: action.listData,
36     };
37   } else if (action instanceof SetSelectedValue) {
38     state.keyProperty && state.onValueSelected(action.value[state.keyProperty]);
39     state = {
40       ...state,
41       collectingData: false,
42       keyProperty: undefined,
43       listSpecification: null,
44       onValueSelected: nc,
45       listData: [],
46     };
47   } else if (action instanceof UpdateDeviceDescription || action instanceof UpdatViewDescription) {
48     state = {
49       ...state,
50       collectingData: false,
51       keyProperty: undefined,
52       listSpecification: null,
53       onValueSelected: nc,
54       listData: [],
55     };
56   }
57   return state;
58 };