Skip to content

Commit

Permalink
getting data for 1 week works now. missing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nichhk committed Jul 2, 2022
1 parent 8064772 commit 4ad35da
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
26 changes: 19 additions & 7 deletions client/components/Map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { getDataRequestSuccess } from '@reducers/data';
import { updateMapPosition } from '@reducers/ui';
import { trackMapExport } from '@reducers/analytics';
import CookieNotice from '../main/CookieNotice';
// import { MAP_MODES } from '../common/CONSTANTS';
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;

Expand All @@ -35,7 +36,7 @@ class MapContainer extends React.Component {

// We store the raw requests from the API call here, but eventually they are
// converted and stored in the Redux store.
this.rawRequests = null;
this.rawRequests = [];
this.isSubscribed = null;
}

Expand All @@ -55,18 +56,27 @@ class MapContainer extends React.Component {
}

getAllRequests = async () => {
// TODO: add date specification. See https://dev-api.311-data.org/docs#/default/get_all_service_requests_requests_get.
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}`);
console.log(url);
const { data } = await axios.get(url);
this.rawRequests = data;
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) {
if (this.rawRequests.length === 0) {
await this.getAllRequests();
}

Expand Down Expand Up @@ -131,6 +141,8 @@ const mapStateToProps = state => ({
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
});

Expand Down
15 changes: 7 additions & 8 deletions client/components/Map/layers/RequestsLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class RequestsLayer extends React.Component {
this.setFilters(selectedTypes, requestStatus);
}
if (requests !== prev.requests && this.ready) {
console.log("got new requests");
console.log(requests);
this.setRequests(requests);
}
if (colorScheme !== prev.colorScheme) {
Expand All @@ -114,6 +112,7 @@ class RequestsLayer extends React.Component {
selectedTypes,
colorScheme,
requestTypes,
requestStatus,
} = this.props;

this.map.addLayer({
Expand All @@ -134,7 +133,7 @@ class RequestsLayer extends React.Component {
'circle-color': circleColors(requestTypes),
'circle-opacity': 0.8,
},
filter: typeFilter(selectedTypes),
filter: this.getFilterSpec(selectedTypes, requestStatus),
}, BEFORE_ID);

// this.map.addLayer({
Expand Down Expand Up @@ -168,9 +167,13 @@ class RequestsLayer extends React.Component {
}
};

getFilterSpec = (selectedTypes, requestStatus) => {
return ['all', typeFilter(selectedTypes), statusFilter(requestStatus)];
};

setFilters = (selectedTypes, requestStatus) => {
this.map.setFilter('request-circles',
['all', typeFilter(selectedTypes), statusFilter(requestStatus)]);
this.getFilterSpec(selectedTypes, requestStatus));
// Currently, we do not support heatmap. If we did, we'd want to update
// its filter here as well.
};
Expand All @@ -194,15 +197,11 @@ class RequestsLayer extends React.Component {

RequestsLayer.propTypes = {
activeLayer: PropTypes.oneOf(['points', 'heatmap']),
selectedTypes: PropTypes.shape({}),
requests: PropTypes.shape({}),
colorScheme: PropTypes.string,
};

RequestsLayer.defaultProps = {
activeLayer: 'points',
selectedTypes: {},
requests: {},
colorScheme: '',
};

Expand Down
8 changes: 5 additions & 3 deletions client/components/common/CONSTANTS.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,9 +829,11 @@ export const MAP_DATE_RANGES = (() => {
];
})();

export const DATE_SPEC = 'MM/DD/YYYY';

export const DATE_RANGES = (() => {
const endDate = moment().format('MM/DD/YYYY');
const priorDate = (num, timeInterval) => moment().subtract(num, timeInterval).format('MM/DD/YYYY');
const endDate = moment().format(DATE_SPEC);
const priorDate = (num, timeInterval) => moment().subtract(num, timeInterval).format(DATE_SPEC);

return [
{
Expand Down Expand Up @@ -873,7 +875,7 @@ export const DATE_RANGES = (() => {
{
id: 'YEAR_TO_DATE',
label: 'Year to Date',
startDate: moment().startOf('year').format('MM/DD/YYYY'),
startDate: moment().startOf('year').format(DATE_SPEC),
endDate,
},
{
Expand Down
2 changes: 2 additions & 0 deletions client/redux/reducers/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const initialState = {
pins: [],
pinsInfo: {},
selectedNcId: null,
// Empty GeoJSON object.
requests: { type: 'FeatureCollection', features: [] },
};

export default (state = initialState, action) => {
Expand Down
6 changes: 4 additions & 2 deletions client/redux/reducers/filters.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DATE_RANGES } from '@components/common/CONSTANTS';

export const types = {
UPDATE_START_DATE: 'UPDATE_START_DATE',
UPDATE_END_DATE: 'UPDATE_END_DATE',
Expand Down Expand Up @@ -35,8 +37,8 @@ export const updateRequestStatus = status => ({

const initialState = {
// dateRange: null,
startDate: null,
endDate: null,
startDate: DATE_RANGES[0].startDate,
endDate: DATE_RANGES[0].endDate,
councilId: null,
requestTypes: {
1: false,
Expand Down

0 comments on commit 4ad35da

Please sign in to comment.