1 import {Action} from "redux";
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";
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;
23 updateUniqueNames(serviceInstance.instanceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
25 newState.serviceInstance[uuid] = _.merge(newState.serviceInstance[uuid], serviceInstance);
28 case ServiceActions.CREATE_SERVICE_INSTANCE : {
29 const updateServiceInstanceAction = <CreateServiceInstanceAction>action;
30 const uuid = updateServiceInstanceAction.serviceUuid;
31 let newState = _.cloneDeep(state);
33 const serviceInstance: ServiceInstance = new ServiceInstance();
34 const currentInstaceName = state.serviceInstance[uuid] ? serviceInstance.instanceName : null;
36 newState.serviceInstance[uuid] = Object.assign(serviceInstance, updateServiceInstanceAction.serviceInstance);
37 if (!_.isNil(updateServiceInstanceAction.serviceInstance)) {
38 updateUniqueNames(currentInstaceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
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);
48 return Object.assign({}, state);
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);
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);
61 case ServiceActions.CHANGE_SERVICE_IS_DIRTY : {
62 let newState = _.cloneDeep(state);
63 let serviceInstanceAction: ServiceInstanceActions = newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].action;
65 if(serviceInstanceAction !== ServiceInstanceActions.None){
66 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
70 const nodes = (<ChangeServiceDirty>action).nodes;
71 for(let node of nodes){
72 const dirty = isDirty(node);
74 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
79 newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = false;
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;
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()];
101 if(!_.isNil(newName)) {
102 existingNames[newName.toLowerCase()] = "";