-
Notifications
You must be signed in to change notification settings - Fork 90
/
explore-selectors.js
126 lines (115 loc) · 3.1 KB
/
explore-selectors.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
126
import { createSelector, createStructuredSelector } from 'reselect';
import bbox from 'turf-bbox';
import lineString from 'turf-linestring';
import {
getActiveDatasetsFromState,
getBasemap,
getLabel
} from 'components/maps/map/selectors';
import { descriptions, topics, stories } from './explore-sections';
const selectSection = (state, { exploreType }) => exploreType;
const selectPTWLoading = state => state.ptw && state.ptw.loading;
const selectPTWData = state => {
const { data } = state.ptw || {};
return (
data &&
data.map(d => {
const bboxCoords = d.bbox.slice(0, 4);
const bboxFromCoords = bbox(lineString(bboxCoords));
const reverseBbox = [
bboxFromCoords[1],
bboxFromCoords[0],
bboxFromCoords[3],
bboxFromCoords[2]
];
return {
id: d.cartodb_id,
image: d.image,
imageCredit: d.image_source,
title: d.name,
summary: d.description,
buttons: [
{
text: 'READ MORE',
extLink: d.link,
theme: 'theme-button-light theme-button-small'
},
{
text: 'VIEW ON MAP',
theme: 'theme-button-small'
}
],
payload: {
mergeQuery: true,
map: {
datasets: [
// admin boundaries
{
dataset: 'fdc8dc1b-2728-4a79-b23f-b09485052b8d',
layers: [
'6f6798e6-39ec-4163-979e-182a74ca65ee',
'c5d1e010-383a-4713-9aaa-44f728c0571c'
],
opacity: 1,
visibility: true
},
// GLADs
{
dataset: 'e663eb09-04de-4f39-b871-35c6c2ed10b5',
layers: [
'581ecc62-9a70-4ef4-8384-0d59363e511d',
'dd5df87f-39c2-4aeb-a462-3ef969b20b66'
],
opacity: 1,
visibility: true,
bbox: reverseBbox
}
],
basemap: {
value: 'default'
},
label: 'default'
}
}
};
})
);
};
const selectedData = createSelector([selectPTWData], ptw => ({
topics: Object.values(topics),
placesToWatch: ptw,
stories: Object.values(stories)
}));
const getCardsData = createSelector(
[selectSection, selectedData],
(section, data) => {
if (!data) return null;
return data[section];
}
);
const getLoading = createSelector(
[selectSection, selectPTWLoading],
(section, ptwLoading) => {
if (section === 'topics') return false;
return ptwLoading;
}
);
const getDescription = createSelector(
[selectSection],
section => descriptions[section]
);
const getCurrentMapPayload = createSelector(
[getActiveDatasetsFromState, getBasemap, getLabel],
(datasets, basemap, label) => ({
datasets,
basemap,
label
})
);
export const mapStateToProps = createStructuredSelector({
data: getCardsData,
section: selectSection,
description: getDescription,
mapState: getCurrentMapPayload,
loading: getLoading
});