summaryrefslogtreecommitdiffstats
path: root/src/app/contextHandler/ContextHandlerActions.js
blob: 5738f2ab2bd9f8677c27b21e50a02071ac84ce7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
import {
  POST,
  POST_HEADER
} from 'app/networking/NetworkConstants.js';
import networkCall from 'app/networking/NetworkCalls.js';
import {EXTERNAL_REQ_ENTITY_SEARCH_URL,
WRONG_EXTERNAL_REQUEST_MESSAGE,
  WRONG_RESULT
} from 'app/contextHandler/ContextHandlerConstants';
import {
  getSetGlobalMessageEvent,
  getClearGlobalMessageEvent
} from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';

import {  STATUS_CODE_204_NO_CONTENT,
  STATUS_CODE_3XX_REDIRECTION,
  MESSAGE_LEVEL_DANGER
} from 'utils/GlobalConstants.js';


import {
  contextHandlerActionTypes
} from 'app/contextHandler/ContextHandlerConstants.js';

function getExternalParamValues(urlParams) {
  var pairs = decodeURIComponent(urlParams).replace('?','').replace(/\r\n|\n/,'').split('&');

  var externalparamObject = {};
  pairs.forEach(function(pair) {
    pair = pair.split('=');
    externalparamObject[pair[0]] = pair[1] || '';
  });
  return externalparamObject;

}

function validateExternalParams(externalURLParams) {
  if(externalURLParams.view && externalURLParams.entityId && externalURLParams.entityType) {
    return true;
  }
  return false;

}


function createSuggestionFoundEvent(suggestion) {
  return {
    type: contextHandlerActionTypes.SINGLE_SUGGESTION_FOUND,
    data: suggestion
  };
}


function fetchDataForExternalRequest(fetchRequestCallback) {
  return dispatch => {
    return fetchRequestCallback().then(
      (response) => {
        if (response.status === STATUS_CODE_204_NO_CONTENT || response.status >= STATUS_CODE_3XX_REDIRECTION) {
          return Promise.reject(new Error(response.status));
        } else {
          // assume 200 status
          return response.json();
        }
      }
    ).then(
      (results)=> {
        if (results.suggestions !== undefined && results.suggestions.length === 1) {
          dispatch(getClearGlobalMessageEvent());
          dispatch(createSuggestionFoundEvent({suggestion: results.suggestions[0]}));
        } else {
          dispatch(getSetGlobalMessageEvent(WRONG_RESULT , MESSAGE_LEVEL_DANGER));
        }
      }
    ).catch(
      () => {
        dispatch(getSetGlobalMessageEvent(WRONG_RESULT , MESSAGE_LEVEL_DANGER));
      }
    );
  };
}

function validateAndFetchExternalParams(externalParams) {
  if(!validateExternalParams(externalParams)) {
    return dispatch => {
      dispatch(
        getSetGlobalMessageEvent(WRONG_EXTERNAL_REQUEST_MESSAGE, MESSAGE_LEVEL_DANGER));
    };
  } else {
    let postBody = JSON.stringify(externalParams);
    let externalfetchRequest =
      () => networkCall.fetchRequestObj(EXTERNAL_REQ_ENTITY_SEARCH_URL, POST,
        POST_HEADER, postBody);
    return dispatch => {
      dispatch(fetchDataForExternalRequest(externalfetchRequest));
    };
  }
}
export function externalUrlRequest(urlParams) {
  let externalURLParams = getExternalParamValues(urlParams);
  return dispatch => {
    dispatch(
      validateAndFetchExternalParams(externalURLParams));
  };
}
export function externalMessageRequest(jsonParams) {
  return dispatch => {
    dispatch(
      validateAndFetchExternalParams(jsonParams));
  };

}