descriptorList.ts 8.59 KB
Newer Older
1
2
3
import { push } from 'react-router-redux';

import { DescriptorListService } from 'service/DescriptorListService';
4
import { DescriptorList, Descriptor, IDescriptorListFilter } from 'model/descriptor.model';
5
import { Page } from 'model/common.model';
Valeriy Panov's avatar
Valeriy Panov committed
6
import {log} from 'utilities/debug';
Maxym Borodenko's avatar
Maxym Borodenko committed
7
8
import { addFilterCode } from 'actions/filterCode';
import {cleanFilters} from 'utilities';
9
10
11

import {
  GET_DESCRIPTORLIST, RECEIVE_DESCRIPTORLIST,
Matija Obreza's avatar
Matija Obreza committed
12
  CREATE_DESCRIPTORLIST,
13
14
15
16
17
  LIST_DESCRIPTORLISTS, RECEIVE_DESCRIPTORLISTS,
} from 'constants/descriptors';

const receiveDescriptorList = (descriptorList: DescriptorList) => ({
  type: RECEIVE_DESCRIPTORLIST,
18
  payload: descriptorList,
19
20
});

21
const receiveDescriptorLists = (paged: Page<DescriptorList>, page, results, sortBy, filter: IDescriptorListFilter, order) => ({
22
  type: RECEIVE_DESCRIPTORLISTS,
23
24
25
26
27
28
  payload: { paged, query: { page, results, sortBy, filter, order } },
});

const loadingDescriptorLists = (page, results, sortBy, filter, order) => ({
  type: LIST_DESCRIPTORLISTS,
  payload: { page, results, sortBy, filter, order },
29
30
31
});

// Just load a descriptor list
32
export const loadDescriptorList = (uuid: string, success?: (d) => any, fail?: (error) => any) => (dispatch, getState) => {
Valeriy Panov's avatar
Valeriy Panov committed
33
  log('Loading descriptor list', uuid);
34
  const token = getState().login.access_token;
35

36
37
  dispatch({ type: GET_DESCRIPTORLIST, payload: uuid });

38
  return DescriptorListService.loadDescriptorList(token, uuid)
39
    // receive the current descriptor list
40
41
42
    // // Demo long load
    // .then((descriptorList) => {
    //   await setTimeout(() => dispatch(receiveDescriptorList(descriptorList)), 10000);
43
    .then((descriptorList) => {
44
45
46
      if (success) {
        return success(descriptorList);
      }
47
      dispatch(receiveDescriptorList(descriptorList));
48
    }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
49
      log(`No descriptor list with uuid ${uuid}`, error);
50
51
52
      if (fail) {
        return fail(error);
      }
53
54
55
56
    });
};

// List current user's descriptor lists
57
export const listMyDescriptorLists = (page?, results?, sortBy?, filter?: string | IDescriptorListFilter, order?) => (dispatch, getState) => {
Valeriy Panov's avatar
Valeriy Panov committed
58
  log('Loading my descriptor lists');
59
  const token = getState().login.access_token;
60

61
62
  dispatch(loadingDescriptorLists(page, results, sortBy, filter, order));

63
  return DescriptorListService.listMyDescriptorLists(token, page, results, sortBy, filter, order)
64
65
    // receive the current descriptor list
    .then((descriptorLists) => {
66
67
      dispatch(receiveDescriptorLists(descriptorLists, page, results, sortBy, descriptorLists.filter, order));
      return dispatch(addFilterCode(descriptorLists.filterCode, descriptorLists.filter));
68
    }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
69
      log(`Error loading my descriptor lists`, error);
70
71
72
    });
};

Valeriy Panov's avatar
Valeriy Panov committed
73
74
75
76
77
78
79
80
81
82
export const promiseListDescriptorLists = (page?, results?, sortBy?, filter?, order?) => (dispatch, getState) => {
    const token = getState().login.access_token;

    return DescriptorListService.listDescriptorLists(token, page, results, sortBy, filter, order)
        .catch((error) => {
            log('Error', error);
            return error;
        });
};

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
export const promiseDescriptorList = (uuid) => (dispatch, getState) => {
    const token = getState().login.access_token;

    return DescriptorListService.loadDescriptorList(token, uuid)
        .catch((error) => {
            log('Error', error);
            return error;
        });
};

export const autocomplete = (term: string = '') => (dispatch, getState) => {
    const token = getState().login.access_token;

    return DescriptorListService.autocomplete(token, term)
        .catch((error) => {
            log('Error', error);
        });
};

102
// List published descriptor lists
103
export const listDescriptorLists = (page?, results?, sortBy?, filter?, order?) => (dispatch, getState) => {
Valeriy Panov's avatar
Valeriy Panov committed
104
  log('Loading published descriptor lists');
105
  const token = getState().login.access_token;
106

107
108
  dispatch(loadingDescriptorLists(page, results, sortBy, filter, order));

109
  return DescriptorListService.listDescriptorLists(token, page, results, sortBy, filter, order)
110
    // receive the current descriptor list
Maxym Borodenko's avatar
Maxym Borodenko committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    .then((paged) => {
      dispatch(receiveDescriptorLists(paged, page, results, sortBy, filter, order));
      return dispatch(addFilterCode(paged.filterCode, paged.filter));
    }).catch((error) => {
      log(`Error loading published descriptor lists`, error);
    });
};

// List published descriptor lists by code
export const listDescriptorListsByCode = (page?, results?, sortBy?, filterCode?, order?) => (dispatch, getState) => {
  log('Loading published descriptor lists by code');
  const token = getState().login.access_token;

  return DescriptorListService.listDescriptorListsByCode(token, page, results, sortBy, filterCode, order)
  // receive the current descriptor list
    .then((paged) => {
      const filter = cleanFilters(paged.filter, ['published']);
      dispatch(receiveDescriptorLists(paged, page, results, sortBy, filter as IDescriptorListFilter, order));
      return dispatch(addFilterCode(paged.filterCode, paged.filter));
130
    }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
131
      log(`Error loading published descriptor lists`, error);
132
133
134
135
136
137
    });
};

// Create a new descriptor list
export const createDescriptorList = () => (dispatch) => {
  dispatch({ type: CREATE_DESCRIPTORLIST });
Matija Obreza's avatar
Matija Obreza committed
138
  return dispatch(push(`/descriptorlists/edit`));
139
140
141
};

// Edit an existing descriptor list
142
export const editDescriptorList = (uuid: string) => (dispatch, getState) => {
143
  return dispatch(loadDescriptorList(uuid))
Matija Obreza's avatar
Matija Obreza committed
144
    .then((descriptorList) => dispatch(push(`/descriptorlists/${descriptorList.uuid}/edit`)));
145
146
147
};

// Save the descriptor list
148
export const saveDescriptorList = (descriptorList: DescriptorList) => (dispatch, getState) => {
149

Valeriy Panov's avatar
Valeriy Panov committed
150
  log('Saving descriptor list', descriptorList);
151
152
  const createOrSave = descriptorList.version ? DescriptorListService.updateDescriptorList : DescriptorListService.createDescriptorList;

153
  return createOrSave(getState().login.access_token, descriptorList)
154
155
156
157
158
    // when response is back
    .then((descriptorList) => {
      // receive the updated descriptor list
      dispatch(receiveDescriptorList(descriptorList));
    }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
159
      log(`Error saving descriptor list`, error, descriptorList);
160
161
162
    });
};

163
164
165
166
167
168
169
170
171
// Publish the descriptor list
export const deleteDescriptorList = (descriptorList: DescriptorList) => (dispatch, getState) => {
  return DescriptorListService.deleteDescriptorList(getState().login.access_token, descriptorList)
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
    });
};

172
// Publish the descriptor list
173
174
export const publishDescriptorList = (descriptorList: DescriptorList, published?: boolean) => (dispatch, getState) => {
  return DescriptorListService.publishDescriptorList(getState().login.access_token, descriptorList, published)
175
176
177
178
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
      // go to the published descriptor list page
Matija Obreza's avatar
Matija Obreza committed
179
      dispatch(push(`/descriptorlists/${descriptorList.uuid}`));
180
181
    });
};
182
183

// Add a descriptor to the descriptor list
184
185
export const addDescriptorToDescriptorList = (descriptorList: DescriptorList, descriptor: Descriptor) => (dispatch, getState) => {
  return DescriptorListService.addDescriptor(getState().login.access_token, descriptorList, [ descriptor.uuid ])
186
187
188
189
190
191
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
    });
};

192
193
194
195
196
197
198
199
200
// Add a descriptor to the descriptor list
export const addDescriptorsToDescriptorList = (descriptorList: DescriptorList, descriptorUuids: string[]) => (dispatch, getState) => {
  return DescriptorListService.addDescriptor(getState().login.access_token, descriptorList, descriptorUuids)
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
    });
};

201
202
203
204
205
206
207
208
209
// Add a descriptor to the descriptor list
export const setDescriptorsToDescriptorList = (descriptorList: DescriptorList, descriptorUuids: string[]) => (dispatch, getState) => {
    return DescriptorListService.setDescriptors(getState().login.access_token, descriptorList, descriptorUuids)
        .then((descriptorList) => {
            // receive updates
            dispatch(receiveDescriptorList(descriptorList));
        });
};

210
// Remove a descriptor to the descriptor list
211
212
export const removeDescriptorFromDescriptorList = (descriptorList: DescriptorList, descriptor: Descriptor) => (dispatch, getState) => {
  return DescriptorListService.removeDescriptor(getState().login.access_token, descriptorList, [ descriptor.uuid ])
213
214
215
216
217
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
    });
};