Skip to content

Commit

Permalink
Move toRelativeOptions to date utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kr-matthews committed Jan 21, 2025
1 parent 55bccbd commit 5144f92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
14 changes: 3 additions & 11 deletions app/webpacker/components/CompetitionsOverview/ListViewSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '../../lib/utils/competition-table';
import { countries } from '../../lib/wca-data.js.erb';
import { adminCompetitionUrl, competitionUrl } from '../../lib/requests/routes.js.erb';
import { dateRange } from '../../lib/utils/dates';
import { dateRange, toRelativeOptions } from '../../lib/utils/dates';
import { DateTime } from 'luxon';

function ListViewSection({
Expand Down Expand Up @@ -454,7 +454,7 @@ function RegistrationStatus({ comp, isLoading }) {
content={
I18n.t(
'competitions.index.tooltips.registration.opens_in',
{ duration: DateTime.fromISO(comp.registration_open).toRelative({ locale: window.I18n.locale }) },
{ duration: DateTime.fromISO(comp.registration_open).toRelative(toRelativeOptions.default) },
)
}
position="top center"
Expand All @@ -464,21 +464,13 @@ function RegistrationStatus({ comp, isLoading }) {
}

if (comp.registration_status === 'past') {
const roundUpAndAtBestDayPrecision = {
locale: window.I18n.locale,
// don't be more precise than "days" (i.e. no hours/minutes/seconds)
unit: ["years", "months", "weeks", "days"],
// round up, e.g. in 8 hours -> pads to 1 day 8 hours -> rounds to "in 1 day"
padding: 24 * 60 * 60 * 1000,
};

return (
<Popup
trigger={<Icon name="user times" color="red" />}
content={
I18n.t(
'competitions.index.tooltips.registration.closed',
{ days: DateTime.fromISO(comp.start_date).toRelative(roundUpAndAtBestDayPrecision) },
{ days: DateTime.fromISO(comp.start_date).toRelative(toRelativeOptions.roundUpAndAtBestDayPrecision) },
)
}
position="top center"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DateTableCell, LocationTableCell, NameTableCell, ReportTableCell,
} from './TableCells';
import I18nHTMLTranslate from '../I18nHTMLTranslate';
import { toRelativeOptions } from '../../lib/utils/dates';

const competingStatusIcon = (competingStatus) => {
switch (competingStatus) {
Expand All @@ -23,19 +24,11 @@ const competingStatusIcon = (competingStatus) => {
};

const registrationStatusIconText = (competition) => {
const toRelativeOptions = {
locale: window.I18n.locale,
// don't be more precise than "days" (i.e. no hours/minutes/seconds)
unit: ["years", "months", "weeks", "days"],
// round up, e.g. in 8 hours -> pads to 1 day 8 hours -> rounds to "in 1 day"
padding: 24 * 60 * 60 * 1000,
};

if (competition.registration_status === 'not_yet_opened') {
return I18n.t('competitions.index.tooltips.registration.opens_in', { duration: DateTime.fromISO(competition.registration_open).toRelative({ locale: window.I18n.locale }) });
return I18n.t('competitions.index.tooltips.registration.opens_in', { duration: DateTime.fromISO(competition.registration_open).toRelative(toRelativeOptions.default) });
}
if (competition.registration_status === 'past') {
return I18n.t('competitions.index.tooltips.registration.closed', { days: DateTime.fromISO(competition.start_date).toRelative(toRelativeOptions) });
return I18n.t('competitions.index.tooltips.registration.closed', { days: DateTime.fromISO(competition.start_date).toRelative(toRelativeOptions.roundUpAndAtBestDayPrecision) });
}
if (competition.registration_status === 'full') {
return I18n.t('competitions.index.tooltips.registration.full');
Expand Down
16 changes: 15 additions & 1 deletion app/webpacker/lib/utils/dates.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { DateTime, Interval } from 'luxon';

// parameter name conventions:
// - `luxonDate` for luxon DateTime objects
// - `date` for date-only ISO strings (no time)
// - `dateTime` for date-and-time ISO strings

export const toRelativeOptions = {
default: {
locale: window.I18n.locale
},
roundUpAndAtBestDayPrecision: {
locale: window.I18n.locale,
// don't be more precise than "days" (i.e. no hours/minutes/seconds)
unit: ["years", "months", "weeks", "days"],
// round up, e.g. in 8 hours -> pads to 1 day 8 hours -> rounds to "in 1 day"
padding: 24 * 60 * 60 * 1000,
},
}

/// / luxon parameters

export const areOnSameDate = (
Expand Down Expand Up @@ -122,4 +136,4 @@ export const todayWithTime = (dateTime, timeZone) => {

export function dateRange(fromDate, toDate, options = {}) {
return Interval.fromDateTimes(DateTime.fromISO(fromDate), DateTime.fromISO(toDate)).toLocaleString({ month: 'short', day: '2-digit', year: 'numeric' }, options);
}
};

0 comments on commit 5144f92

Please sign in to comment.