f29de3e97d10beeb825a0b407b1f9fe79979554c
[aai/sparky-fe.git] / src / app / globalAutoCompleteSearchBar / GlobalAutoCompleteSearchBar.jsx
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 import {connect} from 'react-redux';
24 import React, {Component} from 'react';
25 import  AutoCompleteSearchBar from 'generic-components/autoCompleteSearchBar/AutoCompleteSearchBar.jsx';
26 import {postAnalyticsData} from 'app/analytics/AnalyticsActions.js';
27 import {getClearGlobalMessageEvent} from 'app/GlobalInlineMessageBar/GlobalInlineMessageBarActions.js';
28 import {
29   queryRequestedValues,
30   clearSuggestionsTextField,
31   onSuggestionsChange,
32   onSuggestionsClearRequested,
33   getInvalidSearchInputEvent,
34   setNotificationText
35 } from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions.js';
36
37 let mapActionToProps = (dispatch) => {
38   return {
39     onSuggestionsFetchRequested: ({value}) => dispatch(
40       queryRequestedValues(value)),
41     onClearSuggestionsTextFieldRequested: () => {
42       dispatch(getClearGlobalMessageEvent());
43       dispatch(clearSuggestionsTextField());
44     },
45     onInputChange: (event, {newValue}) => {
46       dispatch(getClearGlobalMessageEvent());
47       dispatch(onSuggestionsChange(event, newValue));
48     },
49     onSuggestionsClearRequested: () => dispatch(onSuggestionsClearRequested()),
50     dispatchAnalytics: () => dispatch(
51       postAnalyticsData(document.documentElement.outerHTML.replace('\s+', ''))),
52     onInvalidSearch: (searchText) => {
53       dispatch(getInvalidSearchInputEvent(searchText));
54     },
55     onMessageStateChange: (msgText, msgSeverity) => {
56       dispatch(setNotificationText(msgText, msgSeverity));
57     }
58   };
59 };
60
61 let mapStateToProps = ({globalAutoCompleteSearchBarReducer}) => {
62   let {
63         value = '',
64         suggestions = [],
65         cachedSuggestions = [],
66         suggestionName = 'text',
67         clearSearchText = false,
68         feedbackMsgText = '',
69         feedbackMsgSeverity = ''
70       } = globalAutoCompleteSearchBarReducer;
71   
72   return {
73     value,
74     suggestions,
75     cachedSuggestions,
76     suggestionName,
77     clearSearchText,
78     feedbackMsgText,
79     feedbackMsgSeverity
80   };
81 };
82
83 export class GlobalAutoCompleteSearchBar extends Component {
84   componentWillReceiveProps(nextProps) {
85     if (nextProps.clearSearchText) {
86       this.props.onClearSuggestionsTextFieldRequested();
87     }
88     
89     if (nextProps.feedbackMsgText !== this.props.feedbackMsgText) {
90       this.props.onMessageStateChange(nextProps.feedbackMsgText,
91         nextProps.feedbackMsgSeverity);
92     }
93   }
94   
95   render() {
96     return (
97       <AutoCompleteSearchBar {...this.props} />
98     );
99   }
100 }
101 export default connect(mapStateToProps, mapActionToProps)(
102   GlobalAutoCompleteSearchBar);