|
| 1 | +import EventDate from '../EventDate/EventDate'; |
| 2 | +import { Link } from '@plone/components/quanta'; |
| 3 | +import * as RRuleLib from 'rrule'; |
| 4 | +import type * as RRuleTypes from 'rrule'; |
| 5 | +import type { EventCT, RootData } from '@plone/types'; |
| 6 | +import { getDate } from '../../helpers'; |
| 7 | +import Calendar from '@plone/components/icons/calendar.svg?react'; |
| 8 | +import { useTranslation } from 'react-i18next'; |
| 9 | +import styles from './EventDetails.module.css'; |
| 10 | + |
| 11 | +interface EventDetailsProps { |
| 12 | + data?: RootData<EventCT>; |
| 13 | +} |
| 14 | + |
| 15 | +interface RecurrenceProps { |
| 16 | + recurrence: string; |
| 17 | + start: string; |
| 18 | + locale: string; |
| 19 | +} |
| 20 | + |
| 21 | +export default function EventDetails({ data }: EventDetailsProps) { |
| 22 | + const { t } = useTranslation(); |
| 23 | + |
| 24 | + if (!data) return; |
| 25 | + |
| 26 | + const { content, locale } = data; |
| 27 | + |
| 28 | + return ( |
| 29 | + <aside className={styles['event-details']}> |
| 30 | + <dl> |
| 31 | + {content.subjects?.length > 0 && ( |
| 32 | + <> |
| 33 | + <dt>{t('layout.views.event.what')}</dt> |
| 34 | + <dd> |
| 35 | + <ul> |
| 36 | + {content.subjects.map((subject, index) => ( |
| 37 | + <li key={index}>{subject}</li> |
| 38 | + ))} |
| 39 | + </ul> |
| 40 | + </dd> |
| 41 | + </> |
| 42 | + )} |
| 43 | + <dt>{t('layout.views.event.when')}</dt> |
| 44 | + <EventDate content={content} locale={locale} /> |
| 45 | + {content.recurrence && ( |
| 46 | + <> |
| 47 | + <dt>{t('layout.views.event.allDates')}</dt> |
| 48 | + <Recurrence |
| 49 | + recurrence={content.recurrence} |
| 50 | + start={content.start} |
| 51 | + locale={locale} |
| 52 | + /> |
| 53 | + </> |
| 54 | + )} |
| 55 | + {content.location && ( |
| 56 | + <> |
| 57 | + <dt>{t('layout.views.event.where')}</dt> |
| 58 | + <dd>{content.location}</dd> |
| 59 | + </> |
| 60 | + )} |
| 61 | + {content.contact_name && ( |
| 62 | + <> |
| 63 | + <dt>{t('layout.views.event.contact.name')}</dt> |
| 64 | + <dd> |
| 65 | + {content.contact_email ? ( |
| 66 | + <Link href={`mailto:${content.contact_email}`}> |
| 67 | + {content.contact_name} |
| 68 | + </Link> |
| 69 | + ) : ( |
| 70 | + content.contact_name |
| 71 | + )} |
| 72 | + </dd> |
| 73 | + </> |
| 74 | + )} |
| 75 | + {content.contact_phone && ( |
| 76 | + <> |
| 77 | + <dt>{t('layout.views.event.contact.phone')}</dt> |
| 78 | + <dd>{content.contact_phone}</dd> |
| 79 | + </> |
| 80 | + )} |
| 81 | + {content.attendees?.length > 0 && ( |
| 82 | + <> |
| 83 | + <dt>{t('layout.views.event.attendees')}</dt> |
| 84 | + <dd> |
| 85 | + <ul> |
| 86 | + {content.attendees.map((attendee, index) => ( |
| 87 | + <li key={index}>{attendee}</li> |
| 88 | + ))} |
| 89 | + </ul> |
| 90 | + </dd> |
| 91 | + </> |
| 92 | + )} |
| 93 | + {content.event_url && ( |
| 94 | + <> |
| 95 | + <dt>{t('layout.views.event.website')}</dt> |
| 96 | + <dd> |
| 97 | + <Link |
| 98 | + href={content.event_url} |
| 99 | + target="_blank" |
| 100 | + rel="noopener noreferrer" |
| 101 | + > |
| 102 | + {t('layout.views.event.visitWebsite')} |
| 103 | + </Link> |
| 104 | + </dd> |
| 105 | + </> |
| 106 | + )} |
| 107 | + </dl> |
| 108 | + <span className="download-event"> |
| 109 | + <Calendar /> |
| 110 | + <Link |
| 111 | + href={`${content['@id']}/ics_view`} |
| 112 | + target="_blank" |
| 113 | + rel="noreferrer" |
| 114 | + > |
| 115 | + {t('layout.views.event.downloadEvent')} |
| 116 | + </Link> |
| 117 | + </span> |
| 118 | + </aside> |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +export const Recurrence = ({ recurrence, start, locale }: RecurrenceProps) => { |
| 123 | + const { RRule, rrulestr } = ((RRuleLib as any).default || |
| 124 | + RRuleLib) as typeof RRuleTypes; |
| 125 | + |
| 126 | + const newRecurrence = !recurrence.includes('DTSTART') |
| 127 | + ? RRule.optionsToString({ dtstart: new Date(start) }) + '\n' + recurrence |
| 128 | + : recurrence; |
| 129 | + |
| 130 | + const rule = rrulestr(newRecurrence, { unfold: true, forceset: true }); |
| 131 | + const ruleItems = rule.all().map((date) => getDate(date, locale)); |
| 132 | + |
| 133 | + return ( |
| 134 | + <dd> |
| 135 | + <ul> |
| 136 | + {ruleItems.map((date, index) => ( |
| 137 | + <li key={index}>{date}</li> |
| 138 | + ))} |
| 139 | + </ul> |
| 140 | + </dd> |
| 141 | + ); |
| 142 | +}; |
0 commit comments