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