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.

147 lines
3.0 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
let { start: start_date } = date
let date_array = []
if (start_date !== null && start_date.length > 0) {
date_array = start_date.split('+')
const tz = date_array[1].charAt(1)
start_date = [date_array[0], `0${tz}:00`].join('+')
}
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 }) {
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 () => {
if (events === null) {
let result = []
try {
const res = await fetch(endpoint, fetch_options)
result = await res.json()
} catch (error) {
console.error(error)
}
setEvents(result)
}
}, [events])
6 years ago
if (events === null) {
return <p>Laster arrangement...</p>
}
if (events.length <= 0) {
return <p>Det er ingen fremtidige arrangement.</p>
}
return events.map((event) => <Event event={event} />)
6 years ago
}