-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider-variation-middleware.js
62 lines (51 loc) · 1.65 KB
/
provider-variation-middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
ACTIVATE_VARIATION,
variations,
findVariationWithProviders
} from './redux/variations'
import {
ENVIRONMENT_CHANGED,
environment
} from './redux/environment'
import {
setProviders,
unselectAllProviders,
displayLegalForFeaturedProvider,
hideLegalForFeaturedProvider,
FETCH_PROVIDERS
} from './redux/providers'
const ProviderVariationMiddleware = store => next => action => {
if(typeof action == 'object'){
const {
type,
payload
} = action
switch(type){
// apply variation providers to store
case ACTIVATE_VARIATION:
payload && dispatchSetProviders(store, payload.providers)
break;
// retrieve stringified provider data and cast to json before sending to store
case `${FETCH_PROVIDERS}_FULFILLED`:
const {data} = payload || {}
const {page} = data || {}
// if there is custom provider data in variation state,
// don't overwrite it with the results of the fetch ...
if(!findVariationWithProviders(store.getState().variations)) {
if(dispatchSetProviders(store, eval(page.providerWwwViewListJsonString || "[]"))) {}
else throw new Error('provider-data endpoint did not contain valid data in global.providerWwwViewListJsonString')
}
break;
}
}
return next(action)
}
// dispatchSetProvider
// convenience function for checking providers and dispatching if not empty
const dispatchSetProviders = (store, providers) => {
if(!providers || !providers.length){
return false
}
store.dispatch(setProviders({providers})); return true
}
export default ProviderVariationMiddleware