blob: 4222c3dac6e065db072db75144407b43968db4bb [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 */
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020016import { actionTypes } from './LoaderConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020017
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018export 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);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029 return {
30 fetchingRequests: fetchingRequests,
31 currentlyFetching: newArray,
32 isLoading: true
33 };
34 case actionTypes.RECEIVE_RESPONSE:
andre.schmid4594cba2022-01-25 19:38:32 +000035 if (fetchingRequests > 0) {
36 fetchingRequests--;
37 }
ilanap1965d162018-01-04 11:34:59 +020038
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020039 newArray = state.currentlyFetching.filter(item => {
40 return item !== action.url;
41 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020042 return {
43 currentlyFetching: newArray,
44 fetchingRequests: fetchingRequests,
45 isLoading: fetchingRequests !== 0
46 };
47 case actionTypes.SHOW:
48 return { isLoading: true };
49 case actionTypes.HIDE:
50 return { isLoading: false };
51 default:
52 return state;
53 }
Michael Landoefa037d2017-02-19 12:57:33 +020054};