dashboard.ts 1.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Constants
import {DASHBOARD_APPEND_ACCESSIONS, DASHBOARD_RECEIVE_ACCESSIONS} from 'accessions/constants';

// Model
import FilteredPage, {IPageRequest} from 'model/FilteredPage';
import Accession from 'model/accession/Accession';
import AccessionFilter from 'model/accession/AccessionFilter';
// Service
import AccessionService from 'service/genesys/AccessionService';


const receiveAccessions = (paged: FilteredPage<Accession>, error = null) => ({
  type: DASHBOARD_RECEIVE_ACCESSIONS,
  payload: { paged, error },
});

const appendAccessions = (paged: FilteredPage<Accession>, error = null) => ({
  type: DASHBOARD_APPEND_ACCESSIONS,
  payload: { paged, error },
});

// Load accession page without redirect
export const loadAccessionsPageAction = (page: IPageRequest, filterCode: string | AccessionFilter) => (dispatch, getState) => {
  return AccessionService.list(filterCode, page)
    .then((paged) => {
      if (paged.number === 0) {
        dispatch(receiveAccessions(paged));
      } else {
        dispatch(appendAccessions(paged));
      }
    }).catch((error) => {
      console.log(`API error`, error);
      dispatch(receiveAccessions(null, error.response));
    });
};