c66cbbc25b34551148067c008b4f0bfd7d682ecc
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / entitlementPools / EntitlementPoolsEditorReducer.js
1 /*!
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import {
17     actionTypes,
18     defaultState,
19     SP_ENTITLEMENT_POOL_FORM
20 } from './EntitlementPoolsConstants.js';
21 import moment from 'moment';
22 import { DATE_FORMAT } from 'sdc-app/onboarding/OnboardingConstants.js';
23
24 export default (state = {}, action) => {
25     switch (action.type) {
26         case actionTypes.entitlementPoolsEditor.OPEN:
27             let entitlementPoolData = { ...action.entitlementPool };
28             let { startDate, expiryDate } = entitlementPoolData;
29             if (startDate) {
30                 entitlementPoolData.startDate = moment(
31                     startDate,
32                     DATE_FORMAT
33                 ).format(DATE_FORMAT);
34             }
35             if (expiryDate) {
36                 entitlementPoolData.expiryDate = moment(
37                     expiryDate,
38                     DATE_FORMAT
39                 ).format(DATE_FORMAT);
40             }
41             return {
42                 ...state,
43                 formReady: null,
44                 formName: SP_ENTITLEMENT_POOL_FORM,
45                 genericFieldInfo: {
46                     name: {
47                         isValid: true,
48                         errorText: '',
49                         validations: [
50                             { type: 'required', data: true },
51                             { type: 'maxLength', data: 120 },
52                             { type: 'validateName', data: true }
53                         ]
54                     },
55                     description: {
56                         isValid: true,
57                         errorText: '',
58                         validations: [
59                             { type: 'maxLength', data: 1000 },
60                             { type: 'validateName', data: true }
61                         ]
62                     },
63                     type: {
64                         isValid: true,
65                         errorText: '',
66                         validations: [{ type: 'required', data: true }]
67                     },
68                     increments: {
69                         isValid: true,
70                         errorText: '',
71                         validations: [
72                             { type: 'maxLength', data: 120 },
73                             { type: 'validateName', data: true }
74                         ]
75                     },
76                     thresholdUnits: {
77                         isValid: true,
78                         errorText: '',
79                         validations: []
80                     },
81                     thresholdValue: {
82                         isValid: true,
83                         errorText: '',
84                         validations: []
85                     },
86                     startDate: {
87                         isValid: true,
88                         errorText: '',
89                         validations: []
90                     },
91                     expiryDate: {
92                         isValid: true,
93                         errorText: '',
94                         validations: []
95                     },
96                     manufacturerReferenceNumber: {
97                         isValid: true,
98                         errorText: '',
99                         validations: [
100                             { type: 'required', data: true },
101                             { type: 'maxLength', data: 100 },
102                             { type: 'validateName', data: true }
103                         ]
104                     }
105                 },
106                 data: action.entitlementPool
107                     ? entitlementPoolData
108                     : defaultState.ENTITLEMENT_POOLS_EDITOR_DATA
109             };
110         case actionTypes.entitlementPoolsEditor.DATA_CHANGED:
111             return {
112                 ...state,
113                 data: {
114                     ...state.data,
115                     ...action.deltaData
116                 }
117             };
118         case actionTypes.entitlementPoolsEditor.CLOSE:
119             return {};
120
121         case actionTypes.entitlementPoolsEditor.LIMITS_LIST_LOADED:
122             return {
123                 ...state,
124                 limitsList: action.response.results
125             };
126         default:
127             return state;
128     }
129 };