-
Notifications
You must be signed in to change notification settings - Fork 90
/
component.jsx
175 lines (162 loc) · 4.97 KB
/
component.jsx
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import isEmpty from 'lodash/isEmpty';
import isEqual from 'lodash/isEqual';
import bbox from 'turf-bbox';
import { Popup as MapPopup } from 'react-map-gl';
import Button from 'components/ui/button/button-component';
import Dropdown from 'components/ui/dropdown/dropdown-component';
import Card from 'components/ui/card';
import DataTable from './components/data-table';
import BoundarySentence from './components/boundary-sentence';
class Popup extends Component {
componentDidUpdate(prevProps) {
const { interactions, activeDatasets } = this.props;
if (
isEmpty(interactions) &&
!isEqual(activeDatasets.length, prevProps.activeDatasets.length)
) {
this.handleClose();
}
}
handleClickZoom = selected => {
const { setMapSettings } = this.props;
const newBbox = bbox(selected.geometry);
setMapSettings({ canBound: true, bbox: newBbox });
this.setState({ open: false });
this.handleClose();
};
handleClickAction = (selected, handleAction) => {
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;
handleAction({
data,
layer,
geometry,
isUse,
isAdmin,
isWdpa
});
this.handleClose();
};
// when clicking popup action the map triggers the interaction event
// causing the popup to open again. this stops it for now.
handleClose = () => {
setTimeout(() => this.props.clearMapInteractions(), 300);
};
render() {
const {
tableData,
cardData,
latlng,
interactions,
selected,
setMapInteractionSelected,
clearMapInteractions,
onSelectBoundary,
setMapSettings,
isBoundary,
zoomToShape,
buttons
} = this.props;
return latlng && latlng.lat && selected && !selected.data.cluster ? (
<MapPopup
latitude={latlng.lat}
longitude={latlng.lng}
onClose={clearMapInteractions}
closeOnClick={false}
>
<div className="c-popup">
{cardData ? (
<Card
className="popup-card"
theme="theme-card-small"
data={{
...cardData,
buttons: cardData.buttons.map(
b =>
(b.text === 'ZOOM'
? {
...b,
onClick: () =>
setMapSettings({
canBound: true,
bbox: cardData.bbox
})
}
: b)
)
}}
/>
) : (
<div className="popup-table">
{interactions &&
interactions.length > 1 && (
<Dropdown
className="layer-selector"
theme="theme-dropdown-native"
value={selected}
options={interactions}
onChange={setMapInteractionSelected}
native
/>
)}
{selected &&
interactions.length === 1 && (
<div className="popup-title">{selected.label}</div>
)}
{isBoundary ? (
<BoundarySentence
selected={selected}
data={tableData}
onSelectBoundary={onSelectBoundary}
/>
) : (
<DataTable data={tableData} />
)}
<div className="popup-footer">
{zoomToShape ? (
<Button onClick={() => this.handleClickZoom(selected)}>
Zoom
</Button>
) : (
buttons &&
buttons.map(p => (
<Button
key={p.label}
onClick={() => {
this.handleClickAction(selected, p.action);
}}
>
{p.label}
</Button>
))
)}
</div>
</div>
)}
</div>
</MapPopup>
) : null;
}
}
Popup.propTypes = {
clearMapInteractions: PropTypes.func,
setMapInteractionSelected: PropTypes.func,
latlng: PropTypes.object,
selected: PropTypes.object,
interactions: PropTypes.array,
tableData: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
isBoundary: PropTypes.bool,
cardData: PropTypes.object,
activeDatasets: PropTypes.array,
onSelectBoundary: PropTypes.func,
setMapSettings: PropTypes.func,
zoomToShape: PropTypes.bool,
buttons: PropTypes.array
};
export default Popup;