admin.ts 4.59 KB
Newer Older
1
2
3
import * as _ from 'lodash';

// constants
4
5
6
7
8
9
import {
  ADMIN_APPEND_MATERIAL_REQUESTS,
  ADMIN_RECEIVE_MATERIAL_REQUEST,
  ADMIN_RECEIVE_MATERIAL_REQUEST_ACCESSIONS,
  ADMIN_RECEIVE_MATERIAL_REQUESTS,
} from 'requests/constants';
10

11
12
13
14
15
// actions
import navigateTo from 'actions/navigation';

// services
import RequestService from 'service/genesys/RequestService';
16
import AccessionService from 'service/genesys/AccessionService';
17

18
// models
19
import MaterialRequest from 'model/request/MaterialRequest';
20
import FilteredPage, { IPageRequest } from 'model/FilteredPage';
21
import Page from 'model/Page';
22
import MaterialRequestFilter from 'model/request/MaterialRequestFilter';
23
import Accession from 'model/accession/Accession';
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40


const receiveRequests = (paged: FilteredPage<MaterialRequest>, error = null) => ({
  type: ADMIN_RECEIVE_MATERIAL_REQUESTS,
  payload: { paged, error },
});

const appendRequests = (paged: FilteredPage<MaterialRequest>, error = null) => ({
  type: ADMIN_APPEND_MATERIAL_REQUESTS,
  payload: { paged, error },
});

const receiveRequest = (request: MaterialRequest, error = null) => ({
  type: ADMIN_RECEIVE_MATERIAL_REQUEST,
  payload: { request, error },
});

41
42
43
44
45
const receiveRequestAccessions = (accessions: Accession[]) => ({
  type: ADMIN_RECEIVE_MATERIAL_REQUEST_ACCESSIONS,
  payload: {accessions},
});

46
47
48
49
50
51
52
53
54
55
56
57
58
59

const refreshRequestPID = (request: MaterialRequest) => (dispatch, getState) => {
  const page = getState().requests.admin.paged;
  if (page) {
    const toUpdateIndex = _.findIndex(page.content, (contentItem) => contentItem.uuid === request.uuid);

    if (toUpdateIndex !== -1) {
      page.content[toUpdateIndex].body.pid = request.body.pid;
    }

    return dispatch(receiveRequests(page));
  }
};

60
61
export const loadMoreRequests = (paged?: FilteredPage<MaterialRequest>) => (dispatch) => {
  return RequestService.list(paged && paged.filterCode, Page.nextPage(paged))
62
63
64
65
66
67
    .then((page) => {
      if (page.number === 0) {
        dispatch(receiveRequests(page));
      } else {
        dispatch(appendRequests(page));
      }
Oleksii Savran's avatar
Oleksii Savran committed
68
      dispatch(updateRoute(paged));
69
70
71
72
73
74
75
    })
    .catch((error) => {
      dispatch(receiveRequests(null, error));
    });
};

export const loadMaterialRequest = (uuid: string) => (dispatch) => {
76
  return RequestService.getRequest(uuid)
77
    .then((request) => {
78
      dispatch(loadRequestAccessions(request));
79
80
81
82
83
84
85
86
      dispatch(receiveRequest(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};

export const recheckPidAction = (uuid: string) => (dispatch) => {
87
  return RequestService.recheckPid(uuid)
88
    .then((request) => {
89
      dispatch(loadRequestAccessions(request));
90
91
92
93
94
95
96
97
98
99
      dispatch(receiveRequest(request));
      dispatch(refreshRequestPID(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};


export const validateRequestAction = (uuid: string) => (dispatch) => {
100
  return RequestService.validateRequest(uuid)
101
    .then((request) => {
102
      dispatch(loadRequestAccessions(request));
103
104
105
106
107
108
109
110
      dispatch(receiveRequest(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};

export const sendValidationEmailAction = (uuid: string) => (dispatch) => {
111
  return RequestService.sendValidationEmail(uuid)
112
    .then((request) => {
113
      dispatch(loadRequestAccessions(request));
114
115
116
117
118
119
      dispatch(receiveRequest(request));
    })
    .catch((error) => {
      dispatch(receiveRequest(null, error));
    });
};
120

121
122
123
124
125
126
const loadRequestAccessions = (request: MaterialRequest) => (dispatch) => {
  return AccessionService.uuidFromIds(request.body.accessionIds)
    .then((uuids) => AccessionService.listAllByUuid(uuids)
      .then((accessions) => dispatch(receiveRequestAccessions(accessions))));
};

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
export const applyFilters = (filters: string | MaterialRequestFilter, page: IPageRequest = { page: 0 }) => (dispatch) => {
  return RequestService.list(filters, page)
    .then((paged) => {
      if (paged.number === 0) {
        dispatch(receiveRequests(paged));
      } else {
        dispatch(appendRequests(paged));
      }
      dispatch(updateRoute(paged));
    }).catch((error) => {
      console.log(`API error`, error);
      dispatch(receiveRequests(null, error));
    });
};

export const updateRoute = (paged: FilteredPage<MaterialRequest>) => (dispatch) => {
Oleksii Savran's avatar
Oleksii Savran committed
143
144
145
146
147
  const qs = {
    s: paged.sort[0].property === MaterialRequest.DEFAULT_SORT.property ? undefined : paged.sort[0].property,
    d: paged.sort[0].direction === MaterialRequest.DEFAULT_SORT.direction ? undefined : paged.sort[0].direction,
  };
  dispatch(navigateTo(paged.filterCode ? `/admin/requests/${paged.filterCode}` : '/admin/requests', qs));
148
149
};