-
Notifications
You must be signed in to change notification settings - Fork 90
/
selectors.js
158 lines (141 loc) · 4.19 KB
/
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { createSelector, createStructuredSelector } from 'reselect';
import isEmpty from 'lodash/isEmpty';
import bbox from 'turf-bbox';
import bboxPolygon from 'turf-bbox-polygon';
import area from 'turf-area';
import lineString from 'turf-linestring';
import {
getActiveDatasetsFromState,
getInteractions,
getInteractionSelected
} from 'components/map/selectors';
const getSearch = state => state.location && state.location.search;
const getLatLng = state =>
state.map &&
state.map.data &&
state.map.data.interactions &&
state.map.data.interactions.latlng;
const getMap = (state, { map }) => map;
export const getIsBoundary = createSelector(
getInteractionSelected,
interaction =>
interaction && interaction.layer && interaction.layer.isBoundary
);
export const getShouldZoomToShape = createSelector(
[getInteractionSelected, getMap],
(selected, map) => {
if (!selected || !map) return null;
if (map.getZoom() > 12) return false;
const { data, layer, geometry } = selected;
const { cartodb_id, wdpaid } = data || {};
const { analysisEndpoint, tableName } = layer || {};
const isAdmin = analysisEndpoint === 'admin';
const isWdpa = analysisEndpoint === 'wdpa' && (cartodb_id || wdpaid);
const isUse = cartodb_id && tableName;
if (isAdmin || isWdpa || isUse) return false;
// get bbox of geometry
const shapeBbox = bbox(geometry);
const shapePolygon = bboxPolygon(shapeBbox);
// get bbox of map
const mapBounds = map.getBounds();
const mapPolygon = bboxPolygon([
mapBounds._sw.lng,
mapBounds._sw.lat,
mapBounds._ne.lng,
mapBounds._ne.lat
]);
// compare size
const shapeArea = area(shapePolygon);
const mapArea = area(mapPolygon);
const ratio = shapeArea / mapArea;
return ratio < 0.25;
}
);
export const getCardData = createSelector(
[getInteractionSelected],
interaction => {
if (isEmpty(interaction) || !interaction.article) {
return null;
}
const { data, layer } = interaction;
const { interactionConfig } = layer || {};
const articleData =
interactionConfig &&
interactionConfig.output &&
interactionConfig.output.reduce((obj, param) => {
const { prefix, renderKey } = param;
const value = data[param.column || param.key];
const newObj = {
...obj,
...(renderKey &&
value && {
[renderKey]: `${prefix || ''}${value}`
})
};
return newObj;
}, {});
const { readMoreLink } = articleData || {};
const readMoreBtn = {
text: 'READ MORE',
extLink: readMoreLink,
theme: `theme-button-small ${data.bbox ? 'theme-button-light' : ''}`
};
const buttons = data.bbox
? [readMoreBtn].concat([
{
text: 'ZOOM',
theme: 'theme-button-small'
}
])
: [readMoreBtn];
let newBbox = data.bbox && JSON.parse(data.bbox).coordinates[0];
if (newBbox) {
const bboxCoords = newBbox.slice(0, 4);
newBbox = bbox(lineString(bboxCoords));
}
return {
...articleData,
...(bbox && {
bbox: newBbox
}),
buttons
};
}
);
export const getTableData = createSelector(
[getInteractionSelected, getIsBoundary],
(interaction, isBoundary) => {
if (isEmpty(interaction) || interaction.article) return null;
const { data, layer } = interaction;
const { interactionConfig } = layer || {};
if (isBoundary && interactionConfig) {
return interactionConfig.output.reduce(
(obj, c) => ({
...obj,
[c.column]: data[c.column]
}),
{}
);
}
return (
interactionConfig &&
interactionConfig.output &&
interactionConfig.output.filter(c => !c.hidden).map(c => ({
...c,
label: c.property,
value: data[c.column]
}))
);
}
);
export const getPopupProps = createStructuredSelector({
interactions: getInteractions,
selected: getInteractionSelected,
tableData: getTableData,
cardData: getCardData,
latlng: getLatLng,
activeDatasets: getActiveDatasetsFromState,
search: getSearch,
isBoundary: getIsBoundary,
zoomToShape: getShouldZoomToShape
});