descriptors.ts 2.27 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
import * as update from 'immutability-helper';
2
import * as _ from 'lodash';
3
import { log } from 'utilities/debug';
Matija Obreza's avatar
Matija Obreza committed
4

5
6
import { IReducerAction, Page } from 'model/common.model';
import { Descriptor } from 'model/descriptor.model';
Matija Obreza's avatar
Matija Obreza committed
7

8
9
import { CREATE_DESCRIPTOR, RECEIVE_DESCRIPTOR, RECEIVE_DESCRIPTOR_EXTRA, RECEIVE_DESCRIPTOR_PAGE, GET_DESCRIPTOR, LIST_DESCRIPTORS } from 'constants/descriptors';
import { LOGIN_USER, LOGIN_APP, LOGOUT } from 'constants/login';
markchkhotua's avatar
markchkhotua committed
10

11
const INITIAL_STATE: {
12
13
14
15
16
  currentDescriptor: Descriptor;
  currentDescriptorExtra: any;
  paged: Page<Descriptor>;
  pagedQuery: any;
  loading: boolean;
Valeriy Panov's avatar
Valeriy Panov committed
17
  list: Descriptor[];
18
19
} = {
    currentDescriptor: null,
20
    currentDescriptorExtra: null,
21
22
23
    paged: null,
    pagedQuery: null,
    loading: null,
Valeriy Panov's avatar
Valeriy Panov committed
24
    list: [],
25
  };
markchkhotua's avatar
markchkhotua committed
26

27
function descriptors(state = INITIAL_STATE, action: IReducerAction) {
markchkhotua's avatar
markchkhotua committed
28

29
30
31
32
33
34
  switch (action.type) {
    case LOGIN_USER:
    case LOGIN_APP:
    case LOGOUT: {
      return update(state, { $set: INITIAL_STATE });
    }
35

36
37
38
39
40
    case GET_DESCRIPTOR: {
      return update(state, {
        loading: { $set: { uuid: action.payload } },
      });
    }
41

42
43
44
45
46
    case LIST_DESCRIPTORS: {
      return update(state, {
        loading: { $set: { uuid: null, ...action.payload } },
      });
    }
Matija Obreza's avatar
Matija Obreza committed
47

48
49
50
51
52
53
    case CREATE_DESCRIPTOR: {
      return update(state, {
        currentDescriptor: { $set: new Descriptor() },
        currentDescriptorExtra: { $set: null },
      });
    }
54

55
56
57
58
59
60
61
62
    case RECEIVE_DESCRIPTOR: {
      log(RECEIVE_DESCRIPTOR, action);
      return update(state, {
        currentDescriptor: { $set: action.payload },
        currentDescriptorExtra: { $set: null },
        loading: { $set: null },
      });
    }
63

64
65
66
67
68
69
70
71
    case RECEIVE_DESCRIPTOR_EXTRA: {
      log(RECEIVE_DESCRIPTOR, action);
      return update(state, {
        currentDescriptor: { $set: action.payload.descriptor },
        currentDescriptorExtra: { $set: action.payload.extra },
        loading: { $set: null },
      });
    }
72

73
74
75
76
77
    case RECEIVE_DESCRIPTOR_PAGE: {
      return update(state, {
        loading: { $set: null },
        paged: { $set: action.payload.paged },
        pagedQuery: { $set: _.cloneDeep(action.payload.query) },
Valeriy Panov's avatar
Valeriy Panov committed
78
        list: {$push: action.payload.paged.content},
79
      });
80
    }
81
82
83
84

    default:
      return state;
  }
markchkhotua's avatar
markchkhotua committed
85
86
87
}

export default descriptors;