1 import {Action} from "redux";
5 CreateServiceInstanceAction,
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";
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;
24 updateUniqueNames(serviceInstance.instanceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
26 newState.serviceInstance[uuid] = _.merge(newState.serviceInstance[uuid], serviceInstance);
29 case ServiceActions.CREATE_SERVICE_INSTANCE : {
30 const updateServiceInstanceAction = <CreateServiceInstanceAction>action;
31 const uuid = updateServiceInstanceAction.serviceUuid;
32 let newState = _.cloneDeep(state);
34 const serviceInstance: ServiceInstance = new ServiceInstance();
35 const currentInstaceName = state.serviceInstance[uuid] ? serviceInstance.instanceName : null;
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]);
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);
50 return Object.assign({}, state);
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);
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);
63 case ServiceActions.CHANGE_SERVICE_IS_DIRTY : {
64 let newState = _.cloneDeep(state);
65 let serviceInstanceAction: ServiceInstanceActions = newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].action;
67 if(serviceInstanceAction !== ServiceInstanceActions.None){
68 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
72 const nodes = (<ChangeServiceDirty>action).nodes;
73 for(let node of nodes){
74 const dirty = isDirty(node);
76 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
81 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = false;
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;
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()];
103 if(!_.isNil(newName)) {
104 existingNames[newName.toLowerCase()] = "";