index.tsx 4.96 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
import * as React from 'react';
Valeriy Panov's avatar
Valeriy Panov committed
2
3
4
5
6
7
8
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {withStyles} from 'material-ui/styles';
import Grid from 'material-ui/Grid';
import TextField from 'material-ui/TextField';
import Tabs, {Tab} from 'material-ui/Tabs';
import Paper from 'material-ui/Paper';
Matija Obreza's avatar
Matija Obreza committed
9

Valeriy Panov's avatar
Valeriy Panov committed
10
import Loading from 'ui/common/Loading';
11
import Container from 'ui/common/container';
Valeriy Panov's avatar
Valeriy Panov committed
12
13
14
15
16
import {searchDatasets, searchDescriptorLists, searchDescriptors} from 'actions/search';
import {Dataset, IDatasetFilter} from 'model/dataset.model';
import {Page} from 'model/common.model';
import {Descriptor, DescriptorList, IDescriptorFilter, IDescriptorListFilter} from 'model/descriptor.model';
import ContentHeader from 'ui/common/heading/ContentHeader';
Matija Obreza's avatar
Matija Obreza committed
17

Valeriy Panov's avatar
Valeriy Panov committed
18
19
20
21
22
23
24
25
26
27
interface IWelcomePageProps extends React.ClassAttributes<any> {
    classes: any;
    search: string;
    loading: boolean;
    pagedDatasets: Page<Dataset>;
    pagedDescriptors: Page<Descriptor>;
    pagedDescriptorLists: Page<DescriptorList>;
    searchDatasets: (page?: number, results?: number, sortBy?: string, filter?: IDatasetFilter) => any;
    searchDescriptors: (page?: number, results?: number, sortBy?: string, filter?: IDescriptorFilter) => any;
    searchDescriptorLists: (page?: number, results?: number, sortBy?: string, filter?: IDescriptorListFilter) => any;
Matija Obreza's avatar
Matija Obreza committed
28
29
}

Valeriy Panov's avatar
Valeriy Panov committed
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
const styles = (theme) => ({
    alignCenter: {
        textAlign: 'center',
    },
    paper: {
        margin: 20,
        height: 500,
        overflow: 'auto' as 'auto',
    },
});

class WelcomePage extends React.Component<IWelcomePageProps, any> {

    constructor(props: any, context: any) {
        super(props, context);

        this.state = {
            search: props.search || '',
            value: 0,
        };
    }

    protected search = (e) => {
        e.preventDefault();

        const {search, value} = this.state;
        const {searchDatasets, searchDescriptors, searchDescriptorLists} = this.props;

        if (!search) {
            return;
        }

        switch (value) {
            case 0:
                searchDatasets(0, 20, null, {_text: search});
                break;
            case 1:
                searchDescriptors(0, 20, null, {_text: search});
                break;
            case 2:
                searchDescriptorLists(0, 20, null, {_text: search});
                break;
        }
    }
Matija Obreza's avatar
Matija Obreza committed
74

Valeriy Panov's avatar
Valeriy Panov committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    protected onSearchChange = (e) => {
        const search = e.target.value;
        this.setState((state) => ({...state, search}));
    }

    protected handleChange = (event, value) => {
        this.setState((state) => ({...state, value}));
    }

    protected getContent = (value) => {
        const {pagedDatasets, pagedDescriptors, pagedDescriptorLists} = this.props;

        switch (value) {
            case 0:
                return pagedDatasets &&  JSON.stringify(pagedDatasets.content);
            case 1:
                return pagedDescriptors &&  JSON.stringify(pagedDescriptors.content);
            case 2:
                return pagedDescriptorLists && JSON.stringify(pagedDescriptorLists.content);
        }
    }

    public render() {
        const {classes, loading} = this.props;
        const {search, value} = this.state;

        return (
            <Container testid="counter" size={ 12 } center>
                <ContentHeader title="Welcome to the Genesys Catalog"/>
                <Grid container spacing={ 0 }>
                    <Grid item xs={ 12 } className={ classes.alignCenter }>
                        <form onSubmit={ this.search }>
                            <TextField
                                label="Search"
                                value={ search }
                                onChange={ this.onSearchChange }
                                margin="normal"
                            />
                        </form>
                    </Grid>
                    <Grid item xs={ 12 }>
                        <Tabs value={ value } onChange={ this.handleChange } centered>
                            <Tab label="Datasets"/>
                            <Tab label="Descriptors"/>
                            <Tab label="Descriptor lists"/>
                        </Tabs>
                        <Paper className={ classes.paper }>
                            { loading ? <Loading/> : this.getContent(value) }
                        </Paper>
                    </Grid>
                </Grid>
            </Container>
        );
    }
Matija Obreza's avatar
Matija Obreza committed
129
130
}

Valeriy Panov's avatar
Valeriy Panov committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const styled = withStyles(styles)(WelcomePage);

const mapStateToProps = (state, ownProps) => ({
    search: state.search.search,
    loading: state.search.loading,
    pagedDatasets: state.search.pagedDatasets,
    pagedDescriptors: state.search.pagedDescriptors,
    pagedDescriptorLists: state.search.pagedDescriptorLists,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
    searchDatasets,
    searchDescriptors,
    searchDescriptorLists,
}, dispatch);

Matija Obreza's avatar
Matija Obreza committed
147
export default connect(
Valeriy Panov's avatar
Valeriy Panov committed
148
149
150
    mapStateToProps,
    mapDispatchToProps,
)(styled);