aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/storeUtil/utils/service/service.reducers.ts
blob: c6d3da52ae50de80c8f74a7b8741242fafbed49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {Action} from "redux";
import {
  AddServiceAction,
  ChangeServiceDirty,
  CreateServiceInstanceAction,
  ServiceActions,
  UpdateServiceInstanceAction,
  UpdateServiceModelAction
} from "./service.actions";
import {ServiceInstance} from "../../../models/serviceInstance";
import {ServiceState} from "../main.reducer";
import {ServiceInstanceActions} from "../../../models/serviceInstanceActions";
import * as _ from "lodash";

export function serviceReducer(state: ServiceState, action: Action) : ServiceState{
    switch (action.type) {
      case ServiceActions.UPDATE_SERVICE_INSTANCE : {
        let newState = _.cloneDeep(state);
        const updateServiceInstanceAction = <UpdateServiceInstanceAction>action;
        const uuid = updateServiceInstanceAction.serviceUuid;
        const serviceInstance = updateServiceInstanceAction.serviceInstance;


        updateUniqueNames(serviceInstance.instanceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);

        newState.serviceInstance[uuid] = _.merge(newState.serviceInstance[uuid], serviceInstance);
        return newState;
      }
      case ServiceActions.CREATE_SERVICE_INSTANCE : {
        const updateServiceInstanceAction = <CreateServiceInstanceAction>action;
        const uuid = updateServiceInstanceAction.serviceUuid;
        let newState = _.cloneDeep(state);

        const serviceInstance: ServiceInstance =  new ServiceInstance();
        const currentInstaceName = state.serviceInstance[uuid] ? serviceInstance.instanceName : null;

        newState.serviceInstance[uuid] = Object.assign(serviceInstance, updateServiceInstanceAction.serviceInstance);
        newState.serviceInstance[uuid].vidNotions = _.get(state,`serviceHierarchy[${uuid}].service.vidNotions`);
        if (!_.isNil(updateServiceInstanceAction.serviceInstance)) {
          updateUniqueNames(currentInstaceName, updateServiceInstanceAction.serviceInstance.instanceName, newState.serviceInstance[uuid]);
        }
        return newState;
      }
      case ServiceActions.DELETE_ALL_SERVICE_INSTANCES: {
        if (state.serviceInstance) {
          let newState = _.cloneDeep(state);
          newState.serviceInstance = {};
          return Object.assign({}, state, newState);
        }
        return Object.assign({}, state);
      }
      case ServiceActions.UPDATE_MODEL: {
        let uuid = (<UpdateServiceModelAction>action).serviceHierarchy.service.uuid;
        state.serviceHierarchy[uuid] = _.cloneDeep((<UpdateServiceModelAction>action).serviceHierarchy);
        return Object.assign({}, state);
      }
      case ServiceActions.ADD_SERVICE_ACTION: {
        const uuid: string = (<AddServiceAction>action).serviceUuid;
        const actionToAdd: ServiceInstanceActions =  (<AddServiceAction>action).action;
        state.serviceInstance[uuid].action =  actionToAdd;
        return Object.assign({}, state);
      }
      case ServiceActions.CHANGE_SERVICE_IS_DIRTY : {
        let newState = _.cloneDeep(state);
        let serviceInstanceAction: ServiceInstanceActions = newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].action;

        if(serviceInstanceAction !== ServiceInstanceActions.None){
          newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
          return newState;
        }

        const nodes =  (<ChangeServiceDirty>action).nodes;
        for(let node of nodes){
          const dirty = isDirty(node);
          if(dirty) {
            newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = true;
            return newState;
          }
        }

        newState.serviceInstance[(<ChangeServiceDirty>action).serviceId].isDirty = false;
        return newState;
      }
    }
}

const isDirty = (node) : boolean => {
  if(node.action !== ServiceInstanceActions.None) return true;
  if(!_.isNil(node.children) && node.children.length > 0){
      for(let child of node.children){
        const dirty: boolean = isDirty(child);
        if(dirty) return true;
      }
  }
  return false;
};

const updateUniqueNames = (oldName : string, newName : string, serviceInstance : ServiceInstance) : void => {
  let existingNames = serviceInstance.existingNames;
  if (!_.isNil(oldName) && oldName.toLowerCase() in existingNames) {
    delete existingNames[oldName.toLowerCase()];
  }
  if(!_.isNil(newName)) {
    existingNames[newName.toLowerCase()] = "";
  }
};