-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.js
165 lines (145 loc) · 4.63 KB
/
index.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
159
160
161
162
163
164
165
/* eslint-disable */
import React from 'react';
import PropTypes from 'proptypes';
import { connect } from 'react-redux';
import { withStyles } from '@material-ui/core/styles';
import axios from 'axios';
import { getDataRequestSuccess } from '@reducers/data';
import { updateMapPosition } from '@reducers/ui';
import { trackMapExport } from '@reducers/analytics';
import CookieNotice from '../main/CookieNotice';
import { DATE_SPEC } from '../common/CONSTANTS';
// import "mapbox-gl/dist/mapbox-gl.css";
import Map from './Map';
import moment from 'moment';
const REQUEST_BATCH_SIZE = 5000;
const styles = theme => ({
root: {
height: `calc(100vh - ${theme.header.height} - ${theme.footer.height})`,
},
})
class MapContainer extends React.Component {
constructor(props) {
super(props)
this.state = {
ncCounts: null,
ccCounts: null,
position: props.position,
lastUpdated: props.lastUpdated,
selectedTypes: this.getSelectedTypes(),
}
// We store the raw requests from the API call here, but eventually they are
// converted and stored in the Redux store.
this.rawRequests = [];
this.isSubscribed = null;
}
componentDidMount() {
this.isSubscribed = true;
this.setData();
}
componentDidUpdate(prevProps) {
if (prevProps.activeMode !== this.props.activeMode ||
prevProps.pins !== this.props.pins)
this.setData();
}
componentWillUnmount() {
this.isSubscribed = false;
}
/**
* Gets all requests over the time range specified in the Redux store.
*
* Since the server is slow to retrieve all the requests at once, we need to
* make multiple API calls, using `skip` and `limit` to retrieve consecutive
* chunks of data.
*/
getAllRequests = async () => {
const { startDate, endDate } = this.props;
const url = new URL(`${process.env.API_URL}/requests`);
url.searchParams.append("start_date", moment(startDate, DATE_SPEC).format('YYYY-MM-DD'));
url.searchParams.append("end_date", moment(endDate, DATE_SPEC).format('YYYY-MM-DD'));
url.searchParams.append("limit", `${REQUEST_BATCH_SIZE}`);
var returned_length = REQUEST_BATCH_SIZE;
var skip = 0;
while (returned_length === REQUEST_BATCH_SIZE) {
url.searchParams.append("skip", `${skip}`);
const { data } = await axios.get(url);
returned_length = data.length;
skip += returned_length;
this.rawRequests.push(...data);
url.searchParams.delete("skip");
}
};
setData = async () => {
const { pins } = this.props;
if (this.rawRequests.length === 0) {
await this.getAllRequests();
}
if (this.isSubscribed) {
const { getDataSuccess } = this.props;
getDataSuccess(this.convertRequests(this.rawRequests));
}
};
convertRequests = requests => ({
type: 'FeatureCollection',
features: requests.map(request => ({
type: 'Feature',
properties: {
requestId: request.requestId,
typeId: request.typeId,
closedDate: request.closedDate,
},
geometry: {
type: 'Point',
coordinates: [
request.longitude,
request.latitude,
]
}
}))
});
// TODO: fix this
getSelectedTypes = () => {
const { requestTypes } = this.props;
// return Object.keys(requestTypes).filter(type => {
// return type !== 'All' && requestTypes[type]
// });
return requestTypes;
};
render() {
const { position, lastUpdated, updatePosition, exportMap, classes, requests } = this.props;
const { ncCounts, ccCounts, selectedTypes } = this.state;
return (
<div className={classes.root}>
<Map
requests={requests}
ncCounts={ncCounts}
ccCounts={ccCounts}
position={position}
lastUpdated={lastUpdated}
updatePosition={updatePosition}
exportMap={exportMap}
selectedTypes={selectedTypes}
/>
<CookieNotice />
</div>
);
}
}
const mapStateToProps = state => ({
pins: state.data.pins,
position: state.ui.map,
lastUpdated: state.metadata.lastPulledLocal,
activeMode: state.ui.map.activeMode,
requestTypes: state.filters.requestTypes,
startDate: state.filters.startDate,
endDate: state.filters.endDate,
requests: state.data.requests
});
const mapDispatchToProps = dispatch => ({
updatePosition: position => dispatch(updateMapPosition(position)),
exportMap: () => dispatch(trackMapExport()),
getDataSuccess: data => dispatch(getDataRequestSuccess(data)),
});
MapContainer.propTypes = {};
MapContainer.defaultProps = {};
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(MapContainer));