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