diff --git a/src/components/CompetitionPage/UpcomingOrCurrentEventInfo.tsx b/src/components/CompetitionPage/UpcomingOrCurrentEventInfo.tsx index aea6d331..f56b6661 100644 --- a/src/components/CompetitionPage/UpcomingOrCurrentEventInfo.tsx +++ b/src/components/CompetitionPage/UpcomingOrCurrentEventInfo.tsx @@ -3,7 +3,7 @@ import {DateTime} from 'luxon' import {FC} from 'react' import {Event} from '@/types/api/competition' -import {formatDateTime, formatDateTimeInterval} from '@/utils/formatDate' +import {DateFormat, formatDateTimeInterval} from '@/utils/formatDate' import {Link} from '../Clickable/Link' @@ -12,22 +12,21 @@ export const UpcomingOrCurrentEventInfo: FC<{event: Event; name: string; shortNa name, shortName, }) => { - const {year, school_year, location, registration_link, publication_set} = event + const {year, school_year, location, registration_link, publication_set, start, end} = event - const upcomingEventDate = event ? formatDateTimeInterval(event.start, event.end) : null + const upcomingEventDate = event ? formatDateTimeInterval(start, end) : null + + const regStart = DateTime.fromISO(registration_link.start) + const regEnd = DateTime.fromISO(registration_link.end) + const now = DateTime.now() const registrationInfo = (() => { - if (DateTime.fromISO(registration_link.start) > DateTime.now()) - return `Registrácia bude otvorená od ${formatDateTime(registration_link.start)}` - else if (DateTime.fromISO(registration_link.end) > DateTime.now()) - return `Registrácia je otvorená do ${formatDateTime(registration_link.end)}` + if (now < regStart) return `Registrácia bude otvorená od ${regStart.toFormat(DateFormat.DATE_TIME)}` + if (now < regEnd) return `Registrácia je otvorená do ${regEnd.toFormat(DateFormat.DATE_TIME)}` return `Registrácia bola ukončená` })() - const isRegistrationActive = registration_link - ? DateTime.fromISO(registration_link.start) < DateTime.now() && - DateTime.fromISO(registration_link.end) > DateTime.now() - : false + const isRegistrationActive = regStart < now && regEnd > now return ( diff --git a/src/utils/formatDate.ts b/src/utils/formatDate.ts index 6165218f..fb502602 100644 --- a/src/utils/formatDate.ts +++ b/src/utils/formatDate.ts @@ -1,15 +1,26 @@ import {DateTime} from 'luxon' -export const formatDateTime = (date: string) => DateTime.fromISO(date).toFormat('dd. MM. yyyy HH:mm') -export const formatDate = (date: string) => DateTime.fromISO(date).toFormat('dd. LLL yyyy') -export const formatDateDigits = (date: string) => DateTime.fromISO(date).toFormat('dd. MM. yyyy') +export enum DateFormat { + TIME = 'HH:mm', + DATE = 'dd. LLL yyyy', + DATE_TIME = 'dd. MM. yyyy HH:mm', + DATE_DIGITS = 'dd. MM. yyyy', +} + +export const formatDateTime = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE_TIME) +export const formatDate = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE) +export const formatDateDigits = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE_DIGITS) export function formatDateTimeInterval(date1: string, date2: string) { - if (date1 === date2) return formatDateDigits(date1) const dateTime1 = DateTime.fromISO(date1) const dateTime2 = DateTime.fromISO(date2) - if (dateTime1 === dateTime2) return formatDate(date1) - if (dateTime1.toFormat('dd.MM.yyyy') === dateTime2.toFormat('dd.MM.yyyy')) - return `${dateTime1.toFormat('dd.MM.yyyy')} ${dateTime1.toFormat('HH:mm')} - ${dateTime2.toFormat('HH:mm')}` - return `${formatDateTime(date1)} - ${formatDateTime(date2)}` + + if (dateTime1.equals(dateTime2)) return formatDateDigits(date1) + + if (dateTime1.startOf('day').equals(dateTime2.startOf('day'))) + return `${dateTime1.toFormat(DateFormat.DATE_DIGITS)} ${dateTime1.toFormat(DateFormat.TIME)} - ${dateTime2.toFormat( + DateFormat.TIME, + )}` + + return `${dateTime1.toFormat(DateFormat.DATE_TIME)} - ${dateTime2.toFormat(DateFormat.DATE_TIME)}` }