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

Custom schedule calendar event rendering #10294

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions app/webpacker/components/Schedule/CalendarView.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import luxonPlugin from '@fullcalendar/luxon3';
import luxonPlugin, { toLuxonDateTime } from '@fullcalendar/luxon3';
import FullCalendar from '@fullcalendar/react';
import timeGridPlugin from '@fullcalendar/timegrid';
import { DateTime } from 'luxon';
import { DateTime, Interval } from 'luxon';
import React from 'react';
import {
earliestTimeOfDayWithBuffer,
Expand Down Expand Up @@ -92,10 +92,62 @@ export default function CalendarView({
locale={calendarLocale}
timeZone={timeZone}
events={fcActivities}
// custom rendering of event content
eventContent={(args) => (
<CalendarEventView {...args} />
)}
/>
{fcActivities.length === 0 && (
<em>{I18n.t('competitions.schedule.no_activities')}</em>
)}
</>
);
}

function CalendarEventView({ event, timeText, view }) {
const startLuxon = toLuxonDateTime(event.start, view.calendar);
const endLuxon = toLuxonDateTime(event.end, view.calendar);
const interval = Interval.fromDateTimes(startLuxon, endLuxon);
const lengthInMin = interval.length('minutes');

const style = getEventStyle(lengthInMin);

return (
<div className='fc-event-main-frame' style={style}>
<InnerEventContent
onlyOneLine={lengthInMin < 25}
title={event.title}
timeText={timeText}
/>
</div>
);
}

function InnerEventContent({ onlyOneLine, timeText, title }) {
if (onlyOneLine) {
return (
<>{timeText} - {title}</>
);
} else {
return (
<>
<div style={{ whiteSpace: 'nowrap' }}>{timeText}</div>
<div>{title}</div>
</>
);
}
}

function getEventStyle(lengthInMin) {
if (lengthInMin < 15) {
return { overflow: 'hidden', whiteSpace: 'nowrap', lineHeight: '1.2em', fontSize: '80%' };
} else if (lengthInMin < 20) {
return { overflow: 'hidden', whiteSpace: 'nowrap', lineHeight: '1.5em' };
} else if (lengthInMin < 25) {
return { overflow: 'hidden', whiteSpace: 'nowrap' };
} else if (lengthInMin < 30) {
return { overflow: 'hidden', lineHeight: '1.4em' };
} else {
return { overflow: 'hidden' };
}
}