merge from ecomp a88f0072 - Modern UI
[vid.git] / vid-webpack-master / src / app / shared / storeUtil / utils / service / service.reducers.ts
1 import {Action} from "redux";
2 import {
3   AddServiceAction,
4   ChangeServiceDirty,
5   ServiceActions,
6   CreateServiceInstanceAction,
7   UpdateServiceModelAction, UpdateServiceInstanceAction
8 } from "./service.actions";
9 import {ServiceInstance} from "../../../models/serviceInstance";
10 import {ServiceState} from "../main.reducer";
11 import {ServiceInstanceActions} from "../../../models/serviceInstanceActions";
12 import * as _ from "lodash";
13
14 export function serviceReducer(state: ServiceState, action: Action) : ServiceState{
15     switch (action.type) {
16       case ServiceActions.UPDATE_SERVICE_INSTANCE : {
17         let newState = _.cloneDeep(state);
18         const updateServiceInstanceAction = <UpdateServiceInstanceAction>action;
19         const uuid = updateServiceInstanceAction.serviceUuid;
20         const serviceInstance = updateServiceInstanceAction.serviceInstance;
21
22
23         updateUniqueNames(serviceInstance.instanceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
24
25         newState.serviceInstance[uuid] = _.merge(newState.serviceInstance[uuid], serviceInstance);
26         return newState;
27       }
28       case ServiceActions.CREATE_SERVICE_INSTANCE : {
29         const updateServiceInstanceAction = <CreateServiceInstanceAction>action;
30         const uuid = updateServiceInstanceAction.serviceUuid;
31         let newState = _.cloneDeep(state);
32
33         const serviceInstance: ServiceInstance =  new ServiceInstance();
34         const currentInstaceName = state.serviceInstance[uuid] ? serviceInstance.instanceName : null;
35
36         newState.serviceInstance[uuid] = Object.assign(serviceInstance, updateServiceInstanceAction.serviceInstance);
37         if (!_.isNil(updateServiceInstanceAction.serviceInstance)) {
38           updateUniqueNames(currentInstaceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
39         }
40         return newState;
41       }
42       case ServiceActions.DELETE_ALL_SERVICE_INSTANCES: {
43         if (state.serviceInstance) {
44           let newState = _.cloneDeep(state);
45           newState.serviceInstance = {};
46           return Object.assign({}, state, newState);
47         }
48         return Object.assign({}, state);
49       }
50       case ServiceActions.UPDATE_MODEL: {
51         let uuid = (<UpdateServiceModelAction>action).serviceHierarchy.service.uuid;
52         state.serviceHierarchy[uuid] = _.cloneDeep((<UpdateServiceModelAction>action).serviceHierarchy);
53         return Object.assign({}, state);
54       }
55       case ServiceActions.ADD_SERVICE_ACTION: {
56         const uuid: string = (<AddServiceAction>action).serviceUuid;
57         const actionToAdd: ServiceInstanceActions =  (<AddServiceAction>action).action;
58         state.serviceInstance[uuid].action =  actionToAdd;
59         return Object.assign({}, state);
60       }
61       case ServiceActions.CHANGE_SERVICE_IS_DIRTY : {
62         let newState = _.cloneDeep(state);
63         let serviceInstanceAction: ServiceInstanceActions = newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].action;
64
65         if(serviceInstanceAction !== ServiceInstanceActions.None){
66           newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
67           return newState;
68         }
69
70         const nodes =  (<ChangeServiceDirty>action).nodes;
71         for(let node of nodes){
72           const dirty = isDirty(node);
73           if(dirty) {
74             newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
75             return newState;
76           }
77         }
78
79         newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = false;
80         return newState;
81       }
82     }
83 }
84
85 const isDirty = (node) : boolean => {
86   if(node.action !== ServiceInstanceActions.None) return true;
87   if(!_.isNil(node.children) && node.children.length > 0){
88       for(let child of node.children){
89         const dirty: boolean = isDirty(child);
90         if(dirty) return true;
91       }
92   }
93   return false;
94 };
95
96 const updateUniqueNames = (oldName : string, newName : string, serviceInstance : ServiceInstance) : void => {
97   let existingNames = serviceInstance.existingNames;
98   if (!_.isNil(oldName) && oldName.toLowerCase() in existingNames) {
99     delete existingNames[oldName.toLowerCase()];
100   }
101   if(!_.isNil(newName)) {
102     existingNames[newName.toLowerCase()] = "";
103   }
104 };
105
106
107