ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2016-2017 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame^] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import {actionTypes} from './LoaderConstants.js'; |
| 17 | |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame^] | 18 | export default (state = {fetchingRequests : 0, currentlyFetching : [], isLoading : false}, action) => { |
| 19 | let fetchingRequests = state.fetchingRequests; |
| 20 | let newArray; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | switch (action.type) { |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame^] | 22 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 48 | case actionTypes.SHOW: |
| 49 | return {isLoading: true}; |
| 50 | case actionTypes.HIDE: |
| 51 | return {isLoading: false}; |
| 52 | default: |
| 53 | return state; |
| 54 | } |
| 55 | }; |