You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
2.8 KiB

6 years ago
import { useEffect, useState } from 'preact/hooks'
const endpoint = 'https://kultar.sout.no/events/'
6 years ago
const fetch_options = {}
6 years ago
function Link(props) {
return <a {...props} target="_blank" rel="noopener noreferrer" />
6 years ago
}
6 years ago
function Facebook({ event_id }) {
const facebook_event_url = `https://facebook.com/events/${event_id}/`
return (
6 years ago
<Link href={facebook_event_url} title="Åpne facebookarrangement.">
Mer info
</Link>
6 years ago
)
}
function Tickets({ ticket_url }) {
return (
6 years ago
<Link href={ticket_url} title="Kjøp billett hos kultar.">
6 years ago
Kjøp billett
6 years ago
</Link>
6 years ago
)
6 years ago
}
function Time({ date }) {
const hasTime = date.getHours() === 0
const time_option = hasTime ? undefined : 'numeric'
const options = {
month: 'long',
weekday: 'long',
day: 'numeric',
hour: time_option,
minute: time_option,
}
return <h4>{new Intl.DateTimeFormat('no', options).format(date)}</h4>
}
function Location({ location }) {
const { location: area, host } = location
const hasHost = host.length !== 0
const hasArea = area.length !== 0
if (hasHost && hasArea) {
return (
<p>
{host}, {area}.
</p>
)
}
if (hasHost) {
return <p>{host}.</p>
}
if (hasArea) {
return <p>{area}.</p>
}
return ''
}
6 years ago
6 years ago
function Links({ event_id, ticket_url }) {
if (ticket_url !== null) {
return (
<>
<Tickets ticket_url={ticket_url} />
&nbsp;&nbsp;
<Facebook event_id={event_id} />
</>
)
}
return <Facebook event_id={event_id} />
}
6 years ago
function EventImage({ images }) {
const { square = null } = images
return (
<img
className="event-image"
src={`https://kultar.sout.no/events/${square}`}
/>
)
}
6 years ago
function EventCard({ event }) {
6 years ago
const { name, location, date, ticket_url, event_id } = event
const { start: start_date } = date
6 years ago
return (
<span className="event-card">
6 years ago
<h3>{name}</h3>
<Time date={new Date(start_date)} />
6 years ago
<Links ticket_url={ticket_url} event_id={event_id} />
6 years ago
<Location location={location} />
</span>
6 years ago
)
}
function Event({ event, index }) {
const isFirstEvent = index === 0
const { images } = event
6 years ago
return (
<div className="event">
<EventImage images={images} />
6 years ago
<EventCard event={event} />
</div>
6 years ago
)
}
export default function Events() {
const [events, setEvents] = useState(null)
useEffect(async () => {
const res = await fetch(endpoint, fetch_options)
const result = await res.json()
setEvents(result)
}, [])
if (events === null) {
return <p>Laster arrangement...</p>
}
if (events.length <= 0) {
return <p>Det er ingen fremtidige arrangement.</p>
}
return (
<div>
6 years ago
{events.map((event, index) => (
<>
<Event event={event} index={index} />
</>
6 years ago
))}
</div>
6 years ago
)
}