summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/storeUtil/utils/global/global.reducers.ts
blob: 49df2088a70c5fd5d73bf20b54bf7a08707908ce (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
import {Action} from 'redux';
import {
  GlobalActions,
  UpdateFlagsAction,
  UpdateGlobalAction,
  UpdateDrawingBoardStatusAction,
  UpdateGenericModalCriteria,
  UpdateGenericModalHelper,
  DeleteGenericModalHelper,
  DeleteGenericModalTabelDataHelper,
  UpdateGenericModalTableDataHelper, UpdateCurrentModalModeAction
} from "./global.actions";
import * as _ from "lodash";

export interface GlobalState {
  name : string;
  flags : { [key: string]: boolean };
  drawingBoardStatus : string;
  genericModalCriteria : { [key: string]: any };
  genericModalHelper : { [key: string]: any };
  isUpdateModalMode?: boolean;

}

const initialState: GlobalState = {
  name : null,
  flags : null,
  drawingBoardStatus : null,
  genericModalCriteria : {
    roles : []
  },
  genericModalHelper : {},
  isUpdateModalMode : null
};

export const globalReducer =
  function (state: GlobalState = initialState, action: Action): GlobalState {
    switch (action.type) {
      case GlobalActions.UPDATE_NAME:
        return Object.assign(state, state, (<UpdateGlobalAction>action));
      case GlobalActions.UPDATE_FLAGS:
        Object.assign(state, (<UpdateFlagsAction>action));
        return Object.assign({}, state);
      case GlobalActions.UPDATE_DRAWING_BOARD_STATUS:
        return Object.assign(state, state, (<UpdateDrawingBoardStatusAction>action));
      case GlobalActions.UPDATE_GENERIC_MODAL_CRITERIA : {
        const updateGenericModalCriteria = <UpdateGenericModalCriteria>action;
        let newState = _.cloneDeep(state);
        if(_.isNil(newState.genericModalCriteria)){
          newState.genericModalCriteria = {};
        }
        newState.genericModalCriteria[updateGenericModalCriteria.field] = updateGenericModalCriteria.values;
        return newState;
      }
      case GlobalActions.UPDATE_GENERIC_MODAL_HELPER : {
        const updateGenericModalHelper= <UpdateGenericModalHelper>action;
        let newState = _.cloneDeep(state);
        if(_.isNil(newState.genericModalHelper) ){newState.genericModalHelper = {}; }
        if(_.isNil(newState.genericModalHelper[updateGenericModalHelper.field])){
          newState.genericModalHelper[updateGenericModalHelper.field] = {};
        }
        newState.genericModalHelper[updateGenericModalHelper.field][updateGenericModalHelper.values[updateGenericModalHelper.uniqObjectField]] = updateGenericModalHelper.values;
        return newState;
      }
      case GlobalActions.DELETE_GENERIC_MODAL_HELPER : {
        const deleteGenericModalHelper= <DeleteGenericModalHelper>action;
        let newState = _.cloneDeep(state);
        delete newState.genericModalHelper[deleteGenericModalHelper.field][deleteGenericModalHelper.uniqObjectField];
        return newState;
      }
      case GlobalActions.CLEAR_ALL_GENERIC_MODAL_HELPER : {
        let newState = _.cloneDeep(state);
        newState.genericModalHelper = {};
        return newState;
      }
      case GlobalActions.DELETE_GENERIC_MODAL_TABLE_DATA_HELPER : {
        let newState = _.cloneDeep(state);
        delete newState.genericModalHelper[(<DeleteGenericModalTabelDataHelper>action).field];
        return newState;
      }
      case GlobalActions.UPDATE_GENERIC_MODAL_TABLE_DATA_HELPER : {
        let newState = _.cloneDeep(state);
        newState.genericModalHelper[(<UpdateGenericModalTableDataHelper>action).field] = (<UpdateGenericModalTableDataHelper>action).values ;
        return newState;
      }
      case GlobalActions.UPDATE_CURRENT_MODAL_MODE : {
        let newState = {...state};
        if ((<UpdateCurrentModalModeAction>action).isUpdateModalMode === null){
          delete newState.isUpdateModalMode;
        } else {
          newState.isUpdateModalMode = (<UpdateCurrentModalModeAction>action).isUpdateModalMode;
        }
        return newState;
      }
      default:
        return state;
    }
  };