public.ts 2.55 KB
Newer Older
1
2
3
4
5
6
/**
 * This is a top-level group for actions in /subsets/* routes.
 */

// Actions
import navigateTo from 'actions/navigation';
Matija Obreza's avatar
Matija Obreza committed
7
import { showSnackbar } from 'actions/snackbar';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
8
import {createApiCaller} from 'actions/ApiCall';
9
10

// Constants
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
11
import {RECEIVE_SUBSET, APPEND_SUBSETS, APPEND_SUBSET_ACCESSIONS} from 'subsets/constants';
12
13
14

// Model
import FilteredPage, { IPageRequest } from 'model/FilteredPage';
15
16
import Subset from 'model/subset/Subset';
import SubsetFilter from 'model/subset/SubsetFilter';
17
import SubsetService from 'service/genesys/SubsetService';
18
import Page from 'model/Page';
19
import {AccessionRef} from 'model/accession/AccessionRef';
20

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
21
22
23
24
// Wrapped API Calls
const apiListSubsets = createApiCaller(SubsetService.list, APPEND_SUBSETS);
const apiListSubsetAccessions = createApiCaller(SubsetService.listAccessions, APPEND_SUBSET_ACCESSIONS);
const apiLoadSubset = createApiCaller(SubsetService.get, RECEIVE_SUBSET);
25
const apiRematchAccessions = createApiCaller(SubsetService.rematchAccessions, RECEIVE_SUBSET);
26

27
export const loadMoreSubsets = (paged: FilteredPage<Subset>) => (dispatch, getState) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
28
29
  return dispatch(apiListSubsets(paged ? paged.filterCode : '', Page.nextPage(paged)))
    .then((paged) => dispatch(updateRoute(paged)));
30
};
31

32
export const loadMoreAccessions = (uuid: string, paged?: Page<AccessionRef>) => (dispatch, getState) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
33
  return dispatch(apiListSubsetAccessions(uuid, Page.nextPage(paged)));
34
35
};

36
37
38
39
40
41
42
43
44
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,
  };
  dispatch(navigateTo(paged.filterCode ? `/subsets/${paged.filterCode}` : '/subsets', qs));
};

export const applyFilters = (filters: string | SubsetFilter, page: IPageRequest = { page: 0 }) => (dispatch) => {
45
  console.log('Applying new filter', filters, page);
Matija Obreza's avatar
Matija Obreza committed
46
  dispatch(showSnackbar('Applying filters...'));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
  return dispatch(apiListSubsets(filters, page))
48
49
    .then((paged) => {
      dispatch(updateRoute(paged));
Matija Obreza's avatar
Matija Obreza committed
50
      dispatch(showSnackbar(`Filters applied.`));
51
52
53
    });
};

54
55
56
57
58
59
60
61
62
63
export const rematchSubsetAccessions = (subset: Subset) => {
  return (dispatch, getState) => {
    return dispatch(apiRematchAccessions(subset.uuid, subset.version))
      .then((saved) => {
        dispatch(loadMoreAccessions(saved.uuid));
        return saved;
      });
  };
};

64
export const loadSubset = (uuid: string) => (dispatch) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
65
  return dispatch(apiLoadSubset(uuid));
66
67
68
};