3f9eb17db34e14088fda97751bc5f18348b24513
[sdc.git] / openecomp-ui / src / nfvo-components / loader / LoaderReducer.js
1 /*
2  * Copyright © 2016-2017 European Support Limited
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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import { actionTypes } from './LoaderConstants.js';
17
18 export default (
19     state = { fetchingRequests: 0, currentlyFetching: [], isLoading: false },
20     action
21 ) => {
22     let fetchingRequests = state.fetchingRequests;
23     let newArray;
24     switch (action.type) {
25         case actionTypes.SEND_REQUEST:
26             fetchingRequests++;
27             newArray = state.currentlyFetching.slice();
28             newArray.splice(0, 0, action.url);
29             return {
30                 fetchingRequests: fetchingRequests,
31                 currentlyFetching: newArray,
32                 isLoading: true
33             };
34         case actionTypes.RECEIVE_RESPONSE:
35             fetchingRequests--;
36
37             newArray = state.currentlyFetching.filter(item => {
38                 return item !== action.url;
39             });
40             return {
41                 currentlyFetching: newArray,
42                 fetchingRequests: fetchingRequests,
43                 isLoading: fetchingRequests !== 0
44             };
45         case actionTypes.SHOW:
46             return { isLoading: true };
47         case actionTypes.HIDE:
48             return { isLoading: false };
49         default:
50             return state;
51     }
52 };