Assistance with Travel Time Calculation for Calendar Events

Capture d’écran 2024-11-19 à 11.41.00.png

Hi 

I’m currently working on implementing a feature to calculate and display travel time between appointments in a calendar view similar to the attached image.

I’ve written some initial code, but it feels more like a workaround than a robust solution. Here’s what I have so far:

const onEventRendered = useCallback(
  (props) => {
    const el = createRoot(props.element);
    el.render(
      <Event
        interval={timeScale.interval}
        slotCount={timeScale.slotCount}
        {...props.data}
      />
    );
  },
  [timeScale.interval, timeScale.slotCount]
);


const Event = ({ TravelStart, StartTime, EndTime, Subject }) => {
  const eventDuration = moment
    .duration(moment(EndTime).diff(StartTime))
    .hours();


  // TODO: calculation width based on travel time and interval/slot count


  return (
    <Stack
      direction={"row"}
      justifyContent={"space-between"}
      sx={{
        width: "100%",
      }}
    >
      <Box sx={{ width: 300 }}> // TODO: calculation width
        <Typography
          fontSize={10}
          textAlign={"center"}
          color={"textSecondary"}
          sx={{
            borderBottom: (theme) => `1px solid ${theme.palette.divider}`,
            height: "auto",
            width: "100%",
            position: "relative",
            top: "20%",
          }}
        >
          {TravelStart}min
        </Typography>
      </Box>


      <Stack
        sx={{
          flexGrow: 1,
          bgcolor: "info.main",
        }}
      >
          {Subject} / {eventDuration} hours
      </Stack>
    </Stack>
  );
};

The code renders travel time as part of the event component, but I am struggling with dynamically calculating the width for the travel block based on the time scale and interval/slot count. This is essential to maintain accuracy in the UI, especially when there are overlapping events.

Could you suggest a better approach or best practices to handle this type of feature? Any advice on how to align travel time blocks with the time grid would be greatly appreciated!

Thanks in advance for your help.

Best regards,


3 Replies 1 reply marked as answer

VR Vijay Ravi Syncfusion Team November 20, 2024 06:37 AM UTC

Hi Jonathan,

According to the current Scheduler element structure, achieving your requirement for calculate and display travel time between appointments, is not possible. Therefore, we are unable to provide any workaround for this case.
 

Don't hesitate to get in touch if you require further help or more information.

Regards,

Vijay



JP Jonathan Payet November 20, 2024 01:15 PM UTC

Hi Vijay,

Thanks for your response. I have found a solution on my end by creating a new event type for travel time. Each time the event is modified in the backend, I recalculate the travel time and update the display accordingly. So, I have two events each time, with the travel time event being read-only. This approach works well with the current structure, allowing me to meet the requirement without needing additional changes to the Scheduler component.

Here’s the code I’m using to render the travel time event:


const onEventRendered = useCallback(({ data, element }) => { if (data.type === "TRAVEL_TIME") { element.style.backgroundColor = "transparent"; element.style.pointerEvents = "none"; const el = createRoot(element); return el.render(<EventTravelTime {...data} />); } return element; }, []);

Image_1863_1732108504716

Best regards,
Jonathan


Marked as answer

VR Vijay Ravi Syncfusion Team November 21, 2024 04:10 AM UTC

Hi Jonathan,

 

Thanks for your update. We are glad to know that your issue has been resolved. Please get back to us if you need any other assistance.

 

Regards,

Vijay


Loader.
Up arrow icon