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