institutes.ts 2.63 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
import { list as listInstitutes, get } from 'actions/genesys/instituteService';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
2
import {APPEND_INSTITUTES, RECEIVE_INSTITUTE, RECEIVE_INSTITUTES} from 'constants/institutes';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
4
5
6
7
8
9
10
11
12
13
14

import FaoInstitute from 'model/FaoInstitute';
import FaoInstituteFilter from 'model/FaoInstituteFilter';
import FilteredPage, {IPageRequest} from 'model/FilteredPage';

import navigateTo from './navigation';

const receiveInstitutes = (paged: FilteredPage<FaoInstitute>, error = null) => ({
    type: RECEIVE_INSTITUTES,
    payload: { paged, error },
});

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
15
16
17
18
19
const appendInstitutes = (paged: FilteredPage<FaoInstitute>, error = null) => ({
    type: APPEND_INSTITUTES,
    payload: { paged, error },
});

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const receiveInstitute = (institute: FaoInstitute, error = null) => ({
    type: RECEIVE_INSTITUTE,
    payload: { institute, error },
});

export const updateRoute = (paged: FilteredPage<FaoInstitute>) => (dispatch) => {
    const qs = {
        s: paged.sort[0].property === FaoInstitute.DEFAULT_SORT.property ? undefined : paged.sort[0].property,
        d: paged.sort[0].direction === FaoInstitute.DEFAULT_SORT.direction ? undefined : paged.sort[0].direction,
    };
    dispatch(navigateTo(paged.filterCode ? `/wiews/${paged.filterCode}` : '/wiews', qs));
};


export { listInstitutes as listInstitutesPromise };

export const applyFilters = (filters: string | FaoInstituteFilter, page: IPageRequest = { page: 0 }) => (dispatch) => {
    console.log('Applying new filter', filters);
    return dispatch(listInstitutes(filters, page))
        .then((paged) => {
            dispatch(receiveInstitutes(paged));
            dispatch(updateRoute(paged));
        }).catch((error) => {
            console.log(`API error`, error);
44
            dispatch(receiveInstitutes(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
45
46
47
48
49
50
51
        });
};

export const loadInstitutesPage = (page: IPageRequest) => (dispatch, getState) => {
    const filterCode = getState().institutes.paged.filterCode;
    return dispatch(listInstitutes(filterCode, page))
        .then((paged) => {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
52
53
54
55
56
            if (paged.number === 0) {
                dispatch(receiveInstitutes(paged));
            } else {
                dispatch(appendInstitutes(paged));
            }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
57
58
59
            dispatch(updateRoute(paged));
        }).catch((error) => {
            console.log(`API error`, error);
60
            dispatch(receiveInstitutes(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
61
62
63
64
65
66
67
68
69
        });
};

export const loadInstitute = (code: string) => (dispatch) => {
    return dispatch(get(code))
        .then((institute) => {
            dispatch(receiveInstitute(institute));
        }).catch((error) => {
            console.log(`API error`, error);
70
            dispatch(receiveInstitute(null, error));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
71
72
        });
};