opentripplanner / otp-ui

React component library, which can be used to build trip planner webapps.
http://www.opentripplanner.org/otp-ui/?path=/story/transitvehicleoverlay--real-time-rectangles
MIT License
56 stars 34 forks source link

Util to convert OTP Modes into human readable label #85

Closed PaulIvanov closed 3 years ago

PaulIvanov commented 4 years ago

Currently the OTP Settings Panel has the ability to convert modes from the config into buttons with human readable labels, It would be great to be able to utilize that logic as a utility function for other places in the application as well.

inputting MICROMOBILITY_RENT into the function will return Transit + E-Scooter inputting BICYCLE will return Bike Only when no transit options are chosen. Will return Transit + Bike when transit options are chosen.

If there are only transitModes in the selected List, return Transit

I attempted to build a somewhat working version of this but it doesnt handle all edges very well

const convertToObject = (newObj, mode) => {
  // eslint-disable-next-line no-param-reassign
  newObj[mode.mode] = mode;
  return newObj;
};

const getModeLabelFromConfig = (modes, modeString) => {
  const flatConfig = {
    ...modes.accessModes.reduce(convertToObject, {}),
    ...modes.exclusiveModes.reduce(convertToObject, {})
  };

  return modeString
    .split(',')
    .filter(
      m => !modes.transitModes.map(tm => tm.mode).includes(m) && m !== 'TRANSIT'
    )
    .map(mode => flatConfig[mode].label)
    .join(' + ');
};
PaulIvanov commented 4 years ago

@fpurcell

fpurcell commented 4 years ago

Hey Paul,

Talked to @binh-dam-ibigroup ... we're not sure an existing utility exists that addresses your need. The otp-rr code, as far as we know, is simply using the human readable mode strings from the config.

So not sure we've got a ready solution. That said, if you can point us toward the otp-rr code you're thinking about, please do.

Take care, Frank

grant-humphries commented 3 years ago

I've picked up the baton on the work in trimet.org that prompted this issue and was able to use the itinerary module in core-utils to put together something that isn't overly complicated works pretty well, so going to go a head and close this