blob: 3afdad0573e54484f21e5eae45235736503a8763 [file] [log] [blame]
ilanap1965d162018-01-04 11:34:59 +02001/*
2 * Copyright © 2016-2017 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
ilanap1965d162018-01-04 11:34:59 +02008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
ilanap1965d162018-01-04 11:34:59 +020012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import {actionTypes} from './LoaderConstants.js';
17
ilanap1965d162018-01-04 11:34:59 +020018export default (state = {fetchingRequests : 0, currentlyFetching : [], isLoading : false}, action) => {
19 let fetchingRequests = state.fetchingRequests;
20 let newArray;
Michael Landoefa037d2017-02-19 12:57:33 +020021 switch (action.type) {
ilanap1965d162018-01-04 11:34:59 +020022 case actionTypes.SEND_REQUEST:
23 fetchingRequests++;
24 newArray = state.currentlyFetching.slice();
25 newArray.splice(0, 0, action.url);
26 if (DEBUG) {
27 console.log('Loader SEND REQUEST url: ' + action.url);
28 console.log('Loader SEND REQUEST number of fetching requests: ' + fetchingRequests);
29 }
30 return {
31 fetchingRequests: fetchingRequests,
32 currentlyFetching : newArray,
33 isLoading: true
34 };
35 case actionTypes.RECEIVE_RESPONSE:
36 fetchingRequests--;
37
38 newArray = state.currentlyFetching.filter((item) => {return item !== action.url;});
39 if (DEBUG) {
40 console.log('Loader RECEIVE_RESPONSE url: ' + action.url);
41 console.log('Loader RECEIVE_RESPONSE: number of fetching requests: ' + fetchingRequests);
42 }
43 return {
44 currentlyFetching : newArray,
45 fetchingRequests: fetchingRequests,
46 isLoading: (fetchingRequests !== 0)
47 };
Michael Landoefa037d2017-02-19 12:57:33 +020048 case actionTypes.SHOW:
49 return {isLoading: true};
50 case actionTypes.HIDE:
51 return {isLoading: false};
52 default:
53 return state;
54 }
55};