1f52c08a1eaca71c3e9213eb1fe4695f80be2d6f
[aai/sparky-fe.git] / src / app / globalAutoCompleteSearchBar / GlobalAutoCompleteSearchBarReducer.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 i18n from 'utils/i18n/i18n';
27
28 import {
29   globalAutoCompleteSearchBarActionTypes,
30   NO_MATCHES_FOUND
31 } from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarConstants.js';
32 import {
33   MESSAGE_LEVEL_WARNING,
34   MESSAGE_LEVEL_DANGER
35 } from 'utils/GlobalConstants.js';
36
37 export default (state = {}, action) => {
38   switch (action.type) {
39     case globalAutoCompleteSearchBarActionTypes.SUGGESTION_FOUND:
40       return {
41         ...state,
42         suggestions: action.data.suggestions,
43         cachedSuggestions: action.data.suggestions,
44         feedbackMsgText: action.data.errorMsg,
45         feedbackMsgSeverity: MESSAGE_LEVEL_DANGER
46       };
47     case globalAutoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND:
48       return {
49         ...state,
50         suggestions: [{ text: i18n(NO_MATCHES_FOUND)}],
51         cachedSuggestions: [{
52           entityType: i18n(NO_MATCHES_FOUND)
53         }],
54         feedbackMsgText: '',
55         feedbackMsgSeverity: ''
56       };
57     case globalAutoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD:
58       return {
59         ...state,
60         suggestions: [],
61         cachedSuggestions: [],
62         value: '',
63         feedbackMsgText: '',
64         feedbackMsgSeverity: '',
65         clearSearchText: false
66       };
67     case globalAutoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS:
68       return {
69         ...state,
70         suggestions: []
71       };
72     case globalAutoCompleteSearchBarActionTypes.SUGGESTION_CHANGED:
73       return {
74         ...state,
75         value: action.data,
76         feedbackMsgText: '',
77         feedbackMsgSeverity: ''
78       };
79     case globalAutoCompleteSearchBarActionTypes.SUGGESTION_CLICKED:
80       return {
81         ...state,
82         selectedSuggestion: action.data.selectedSuggestion,
83         performPrepareVisualization: true,
84         feedbackMsgText: '',
85         feedbackMsgSeverity: ''
86       };
87     case globalAutoCompleteSearchBarActionTypes.NETWORK_ERROR:
88       return {
89         ...state,
90         suggestions: [],
91         cachedSuggestions: [],
92         feedbackMsgText: action.data.errorMsg,
93         feedbackMsgSeverity: MESSAGE_LEVEL_DANGER
94       };
95     case globalAutoCompleteSearchBarActionTypes.SEARCH_WARNING_EVENT:
96       return {
97         ...state,
98         suggestions: [],
99         cachedSuggestions: [],
100         feedbackMsgText: action.data.errorMsg,
101         feedbackMsgSeverity: MESSAGE_LEVEL_WARNING
102       };
103   }
104   return state;
105 };