subsets.ts 7.08 KB
Newer Older
Matija Obreza's avatar
Subsets  
Matija Obreza committed
1
2
3
/**
 * This is a top-level group for actions in /subsets/* routes.
 */
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
4

Matija Obreza's avatar
Subsets  
Matija Obreza committed
5
import navigateTo from 'actions/navigation';
Matija Obreza's avatar
Matija Obreza committed
6
import {list as listSubsets, listMine as listMySubsets, get, update, addAccessions, remove, create, updatePublishState} from 'actions/genesys/subsetService';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
7
8
import {toUUIDPromise} from 'actions/accessions';

Matija Obreza's avatar
Subsets  
Matija Obreza committed
9
10
11
import FilteredPage, { IPageRequest } from 'model/FilteredPage';
import Subset from 'model/Subset';
import SubsetFilter from 'model/SubsetFilter';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
12
13
import {AccessionIdentifier} from 'model/dataset.model';
import {PublishState} from 'model/common.model';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
14
import Creator from 'model/Creator';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
15

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
16
import steps from 'ui/pages/dashboard/subsets/subset-stepper/steps';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
17
18
import {RECEIVE_SUBSETS, RECEIVE_SUBSET, APPEND_SUBSETS, REMOVE_SUBSET, REMOVE_CREATOR_FROM_SUBSET, UPDATE_SUBSET_CREATOR, ADD_CREATOR_TO_SUBSET} from 'constants/subsets';
import SubsetService from 'service/genesys/SubsetService';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
19
20
21
22
23
24

const receiveSubsets = (paged: FilteredPage<Subset>, error = null) => ({
  type: RECEIVE_SUBSETS,
  payload: { paged, error },
});

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
25
26
27
28
29
const appendSubsets = (paged: FilteredPage<Subset>, error = null) => ({
  type: APPEND_SUBSETS,
  payload: { paged, error },
});

Matija Obreza's avatar
Subsets  
Matija Obreza committed
30
31
32
33
34
const receiveSubset = (subset: Subset, error = null) => ({
  type: RECEIVE_SUBSET,
  payload: { subset, error },
});

Matija Obreza's avatar
Matija Obreza committed
35
export { listSubsets as listSubsetsPromise, listMySubsets as listMySubsetsPromise };
Matija Obreza's avatar
Subsets  
Matija Obreza committed
36

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
37
38
39
40
41
42
43
const removeSubset = (subset) => (dispatch) => {
  dispatch({
    type: REMOVE_SUBSET,
    payload: { subset },
  });
};

Matija Obreza's avatar
Subsets  
Matija Obreza committed
44
45
46
47
48
export const updateRoute = (paged: FilteredPage<Subset>) => (dispatch) => {
  const qs = {
    s: paged.sort[0].property === Subset.DEFAULT_SORT.property ? undefined : paged.sort[0].property,
    d: paged.sort[0].direction === Subset.DEFAULT_SORT.direction ? undefined : paged.sort[0].direction,
  };
49
  dispatch(navigateTo(paged.filterCode ? `/subsets/${paged.filterCode}` : '/subsets', qs));
Matija Obreza's avatar
Subsets  
Matija Obreza committed
50
51
52
53
54
55
56
57
58
};

export const applyFilters = (filters: string | SubsetFilter, page: IPageRequest = { page: 0 }) => (dispatch) => {
  console.log('Applying new filter', filters);
  return dispatch(listSubsets(filters, page))
    .then((paged) => {
      dispatch(receiveSubsets(paged));
      dispatch(updateRoute(paged));
    }).catch((error) => {
Matija Obreza's avatar
Matija Obreza committed
59
      console.log(`API error`, error);
60
      dispatch(receiveSubsets(null, error));
Matija Obreza's avatar
Subsets  
Matija Obreza committed
61
62
63
64
65
66
67
    });
};

export const loadSubsetsPage = (page: IPageRequest) => (dispatch, getState) => {
  const filterCode = getState().subsets.paged.filterCode;
  return dispatch(listSubsets(filterCode, page))
    .then((paged) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
68
69
70
71
72
      if (paged.number === 0) {
        dispatch(receiveSubsets(paged));
      } else {
        dispatch(appendSubsets(paged));
      }
Matija Obreza's avatar
Subsets  
Matija Obreza committed
73
      dispatch(updateRoute(paged));
Matija Obreza's avatar
Matija Obreza committed
74
75
    }).catch((error) => {
      console.log(`API error`, error);
76
      dispatch(receiveSubsets(null, error));
Matija Obreza's avatar
Subsets  
Matija Obreza committed
77
78
79
    });
};

Matija Obreza's avatar
Matija Obreza committed
80
81
export const loadMySubsets = (page: IPageRequest) => (dispatch, getState) => {
    return dispatch(listMySubsets('', page))
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
82
83
84
85
        .then((paged) => {
            dispatch(receiveSubsets(paged));
        }).catch((error) => {
            console.log(`API error`, error);
86
            dispatch(receiveSubsets(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
87
88
89
        });
};

Matija Obreza's avatar
Subsets  
Matija Obreza committed
90
91
92
93
94
export const loadSubset = (uuid: string) => (dispatch) => {
  return dispatch(get(uuid))
    .then((subset) => {
      dispatch(receiveSubset(subset));
    }).catch((error) => {
Matija Obreza's avatar
Matija Obreza committed
95
      console.log(`API error`, error);
96
      dispatch(receiveSubset(null, error));
Matija Obreza's avatar
Subsets  
Matija Obreza committed
97
98
    });
};
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

export const createSubset = () => (dispatch) => {
    dispatch(receiveSubset(new Subset()));
};

export const saveSubset = (subset: Subset) => (dispatch) => {
    if (! subset.uuid) {
      subset.publisher = subset.wiewsCode;
      return dispatch(create(subset))
            .then((subset) => {
                dispatch(receiveSubset(subset));
                dispatch(gotoNextStep(subset));
            });
    }
    return dispatch(update(subset))
        .then((subset) => dispatch(receiveSubset(subset)));
};
export const updateSubsetAccessionIdentifiers = (subset: Subset, accessionIdentifiers: AccessionIdentifier[]) => (dispatch) => {
    dispatch(toUUIDPromise(accessionIdentifiers))
      .then((accessionIdentifiers) => {
        dispatch(addAccessions(subset.uuid, subset.version, Object.keys(accessionIdentifiers)))
          .then((subset) => {
            dispatch(receiveSubset(subset));
          });
      });
};

export const publishSubset = (subset: Subset) => (dispatch) => {
   return dispatch(updatePublishState(subset, PublishState.REVIEWING))
        .then((subset) => {
            dispatch(receiveSubset(subset));
        });
};
export const unpublishSubset = (subset: Subset) => (dispatch) => {
   return dispatch(updatePublishState(subset, PublishState.DRAFT))
        .then((subset) => {
            dispatch(receiveSubset(subset));
        });
};

export const approveSubset = (subset: Subset) => (dispatch) => {
  return dispatch(updatePublishState(subset, PublishState.PUBLISHED))
    .then((subset) => {
      dispatch(receiveSubset(subset));
    });
};

export const deleteSubset = (subset: Subset) => (dispatch) => {
  return dispatch(remove(subset.uuid, subset.version))
    .then((subset) => {
      dispatch(removeSubset(subset));
    });
};

const gotoNextStep = (subset: Subset) => {
    return (dispatch, getState) => {
        const link = window.location.pathname.split('/').pop();
        const stepId = steps.find((e) => e.link.endsWith(link)).id;
        const path = steps.find((e) => e.id === (stepId + 1)).link;
        dispatch(navigateTo(`/subsets/${subset.uuid}/${path}`));
    };
};

export const remoteSubmit = (values: Subset, dispatch) => {
    dispatch(saveSubset(values));
};
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

// Creators
const addCreatorToSubset = (creator: Creator) => ({
    type: ADD_CREATOR_TO_SUBSET,
    payload: { creator },
});

export const createSubsetCreator = () => (dispatch, getState) => {
    const currentSubset = getState().subsets.subset;
    return SubsetService.createCreator(getState().login.access_token, currentSubset.uuid)
      .then((obj) => {
        dispatch(addCreatorToSubset(obj));
      }).catch((error) => {
        console.log('Create creator error', error);
      });
};

const updateCreator = (creator: Creator) => ({
    type: UPDATE_SUBSET_CREATOR,
    payload : { creator },
});

export const updateSubsetCreatorRequest = (creator: Creator) => (dispatch, getState) => {
    const currentSubset = getState().subsets.subset;
    const token = getState().login.access_token;
    return SubsetService.updateCreator(token, currentSubset.uuid, creator)
      .then((obj) => {
        dispatch(updateCreator(obj));
      }).catch((error) => {
        console.log('Update creator error', error);
      });
};

const removeCreator = (creator: Creator) => ({
    type: REMOVE_CREATOR_FROM_SUBSET,
    payload : { creator },
});

export const deleteSubsetCreatorRequest = (creator: Creator) => (dispatch, getState) => {
    const token = getState().login.access_token;
    const currentSubset = getState().subsets.subset;
    return SubsetService.deleteCreator(token, currentSubset.uuid, creator)
      .then((obj) => {
        dispatch(removeCreator(obj));
      }).catch((error) => {
        console.log('Delete creator error', error);
      });
};