search.ts 1.63 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
import * as update from 'immutability-helper';

import {
    SEARCH_DATASET_PAGE,
    SEARCH_DESCRIPTOR_PAGE,
    SEARCH_DESCRIPTOR_LIST_PAGE,
    NEW_SEARCH,
} from 'constants/search';

import {Dataset} from 'model/dataset.model';
import {Page} from 'model/common.model';
import {Descriptor, DescriptorList} from 'model/descriptor.model';

const INITIAL_STATE: {
    search: string;
    loading: boolean;
    pagedDatasets: Page<Dataset>;
    pagedDescriptors: Page<Descriptor>;
    pagedDescriptorLists: Page<DescriptorList>;
} = {
    search: null,
    loading: false,
    pagedDatasets: null,
    pagedDescriptors: null,
    pagedDescriptorLists: null,
};

function search(state = INITIAL_STATE, action: { type?: string, payload?: any } = {type: '', payload: {}}) {

    switch (action.type) {
        case NEW_SEARCH: {
            return update(state, {
                search: {$set: action.payload.search},
                loading: {$set: true},
            });
        }

        case SEARCH_DATASET_PAGE: {
            return update(state, {
                pagedDatasets: {$set: action.payload.paged},
                loading: {$set: false},
            });
        }
        case SEARCH_DESCRIPTOR_PAGE: {
            return update(state, {
                pagedDescriptors: {$set: action.payload.paged},
                loading: {$set: false},
            });
        }
        case SEARCH_DESCRIPTOR_LIST_PAGE: {
            return update(state, {
                pagedDescriptorLists: {$set: action.payload.paged},
                loading: {$set: false},
            });
        }

        default:
            return state;
    }
}

export default search;