descriptorList.ts 6.88 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';
7
8
9

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

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

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

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

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

34
35
  dispatch({ type: GET_DESCRIPTORLIST, payload: uuid });

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

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

59
60
  dispatch(loadingDescriptorLists(page, results, sortBy, filter, order));

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

// List published descriptor lists
71
export const listDescriptorLists = (page?, results?, sortBy?, filter?, order?) => (dispatch, getState) => {
Valeriy Panov's avatar
Valeriy Panov committed
72
  log('Loading published descriptor lists');
73
  const token = getState().login.access_token;
74

75
76
  dispatch(loadingDescriptorLists(page, results, sortBy, filter, order));

77
  return DescriptorListService.listDescriptorLists(token, page, results, sortBy, filter, order)
78
79
    // receive the current descriptor list
    .then((descriptorLists) => {
80
      return dispatch(receiveDescriptorLists(descriptorLists, page, results, sortBy, filter, order));
81
    }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
82
      log(`Error loading published descriptor lists`, error);
83
84
85
86
87
88
    });
};

// Create a new descriptor list
export const createDescriptorList = () => (dispatch) => {
  dispatch({ type: CREATE_DESCRIPTORLIST });
Matija Obreza's avatar
Matija Obreza committed
89
  return dispatch(push(`/descriptorlists/edit`));
90
91
92
};

// Edit an existing descriptor list
93
export const editDescriptorList = (uuid: string) => (dispatch, getState) => {
94
  return dispatch(loadDescriptorList(uuid))
Matija Obreza's avatar
Matija Obreza committed
95
    .then((descriptorList) => dispatch(push(`/descriptorlists/${descriptorList.uuid}/edit`)));
96
97
98
};

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

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

104
  return createOrSave(getState().login.access_token, descriptorList)
105
106
107
108
109
    // when response is back
    .then((descriptorList) => {
      // receive the updated descriptor list
      dispatch(receiveDescriptorList(descriptorList));
      // and redirect to proper edit page
Matija Obreza's avatar
Matija Obreza committed
110
      return dispatch(push(`/descriptorlists/${descriptorList.uuid}/edit`));
111
    }).catch((error) => {
Valeriy Panov's avatar
Valeriy Panov committed
112
      log(`Error saving descriptor list`, error, descriptorList);
113
114
115
    });
};

116
117
118
119
120
121
122
123
124
125
126
// 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));
      // go to the published descriptor list page
      dispatch(push(`/descriptorlist`));
    });
};

127
// Publish the descriptor list
128
129
export const publishDescriptorList = (descriptorList: DescriptorList, published?: boolean) => (dispatch, getState) => {
  return DescriptorListService.publishDescriptorList(getState().login.access_token, descriptorList, published)
130
131
132
133
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
      // go to the published descriptor list page
Matija Obreza's avatar
Matija Obreza committed
134
      dispatch(push(`/descriptorlists/${descriptorList.uuid}`));
135
136
    });
};
137
138

// Add a descriptor to the descriptor list
139
140
export const addDescriptorToDescriptorList = (descriptorList: DescriptorList, descriptor: Descriptor) => (dispatch, getState) => {
  return DescriptorListService.addDescriptor(getState().login.access_token, descriptorList, [ descriptor.uuid ])
141
142
143
144
145
146
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
    });
};

147
148
149
150
151
152
153
154
155
// 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));
    });
};

156
157
158
159
160
161
162
163
164
// 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));
        });
};

165
// Remove a descriptor to the descriptor list
166
167
export const removeDescriptorFromDescriptorList = (descriptorList: DescriptorList, descriptor: Descriptor) => (dispatch, getState) => {
  return DescriptorListService.removeDescriptor(getState().login.access_token, descriptorList, [ descriptor.uuid ])
168
169
170
171
172
    .then((descriptorList) => {
      // receive updates
      dispatch(receiveDescriptorList(descriptorList));
    });
};