talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | * or implied. See the License for the specific language governing |
| 14 | * permissions and limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
| 18 | import Configuration from 'sdc-app/config/Configuration.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 19 | import { actionTypes } from './UsersConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 20 | |
| 21 | function getUserId() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 22 | let catalogApiHeaders = Configuration.get('CatalogApiHeaders'); |
| 23 | let User = catalogApiHeaders && catalogApiHeaders.userId; |
| 24 | let userId = User && User.value ? User.value : ''; |
| 25 | return userId; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | function baseUrl() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | const restCatalogPrefix = Configuration.get('restCatalogPrefix'); |
| 30 | return `${restCatalogPrefix}`; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 31 | } |
| 32 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 33 | function fetchUsersList() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 34 | const url = '/v1/user/users'; |
| 35 | return RestAPIUtil.fetch(`${baseUrl()}${url}`); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 36 | } |
| 37 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 38 | const UsersActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 39 | fetchUsersList(dispatch) { |
| 40 | fetchUsersList().then(response => { |
| 41 | dispatch({ |
| 42 | type: actionTypes.USERS_LIST_LOADED, |
| 43 | usersList: response |
| 44 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 45 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 46 | let userId = getUserId(); |
| 47 | let userInfo = response.find(user => user.userId === userId); |
| 48 | dispatch({ |
| 49 | type: actionTypes.GOT_USER_INFO, |
| 50 | userInfo |
| 51 | }); |
| 52 | }); |
| 53 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | export default UsersActionHelper; |