Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vehicles #51

Merged

Conversation

fpurcell
Copy link
Member

Storybook is here: https://opentransittools.github.io/otp-ui/?path=/story/realtime-vehiclelayer--real-time-vehicles-layer

NOTE: storybook has 'knobs' that allows mucking with the interface.

… managing the setInterval/clearInterval w/in Component mount / unmount lifecyele routines
retVal = true;
}
}
} catch (e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/catch shouldn't be needed at all.

} catch (e) {
console.log(e);
}
return retVal;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be rewritten into a one-liner:

return !!vehicleA && !!vehicleB && (vehicleA.vehicleId === vehicleB.vehicleId || vehicleA.tripId === vehicleB.tripId);

url = url.indexOf("?") ? `${url}?` : `${url}&`;
url = `${url}__time__=${d}`;

fetch(url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there already a polyfill for this fetch statement in the webpack build? Not sure this will work in all browsers.

*/
export function fetchVehicles(setData, trackedId, baseUrl, query) {
// build url
baseUrl = baseUrl || "https://maps.trimet.org/gtfs/rt/vehicles/";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to avoid TriMet-specific config and urls as much as possible. The component that calls this already has a default set, so there's probably no need to have a fallback value again here.

fetch(url)
.then(res => {
if (!res.ok) {
console.log(res.statusText);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all console.log statements.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be changed to console.error

}
return res;
})
.then(res => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This then statement could be combined with the previous one.

* fetch line geometry for a given pattern
*
* https://newplanner.trimet.org/ws/ti/v0/index/patterns/TRIMET:433758/geometry/geojson
* https://newplanner.trimet.org/ws/ti/v0/index/patterns/{agency}:{pattern}/geometry/geojson
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more context might be able to be added to note that these URLs are examples to look at.

tiUrl,
geojson = "/geojson"
) {
if (!tiUrl) tiUrl = "https://newplanner.trimet.org/ws/ti/v0/index";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove default TriMet config

}
})
.catch(error => {
console.log(`VEH fetch() error: ${error} for ${url}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be console.error

fetch(url)
.then(res => {
const retVal = res.json();
return retVal;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simply:

.then(res => res.json())

return retVal;
})
.then(json => {
if (geojson.indexOf("geojson") >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment describing what this if statement is checking?

setPatternData(patternId, json);
})
.catch(error => {
console.log(`VEH GEOMETRY fetch() error: ${error}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be console.error

return retVal;
}

export function formatTime(seconds) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could core-utils/src/time.js#formatDuration be modified to satisfy this use case?

);
}
} catch (e) {
console.log(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be console.error

color: null,

geometryUrl: "https://newplanner.trimet.org/ws/ti/v0/index",
vehicleUrl: "https://maps.trimet.org/gtfs/rt/vehicles/",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to not have TriMet-specific defaults.

*/
function VehicleLayer(props) {
const { vehicles } = props;
const { trackedVehicle } = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be:

const { trackedVehicle, vehicles } = props;

const { leaflet } = props;

const { closeZoom, midZoom } = props;
const { midSize, farSize } = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these props can be destructured all at once.

return icon;
}

const icon = makeIcon();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd probably be a good idea to cache these icons, but that's probably something for a later effort.

function VehiclePopup(props) {
const { vehicle } = props;
const { tracked } = props;
const { setTracked } = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can all be destructed in one statement.

vid = `Vehicle: ${vehicle.vehicleId}`;
}

const stopLink = `https://trimet.org/ride/stop.html?stop_id=${vehicle.stopId}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be a slot for a function to generate this link.


return (
<Popup>
<div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be encapsulated within a styled component.

function VehicleTracker(props) {
const { vehicle } = props;
const { tracked } = props;
const { setTracked } = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Props can be destructured all at once.

const { tracked } = props;
const { setTracked } = props;

function handleClick() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing this function and the next makes me think the VehicleTracker should be converted into a Component. Otherwise, these functions are defined all over again on every render.

const { trackedVehicle } = props;
const { pattern } = props;
const { lowlight, color } = props;
let { highlight } = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can destructure all these props at once.

highlight = setColor(color, highlight);
}

let retVal = <FeatureGroup />;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be moved to after the following if block and the FeatureGroup with the segments could be immediately returned.

if (trackedVehicle && pattern && pattern.data) {
const pt = utils.findPointOnLine(trackedVehicle, pattern.data);
const geom = utils.splitGeometry(pattern.data, pt, pattern.id);
const segments = utils.makeSplitLine(geom, highlight, lowlight);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calculations from the above lines probably can be cached somehow, but that might be something for a later effort.

Copy link
Contributor

@evansiroky evansiroky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving for now with the expectation that some of this may be revisited at a later time.

@fpurcell fpurcell merged commit 4987801 into opentripplanner:master Feb 3, 2020
@fpurcell fpurcell deleted the vehicles_refact_2 branch February 5, 2020 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants