-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathactions.js
125 lines (118 loc) · 4.02 KB
/
actions.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { createAction, createThunkAction } from 'redux-tools';
import { parseGadm36Id } from 'utils/format';
import axios from 'axios';
import uniqBy from 'lodash/uniqBy';
import {
getCountriesProvider,
getFAOCountriesProvider,
getRegionsProvider,
getSubRegionsProvider,
getCountryLinksProvider
} from 'services/country';
export const setCountriesLoading = createAction('setCountriesLoading');
export const setRegionsLoading = createAction('setRegionsLoading');
export const setSubRegionsLoading = createAction('setSubRegionsLoading');
export const setCountryLinksLoading = createAction('setCountryLinksLoading');
export const setCountries = createAction('setCountries');
export const setFAOCountries = createAction('setFAOCountries');
export const setGadmCountries = createAction('setGadmCountries');
export const setRegions = createAction('setRegions');
export const setSubRegions = createAction('setSubRegions');
export const setCountryLinks = createAction('setCountryLinks');
export const getCountries = createThunkAction(
'getCountries',
() => (dispatch, getState) => {
const { countryData } = getState();
if (countryData && !countryData.isCountriesLoading) {
dispatch(setCountriesLoading(true));
axios
.all([getCountriesProvider(), getFAOCountriesProvider()])
.then(
axios.spread((gadm36Countries, faoCountries) => {
dispatch(setGadmCountries(gadm36Countries.data.rows));
dispatch(setFAOCountries(faoCountries.data.rows));
dispatch(setCountries(gadm36Countries.data.rows));
dispatch(setCountriesLoading(false));
})
)
.catch(error => {
dispatch(setCountriesLoading(false));
console.info(error);
});
}
}
);
export const getRegions = createThunkAction(
'getRegions',
country => (dispatch, getState) => {
const { countryData } = getState();
if (countryData && !countryData.isRegionsLoading) {
dispatch(setRegionsLoading(true));
getRegionsProvider(country)
.then(response => {
const parsedResponse = [];
uniqBy(response.data.rows).forEach(row => {
parsedResponse.push({
id: parseGadm36Id(row.id).adm1,
name: row.name
});
});
dispatch(setRegions(parsedResponse, 'id'));
dispatch(setRegionsLoading(false));
})
.catch(error => {
dispatch(setRegionsLoading(false));
console.info(error);
});
}
}
);
export const getSubRegions = createThunkAction(
'getSubRegions',
({ adm0, adm1 }) => (dispatch, getState) => {
const { countryData } = getState();
if (countryData && !countryData.isSubRegionsLoading) {
dispatch(setSubRegionsLoading(true));
getSubRegionsProvider(adm0, adm1)
.then(subRegions => {
const { rows } = subRegions.data;
const parsedResponse = [];
uniqBy(rows).forEach(row => {
parsedResponse.push({
id: parseGadm36Id(row.id).adm2,
name: row.name
});
});
dispatch(setSubRegions(uniqBy(parsedResponse, 'id')));
dispatch(setSubRegionsLoading(false));
})
.catch(error => {
dispatch(setSubRegionsLoading(false));
console.info(error);
});
}
}
);
export const getCountryLinks = createThunkAction(
'getCountryLinks',
() => (dispatch, getState) => {
const { countryData } = getState();
if (countryData && !countryData.isCountryLinksLoading) {
dispatch(setCountryLinksLoading(true));
getCountryLinksProvider()
.then(response => {
const data = {};
if (response.data && response.data.rows.length) {
response.data.rows.forEach(d => {
data[d.iso] = JSON.parse(d.external_links);
});
}
dispatch(setCountryLinks(data));
})
.catch(error => {
dispatch(setCountryLinksLoading(false));
console.info(error);
});
}
}
);