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 | */ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 16 | import { actionTypes } from './LoaderConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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 | if (DEBUG) { |
| 30 | console.log('Loader SEND REQUEST url: ' + action.url); |
| 31 | console.log( |
| 32 | 'Loader SEND REQUEST number of fetching requests: ' + |
| 33 | fetchingRequests |
| 34 | ); |
| 35 | } |
| 36 | return { |
| 37 | fetchingRequests: fetchingRequests, |
| 38 | currentlyFetching: newArray, |
| 39 | isLoading: true |
| 40 | }; |
| 41 | case actionTypes.RECEIVE_RESPONSE: |
| 42 | fetchingRequests--; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 43 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 44 | newArray = state.currentlyFetching.filter(item => { |
| 45 | return item !== action.url; |
| 46 | }); |
| 47 | if (DEBUG) { |
| 48 | console.log('Loader RECEIVE_RESPONSE url: ' + action.url); |
| 49 | console.log( |
| 50 | 'Loader RECEIVE_RESPONSE: number of fetching requests: ' + |
| 51 | fetchingRequests |
| 52 | ); |
| 53 | } |
| 54 | return { |
| 55 | currentlyFetching: newArray, |
| 56 | fetchingRequests: fetchingRequests, |
| 57 | isLoading: fetchingRequests !== 0 |
| 58 | }; |
| 59 | case actionTypes.SHOW: |
| 60 | return { isLoading: true }; |
| 61 | case actionTypes.HIDE: |
| 62 | return { isLoading: false }; |
| 63 | default: |
| 64 | return state; |
| 65 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 66 | }; |