connected seatch to wf catalog
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / catalog / CatalogView.jsx
1 /*
2 * Copyright © 2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http: //www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 import React, { Component } from 'react';
18 import PropTypes from 'prop-types';
19 import InfiniteScroll from 'shared/scroll/InfiniteScroll';
20 import Workflows from 'features/catalog/views/Workflows';
21 import AddWorkflow from 'features/catalog/views/AddWorkflow';
22
23 import Header from 'features/catalog/views/Header';
24 import Main from 'features/catalog/views/Main';
25 import { NAME, ASC, DESC } from 'features/catalog/catalogConstants';
26
27 class CatalogView extends Component {
28     constructor(props) {
29         super(props);
30         this.state = {
31             searchValue: ''
32         };
33     }
34
35     componentDidMount() {
36         const { clearWorkflow } = this.props;
37
38         clearWorkflow();
39     }
40
41     componentWillUnmount() {
42         this.props.handleResetWorkflow();
43     }
44
45     handleAlphabeticalOrderByClick = e => {
46         e.preventDefault();
47
48         const {
49             handleFetchWorkflow,
50             catalog: { sort }
51         } = this.props;
52
53         const payload = { ...sort };
54
55         payload[NAME] = payload[NAME] === ASC ? DESC : ASC;
56
57         handleFetchWorkflow(payload);
58     };
59
60     handleScroll = () => {
61         const {
62             catalog: {
63                 paging: { offset },
64                 sort
65             },
66             handleFetchWorkflow
67         } = this.props;
68
69         handleFetchWorkflow(sort, offset);
70     };
71
72     goToOverviewPage = id => {
73         const { history } = this.props;
74         history.push('/workflow/' + id + '/overview');
75     };
76
77     searchChange = searchValue => {
78         const { searchInputChanged, catalog } = this.props;
79         this.setState({ searchValue: searchValue });
80         searchInputChanged({
81             ...catalog,
82             searchNameFilter: searchValue
83         });
84     };
85
86     render() {
87         const { catalog, showNewWorkflowModal } = this.props;
88         const {
89             sort,
90             paging: { hasMore, total },
91             items
92         } = catalog;
93         const alphabeticalOrder = sort[NAME];
94
95         return (
96             <div className="wf-catalog">
97                 <Header
98                     searchChange={this.searchChange}
99                     searchValue={this.state.searchValue}
100                 />
101                 <InfiniteScroll
102                     useWindow={false}
103                     loadMore={this.handleScroll}
104                     hasMore={hasMore}>
105                     <Main
106                         total={total}
107                         alphabeticalOrder={alphabeticalOrder}
108                         onAlphabeticalOrderByClick={
109                             this.handleAlphabeticalOrderByClick
110                         }>
111                         <div className="main__content">
112                             <AddWorkflow onClick={showNewWorkflowModal} />
113                             <Workflows
114                                 items={items}
115                                 onWorkflowClick={this.goToOverviewPage}
116                             />
117                         </div>
118                     </Main>
119                 </InfiniteScroll>
120             </div>
121         );
122     }
123 }
124
125 CatalogView.propTypes = {
126     history: PropTypes.object,
127     catalog: PropTypes.object,
128     handleResetWorkflow: PropTypes.func,
129     handleFetchWorkflow: PropTypes.func,
130     showNewWorkflowModal: PropTypes.func,
131     clearWorkflow: PropTypes.func,
132     searchInputChanged: PropTypes.func
133 };
134
135 CatalogView.defaultProps = {
136     showNewWorkflowModal: () => {},
137     clearWorkflow: () => {}
138 };
139
140 export default CatalogView;