admin.ts 8.35 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
import update from 'immutability-helper';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
2
import { IReducerAction } from 'model/common.model';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
import {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
4
  ADMIN_APPEND_EXEC_RUNS,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
5 6 7
  ADMIN_RECEIVE_DIM,
  ADMIN_RECEIVE_DIMS,
  ADMIN_RECEIVE_EXEC,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
8
  ADMIN_RECEIVE_EXEC_CHANGES,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
9 10
  ADMIN_RECEIVE_EXEC_DET,
  ADMIN_RECEIVE_EXEC_LASTRUN,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
  ADMIN_RECEIVE_EXEC_RUNS,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
12 13 14 15 16 17
  ADMIN_RECEIVE_EXECS,
  ADMIN_RECEIVE_PARAM,
  ADMIN_RECEIVE_PARAMS,
  ADMIN_REMOVE_DIM,
  ADMIN_REMOVE_EXEC,
  ADMIN_REMOVE_PARAM,
Oleksii Savran's avatar
Oleksii Savran committed
18 19 20
  ADMIN_TOGGLE_PARAM_MODAL,
  ADMIN_TOGGLE_DIM_MODAL,
  ADMIN_TOGGLE_EXEC_MODAL,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
21 22 23 24 25
} from 'kpi/constants';
import Execution from 'model/kpi/Execution';
import Page from 'model/Page';
import Dimension from 'model/kpi/Dimension';
import KPIParameter from 'model/kpi/KPIParameter';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
26 27 28 29
import ApiCall from 'model/ApiCall';
import ExecutionDetails from 'model/kpi/ExecutionDetails';
import FilteredPage from 'model/FilteredPage';
import {ensureDimensionType} from 'utilities';
Matija Obreza's avatar
Matija Obreza committed
30 31

const INITIAL_STATE: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
32
  exec: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
33 34 35
    page: ApiCall<Page<Execution>>,
    details: ApiCall<ExecutionDetails>,
    changes: ApiCall<any>,
Oleksii Savran's avatar
Oleksii Savran committed
36
    modalIsOpen: boolean,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
37 38
  },
  dim: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
39 40
    page: ApiCall<Page<Dimension<any>>>,
    details: ApiCall<Dimension<any>>,
Oleksii Savran's avatar
Oleksii Savran committed
41
    modalIsOpen: boolean,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42 43
  },
  param: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
44 45
    page: ApiCall<Page<KPIParameter>>,
    details: ApiCall<KPIParameter>,
Oleksii Savran's avatar
Oleksii Savran committed
46
    modalIsOpen: boolean,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
  },
Matija Obreza's avatar
Matija Obreza committed
48 49
} = {
  exec: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
50 51
    page: null,
    details: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52
    changes: null,
Oleksii Savran's avatar
Oleksii Savran committed
53
    modalIsOpen: false,
Matija Obreza's avatar
Matija Obreza committed
54 55 56
  },
  dim: {
    page: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
57
    details: null,
Oleksii Savran's avatar
Oleksii Savran committed
58
    modalIsOpen: false,
Matija Obreza's avatar
Matija Obreza committed
59 60 61
  },
  param: {
    page: null,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
62
    details: null,
Oleksii Savran's avatar
Oleksii Savran committed
63
    modalIsOpen: false,
Matija Obreza's avatar
Matija Obreza committed
64 65 66 67 68 69 70
  },
};

export default function admin(state = INITIAL_STATE, action: IReducerAction) {

  switch (action.type) {
    case ADMIN_RECEIVE_EXECS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
71
      const {apiCall} = action.payload;
Matija Obreza's avatar
Matija Obreza committed
72
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
73
        exec: { page: { $set: apiCall } },
Matija Obreza's avatar
Matija Obreza committed
74 75 76
      });
    }
    case ADMIN_RECEIVE_EXEC: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
77
      const {apiCall} = action.payload;
78
      console.log(`ADMIN_RECEIVE_EXEC`, apiCall);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
79

80 81 82 83 84 85 86 87 88
      const updates: any = { exec: {} };
      if (state.exec.details) {
        if (apiCall.data) {
          if (state.exec.details.data && apiCall.data.id === state.exec.details.data.execution.id) {
            updates.exec.details = {
              data: { execution: { $set: apiCall.data } },
            };
          } else {
            updates.exec.details = {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
89
              data: {
90 91 92
                execution: { $set: apiCall.data },
                runs: { $set: null },
                lastRun: { $set: null },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
93
              },
94 95 96
            };
          }
        }
97 98
      }

99 100 101 102 103 104 105
      if (apiCall.data && state.exec.page && state.exec.page.data) {
        // update page if exists
        const receivedIndex = apiCall.data ? state.exec.page.data.content.findIndex((item) => item.id === apiCall.data.id) : -1;
        if (receivedIndex !== -1) {
          updates.exec.page = {
            data: {
              content: { [receivedIndex]: { $set: apiCall.data } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
106
            },
107 108 109 110 111
          };
        } else {
          updates.exec.page = {
            data: {
              content: {$push: [apiCall.data]},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
112
            },
113 114
          };
        }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
115
      }
116
      return update(state, updates);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
117 118 119
    }

    case ADMIN_REMOVE_EXEC: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
120 121
    const {apiCall} = action.payload;
    const toDeleteIndex = state.exec && state.exec.page && apiCall.data && state.exec.page.data.content ? state.exec.page.data.content.findIndex((item) => apiCall.data.name === item.name) : -1;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
122 123 124 125 126 127

    return toDeleteIndex === -1 ? state
      :
      update(state, {
        exec: {
          page: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
128 129 130
            data: {
              content: {$splice: [[toDeleteIndex, 1]]},
            },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
131 132 133 134 135 136 137
          },
          details: {$set: null},
        },
      });
    }

    case ADMIN_RECEIVE_EXEC_DET: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
138
      const {apiCall} = action.payload;
Matija Obreza's avatar
Matija Obreza committed
139
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
140
        exec: { details: { $set: apiCall }, changes: { $set: null } },
Matija Obreza's avatar
Matija Obreza committed
141 142 143 144
      });
    }
    case ADMIN_RECEIVE_EXEC_LASTRUN: {
      // only update lastRun
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
145
      const {apiCall} = action.payload;
Matija Obreza's avatar
Matija Obreza committed
146
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
147
        exec: { details: { lastRun: { $set: apiCall.data } } as any },
Matija Obreza's avatar
Matija Obreza committed
148 149
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
150
    case ADMIN_RECEIVE_EXEC_RUNS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
151
      const { apiCall } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
152 153 154
      return update(state, {
        exec: {
          details: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
155 156 157
            data: {
              runs: {$set: apiCall.data},
            },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
158 159 160 161 162
          },
        },
      });
    }
    case ADMIN_APPEND_EXEC_RUNS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
163
      const { apiCall } = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
164 165 166
      return update(state, {
        exec: {
          details: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
167 168
            data: {
              runs: {$set: FilteredPage.merge(state.exec.details.data && state.exec.details.data.runs, apiCall.data)},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
169 170 171 172 173
            },
          },
        },
      });
    }
Oleksii Savran's avatar
Oleksii Savran committed
174 175 176 177 178 179 180
    case ADMIN_TOGGLE_EXEC_MODAL: {
      return update(state, {
        exec: {
          modalIsOpen: {$set: action.payload},
        },
      });
    }
Matija Obreza's avatar
Matija Obreza committed
181
    case ADMIN_RECEIVE_DIMS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
182 183
      const {apiCall} = action.payload;
      apiCall.data = apiCall.data && ensureDimensionType(apiCall.data) || apiCall.data;
Matija Obreza's avatar
Matija Obreza committed
184
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
185
        dim: { page: { $set: apiCall } },
Matija Obreza's avatar
Matija Obreza committed
186 187
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
188
    case ADMIN_RECEIVE_DIM: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
189
      const {apiCall} = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
190

191 192 193 194 195 196 197 198 199 200 201
      const updates: any = {
        dim: {
          details: {$set: apiCall},
        },
      };
      if (apiCall.data && state.dim.page && state.dim.page.data) {
        const receivedIndex = apiCall.data ? state.dim.page.data.content.findIndex((item) => item.id === apiCall.data.id) : -1;
        if (receivedIndex !== -1) {
          updates.dim.page = {
            data: {
              content: { [receivedIndex]: { $set: apiCall.data } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
202
            },
203 204 205 206 207
          };
        } else {
          updates.dim.page = {
            data: {
              content: {$push: [apiCall.data]},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
208
            },
209 210
          };
        }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
211
      }
212
      return update(state, updates);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
213
    }
214

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
215
    case ADMIN_REMOVE_DIM: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
216 217
      const {apiCall} = action.payload;
      const toDeleteIndex = state.dim && state.dim.page && state.dim.page.data && apiCall.data && state.dim.page.data.content ? state.dim.page.data.content.findIndex((item) => apiCall.data.name === item.name) : -1;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
218 219 220 221 222 223

      return toDeleteIndex === -1 ? state
        :
        update(state, {
          dim: {
            page: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
224 225 226
              data: {
                content: {$splice: [[toDeleteIndex, 1]]},
              },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
227 228 229 230 231
            },
            details: {$set: null},
          },
        });
    }
Oleksii Savran's avatar
Oleksii Savran committed
232 233 234 235 236 237 238
    case ADMIN_TOGGLE_DIM_MODAL: {
      return update(state, {
        dim: {
          modalIsOpen: {$set: action.payload},
        },
      });
    }
Matija Obreza's avatar
Matija Obreza committed
239
    case ADMIN_RECEIVE_PARAMS: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
240
      const {apiCall} = action.payload;
Matija Obreza's avatar
Matija Obreza committed
241
      return update(state, {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
242
        param: { page: { $set: apiCall } },
Matija Obreza's avatar
Matija Obreza committed
243 244
      });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
245
    case ADMIN_RECEIVE_PARAM: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
246
      const {apiCall} = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
247

248 249 250 251 252
      const updates: any = {
        param: {
          details: {$set: apiCall},
        },
      };
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
253

254 255 256 257 258 259 260
      // console.log(`Received param update`, apiCall);
      if (apiCall.data && state.param.page && state.param.page.data) {
        const receivedIndex = apiCall.data ? state.param.page.data.content.findIndex((item) => item.id === apiCall.data.id) : -1;
        if (receivedIndex !== -1) {
          updates.param.page = {
            data: {
              content: { [receivedIndex]: { $set: apiCall.data } },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
261
            },
262 263 264 265 266
          };
        } else {
          updates.param.page = {
            data: {
              content: {$push: [apiCall.data]},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
267
            },
268 269
          };
        }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
270
      }
271 272

      return update(state, updates);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
273 274
    }
    case ADMIN_REMOVE_PARAM: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
275 276
      const {apiCall} = action.payload;
      const toDeleteIndex = state.param && state.param.page && state.param.page.data && state.param.page.data.content && apiCall.data ? state.param.page.data.content.findIndex((item) => apiCall.data.name === item.name) : -1;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
277 278 279 280 281 282

      return toDeleteIndex === -1 ? state
        :
        update(state, {
          param: {
            page: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
283 284 285
              data: {
                content: {$splice: [[toDeleteIndex, 1]]},
              },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
286 287 288 289 290
            },
            details: {$set: null},
          },
        });
    }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
291
    case ADMIN_RECEIVE_EXEC_CHANGES: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
292
      const {apiCall} = action.payload;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
293 294
      return update(state, {
        exec: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
295
          changes: {$set: apiCall},
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
296 297 298 299
        },
      });
    }

Oleksii Savran's avatar
Oleksii Savran committed
300 301 302 303 304 305 306
    case ADMIN_TOGGLE_PARAM_MODAL: {
      return update(state, {
        param: {
          modalIsOpen: {$set: action.payload},
        },
      });
    }
Matija Obreza's avatar
Matija Obreza committed
307 308 309 310
    default:
      return state;
  }
}