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

Competition page content #389

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
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
74 changes: 23 additions & 51 deletions src/components/CompetitionPage/CompetitionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {FC, useEffect} from 'react'
import {Link} from '@/components/Clickable/Link'
import {Competition, Event} from '@/types/api/competition'
import {BannerContainer} from '@/utils/BannerContainer'
import {formatDateTime} from '@/utils/formatDate'

import {UpcomingOrCurrentEventInfo} from './UpcomingOrCurrentEventInfo'

type OurCompetition = Omit<Competition, 'history_events'> & {history_events: Event[]}

Expand All @@ -16,13 +17,19 @@ type CompetitionPageProps = {
}

export const CompetitionPage: FC<CompetitionPageProps> = ({
competition: {id, name, who_can_participate, description, upcoming_or_current_event, history_events, rules},
competition: {
id,
competition_type,
name,
who_can_participate,
description,
upcoming_or_current_event,
history_events,
rules,
},
}) => {
const {setBannerMessages} = BannerContainer.useContainer()

const startDate = upcoming_or_current_event ? formatDateTime(upcoming_or_current_event.start) : null
const endDate = upcoming_or_current_event ? formatDateTime(upcoming_or_current_event.end) : null

const {data: bannerMessage, isLoading: isBannerLoading} = useQuery({
queryKey: ['cms', 'info-banner', 'competition', id],
queryFn: () => axios.get<string[]>(`/api/cms/info-banner/competition/${id}`),
Expand All @@ -39,10 +46,10 @@ export const CompetitionPage: FC<CompetitionPageProps> = ({
const rulesLink = `${router.asPath}/pravidla`

return (
<Stack gap={3}>
<Stack gap={5}>
<Typography variant="body1">
{who_can_participate && `Súťaž je určená pre ${who_can_participate}.`}
{description && ` ${description}`}
{description}
{who_can_participate && ` Súťaž je určená pre ${who_can_participate}.`}
</Typography>

{rules && (
Expand All @@ -59,63 +66,28 @@ export const CompetitionPage: FC<CompetitionPageProps> = ({
)}

<Stack gap={2}>
<Typography variant="h2">Nadchádzajúci ročník</Typography>
{upcoming_or_current_event ? (
<Stack gap={1}>
{startDate && (
<Typography variant="body1">
<b>Od:</b> {startDate}
</Typography>
)}
{endDate && (
<Typography variant="body1">
<b>Do:</b> {endDate}
</Typography>
)}
{upcoming_or_current_event.publication_set.length > 0 && (
<Stack sx={{alignItems: 'end'}}>
<Link variant="button2" href={`/api/${upcoming_or_current_event.publication_set[0].file}`}>
Pozvánka
</Link>
</Stack>
)}
{upcoming_or_current_event.registration_link && (
<>
<Typography variant="body1">{upcoming_or_current_event.registration_link.additional_info}</Typography>
<Typography variant="body1">
<b>Registrácia prebieha do:</b> {formatDateTime(upcoming_or_current_event.registration_link.end)}
</Typography>
<Stack sx={{alignItems: 'end'}}>
<Link variant="button2" href={upcoming_or_current_event.registration_link.url}>
Registračný formulár
</Link>
</Stack>
</>
)}
</Stack>
<UpcomingOrCurrentEventInfo
event={upcoming_or_current_event}
name={name}
shortName={competition_type?.short_name}
/>
) : (
<Typography variant="body1" sx={{marginTop: 1}}>
Pripravujeme
Ďalší ročník aktuálne pripravujeme
</Typography>
)}
</Stack>
<Stack>
<Typography variant="h2">Archív</Typography>
{/* TODO: asi zjednotit styly, neriesit with/without publications */}

<Stack gap={1}>
{history_events.map((event) => (
<Stack
key={event.id}
direction="row"
sx={{
justifyContent: 'space-between',
}}
>
<Stack key={event.id} direction="row" sx={{justifyContent: 'space-between'}}>
<Typography variant="h3" component="span">
{name} {event.school_year}
</Typography>
<Stack direction="row" gap={2}>
<Stack direction="row" sx={{gap: {xs: 1, sm: 2}}}>
{event.publication_set.map((publication) => (
<Link variant="button2" key={publication.id} href={`/api/${publication.file}`}>
{publication.name}
Expand Down
57 changes: 57 additions & 0 deletions src/components/CompetitionPage/UpcomingOrCurrentEventInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {Stack, Typography} from '@mui/material'
import {DateTime} from 'luxon'
import {FC} from 'react'

import {Event} from '@/types/api/competition'
import {DateFormat, formatDateTimeInterval} from '@/utils/formatDate'

import {Link} from '../Clickable/Link'

export const UpcomingOrCurrentEventInfo: FC<{event: Event; name: string; shortName: string | undefined}> = ({
event,
name,
shortName,
}) => {
const {year, school_year, location, registration_link, publication_set, start, end} = event

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 (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 = regStart < now && regEnd > now

return (
<Stack gap={1}>
<Typography variant="body1" fontWeight={800}>
{shortName === 'súťaž' &&
`${year}. ročník súťaže ${name} sa bude konať ${upcomingEventDate} ${location || ''}. `}
{shortName === 'tábor' &&
`${name} v roku ${school_year?.split('/')[1]} sa bude konať ${upcomingEventDate} ${location || ''}.`}
{shortName === 'seminár' && `Aktuálne prebieha ${year}. ročník seminára ${name}`}
</Typography>
<Typography variant="body1" fontWeight={800}>
{registrationInfo}
</Typography>
<Stack direction="row" sx={{justifyContent: 'end', gap: {xs: 1, sm: 2}}}>
{publication_set.length > 0 && (
<Link variant="button2" href={`/api/${publication_set[0].file}`}>
Pozvánka
</Link>
)}
{isRegistrationActive && (
<Link variant="button2" href={registration_link.url}>
Registrácia
</Link>
)}
</Stack>
</Stack>
)
}
2 changes: 1 addition & 1 deletion src/components/Problems/Problem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const Problem: FC<{
mt: 0.5,
justifyContent: 'end',
flexWrap: 'wrap',
columnGap: {xs: 1, sm: 2, md: 2},
columnGap: {xs: 1, sm: 2},
rowGap: 1,
}}
>
Expand Down
2 changes: 2 additions & 0 deletions src/types/api/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface Event {
is_registered: boolean
publication_set: Publication[]
registration_link: RegistrationLink
location: string | null
year: number | null
school_year: string | null
season_code: number
Expand All @@ -43,6 +44,7 @@ export interface Event {
export interface CompetitionType {
id: number
name: string
short_name: string
}

export interface Competition {
Expand Down
26 changes: 24 additions & 2 deletions src/utils/formatDate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +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 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) {
const dateTime1 = DateTime.fromISO(date1)
const dateTime2 = DateTime.fromISO(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)}`
}
Loading