search.ts 2.45 KB
Newer Older
Valeriy Panov's avatar
Valeriy Panov committed
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {DescriptorService} from 'service/DescriptorService';
import {DatasetService} from 'service/DatasetService';
import {DescriptorListService} from 'service/DescriptorListService';
import {Descriptor, DescriptorList} from 'model/descriptor.model';
import {Page} from 'model/common.model';
import {Dataset} from 'model/dataset.model';
import {
    SEARCH_DATASET_PAGE,
    SEARCH_DESCRIPTOR_PAGE,
    SEARCH_DESCRIPTOR_LIST_PAGE,
    NEW_SEARCH,
} from 'constants/search';
import {log} from 'utilities/debug';

const search = (search: string) => ({
    type: NEW_SEARCH,
    payload: { search },
});

const searchDatasetPage = (paged: Page<Dataset>) => ({
    type: SEARCH_DATASET_PAGE,
    payload: { paged },
});

const searchDescriptorPage = (paged: Page<Descriptor>) => ({
    type: SEARCH_DESCRIPTOR_PAGE,
    payload: { paged },
});

const searchDescriptorListPage = (paged: Page<DescriptorList>) => ({
    type: SEARCH_DESCRIPTOR_LIST_PAGE,
    payload: { paged },
});

export function searchDatasets(page?, results?, sortBy?, filter?, order?) {
    return (dispatch, getState) => {
        const token = getState().login.access_token;

        dispatch(search(filter._text));

        return DatasetService.listDatasets(token, page, results, sortBy, filter, order)
            .then((paged) => {
                return dispatch(searchDatasetPage(paged));
            })
            .catch((error) => {
                log('Error', error);
            });
    };
}

export function searchDescriptors(page?, results?, sortBy?, filter?, order?) {
    return (dispatch, getState) => {
        const token = getState().login.access_token;

        dispatch(search(filter._text));

        return DescriptorService.listDescriptors(token, page, results, sortBy, filter, order)
            .then((paged) => {
                return dispatch(searchDescriptorPage(paged));
            })
            .catch((error) => {
                log('Error', error);
            });
    };
}

export function searchDescriptorLists(page?, results?, sortBy?, filter?, order?) {
    return (dispatch, getState) => {
        const token = getState().login.access_token;

        dispatch(search(filter._text));

        return DescriptorListService.listDescriptorLists(token, page, results, sortBy, filter, order)
            .then((paged) => {
                return dispatch(searchDescriptorListPage(paged));
            })
            .catch((error) => {
                log('Error', error);
            });
    };
}