Adding filter bar
[aai/sparky-fe.git] / src / app / globalAutoCompleteSearchBar / GlobalAutoCompleteSearchBarActions.js
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 {getTSUIElasticSearchQueryString} from 'app/networking/NetworkUtil.js';
27 import networkCall from 'app/networking/NetworkCalls.js';
28 import {
29   POST,
30   POST_HEADER,
31   ERROR_RETRIEVING_DATA
32 } from 'app/networking/NetworkConstants.js';
33
34 import {
35   globalAutoCompleteSearchBarActionTypes,
36   ERROR_INVALID_SEARCH_TERMS,
37   GLOBAL_SEARCH_URL
38 } from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarConstants.js';
39 import {
40   getSetGlobalMessageEvent,
41   getClearGlobalMessageEvent
42 } from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';
43
44
45 function createSuggestionFoundEvent({suggestions}) {
46   return {
47     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
48     data: {suggestions}
49   };
50 }
51
52 function createSuggestionNotFoundEvent() {
53   return {
54     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
55   };
56 }
57
58 function createSuggestionChangedEvent(value) {
59   return {
60     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
61     data: value
62   };
63 }
64
65 function getInvalidQueryEvent(value) {
66   return {
67     type: globalAutoCompleteSearchBarActionTypes.NETWORK_ERROR,
68     data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
69   };
70 }
71
72 function getSearchBarWarningMessageEvent(message) {
73   return {
74     type: globalAutoCompleteSearchBarActionTypes.SEARCH_WARNING_EVENT,
75     data: {errorMsg: message}
76   };
77 }
78 export function getInvalidSearchInputEvent(value) {
79   return getSearchBarWarningMessageEvent(
80     ERROR_INVALID_SEARCH_TERMS + ': ' + value);
81 }
82
83
84 function fetchView(selectedSuggestion) {
85   return {
86     type: globalAutoCompleteSearchBarActionTypes.SUGGESTION_CLICKED,
87     data: {selectedSuggestion: selectedSuggestion}
88   };
89 }
90
91
92 export function populateView(
93   searchRequestObject, keyWord, selectedNodeFetchRequest) {
94   if (selectedNodeFetchRequest === undefined) {
95     let postBody = JSON.stringify(searchRequestObject);
96     selectedNodeFetchRequest =
97       () => networkCall.fetchRequest('', POST,
98         POST_HEADER, postBody);
99   }
100
101   return dispatch => {
102     dispatch(fetchView(searchRequestObject));
103   };
104 }
105
106
107 export function fetchRequestedValues(fetchRequestCallback, keyWord) {
108   return dispatch => {
109     return fetchRequestCallback().then(
110       (responseJson) => responseJson.suggestions
111     ).then(
112       (filteredResults)=> {
113         if (filteredResults.length > 0) {
114           dispatch(createSuggestionFoundEvent({suggestions: filteredResults}));
115         } else {
116           dispatch(createSuggestionNotFoundEvent());
117         }
118       }
119     ).catch(
120       () => {
121         dispatch(getInvalidQueryEvent(keyWord));
122       }
123     );
124   };
125 }
126
127 export function queryRequestedValues(keyWord, requestedFetchRequest) {
128   if (requestedFetchRequest === undefined) {
129     let postBody = JSON.stringify(getTSUIElasticSearchQueryString(keyWord));
130     requestedFetchRequest =
131       () => networkCall.fetchRequest(GLOBAL_SEARCH_URL, POST, POST_HEADER,
132         postBody);
133   }
134   return dispatch => {
135     dispatch(fetchRequestedValues(requestedFetchRequest, keyWord), keyWord);
136   };
137 }
138
139 export function clearSuggestionsTextField() {
140   return dispatch => {
141     dispatch(
142       {type: globalAutoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD});
143   };
144 }
145
146 export function onSuggestionsChange(event, value) {
147   return dispatch => {
148     dispatch(createSuggestionChangedEvent(value));
149     //Only fetch values if the enter key is used.
150     if (event.keyCode === 13) {
151       let postBody = JSON.stringify(getTSUIElasticSearchQueryString(value));
152       dispatch(fetchRequestedValues(
153         () => networkCall.fetchRequest(GLOBAL_SEARCH_URL, POST, POST_HEADER,
154           postBody), value));
155     }
156   };
157 }
158
159 export function onSuggestionsClearRequested() {
160   return dispatch => {
161     dispatch({type: globalAutoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS});
162   };
163 }
164
165 export function setNotificationText(msgText, msgSeverity) {
166   if (msgText.length > 0) {
167     return dispatch => {
168       dispatch(
169         getSetGlobalMessageEvent(msgText, msgSeverity));
170     };
171   } else {
172     return dispatch => {
173       dispatch(getClearGlobalMessageEvent());
174     };
175   }
176 }