react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / flows / FlowsListReducer.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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import { actionTypes, FLOWS_EDITOR_FORM } from './FlowsConstants.js';
17
18 export default (state = {}, action) => {
19     switch (action.type) {
20         case actionTypes.FLOW_LIST_LOADED:
21             return {
22                 ...state,
23                 flowList: action.results,
24                 flowParticipants: action.participants,
25                 serviceID: action.serviceID,
26                 diagramType: action.diagramType,
27                 readonly: action.readonly
28             };
29         case actionTypes.ADD_OR_UPDATE_FLOW:
30         case actionTypes.ARTIFACT_LOADED:
31             let flowList = state.flowList || [];
32             let index = flowList.findIndex(
33                 flow => flow.uniqueId === action.flow.uniqueId
34             );
35             if (index === -1) {
36                 index = flowList.length;
37             }
38             let flowToBeUpdated = flowList[index];
39             flowList = [
40                 ...flowList.slice(0, index),
41                 { ...flowToBeUpdated, ...action.flow },
42                 ...flowList.slice(index + 1)
43             ];
44             return {
45                 ...state,
46                 flowList,
47                 serviceID: action.flow.serviceID,
48                 diagramType: action.flow.artifactType || state.diagramType
49             };
50         case actionTypes.DELETE_FLOW:
51             return {
52                 ...state,
53                 flowList: state.flowList.filter(
54                     flow => flow.uniqueId !== action.flow.uniqueId
55                 )
56             };
57         case actionTypes.OPEN_FLOW_DETAILS_EDITOR:
58             return {
59                 ...state,
60                 formName: FLOWS_EDITOR_FORM,
61                 formReady: null,
62                 genericFieldInfo: {
63                     artifactName: {
64                         isValid: true,
65                         errorText: '',
66                         validations: [{ type: 'required', data: true }]
67                     },
68                     description: {
69                         isValid: true,
70                         errorText: '',
71                         validations: [{ type: 'required', data: true }]
72                     }
73                 },
74                 data: action.flow
75             };
76
77         case actionTypes.CLOSE_FLOW_DETAILS_EDITOR:
78             return {
79                 ...state,
80                 data: undefined
81             };
82         case actionTypes.OPEN_FLOW_DIAGRAM_EDITOR:
83             return {
84                 ...state,
85                 data: action.flow,
86                 shouldShowWorkflowsEditor: false
87             };
88         case actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR:
89             return {
90                 ...state,
91                 data: undefined,
92                 shouldShowWorkflowsEditor: true
93             };
94         case actionTypes.RESET:
95             return {};
96     }
97
98     return state;
99 };