2022-01-09 12:11:24 +01:00
|
|
|
import Formatter from './formatter.js'
|
|
|
|
import { createAnchorElement } from './html-creator.js'
|
2021-05-05 10:02:57 +02:00
|
|
|
|
|
|
|
export function displayEvents({ data, document, list }) {
|
|
|
|
const locale = list.getAttribute('data-locale')
|
|
|
|
const maxEventsCount = list.getAttribute('data-maximum')
|
2021-05-05 10:40:46 +02:00
|
|
|
const timeZone = list.getAttribute('data-time-zone')
|
2021-12-09 19:46:19 +01:00
|
|
|
const isShortOffsetNameShown = list.hasAttribute(
|
|
|
|
'data-is-short-offset-name-shown'
|
|
|
|
)
|
|
|
|
const events = data.events
|
|
|
|
? data.events.elements
|
|
|
|
: data.group.organizedEvents.elements
|
2021-05-05 10:02:57 +02:00
|
|
|
const eventsCount = Math.min(maxEventsCount, events.length)
|
|
|
|
for (let i = 0; i < eventsCount; i++) {
|
|
|
|
const li = document.createElement('li')
|
|
|
|
|
2021-12-09 19:46:19 +01:00
|
|
|
const a = createAnchorElement({
|
|
|
|
document,
|
|
|
|
text: events[i].title,
|
|
|
|
url: events[i].url,
|
|
|
|
})
|
2021-05-05 10:02:57 +02:00
|
|
|
li.appendChild(a)
|
|
|
|
|
|
|
|
const br = document.createElement('br')
|
|
|
|
li.appendChild(br)
|
|
|
|
|
2021-05-23 15:37:29 +02:00
|
|
|
const date = Formatter.formatDate({
|
|
|
|
locale,
|
|
|
|
start: events[i].beginsOn,
|
|
|
|
end: events[i].endsOn,
|
|
|
|
timeZone,
|
|
|
|
isShortOffsetNameShown,
|
|
|
|
})
|
2021-05-05 10:02:57 +02:00
|
|
|
const textnode = document.createTextNode(date)
|
|
|
|
li.appendChild(textnode)
|
|
|
|
|
|
|
|
if (events[i].physicalAddress) {
|
|
|
|
const location = Formatter.formatLocation({
|
|
|
|
description: events[i].physicalAddress.description,
|
2021-12-09 19:46:19 +01:00
|
|
|
locality: events[i].physicalAddress.locality,
|
2021-05-05 10:02:57 +02:00
|
|
|
})
|
|
|
|
if (location) {
|
|
|
|
const brBeforeLocation = document.createElement('br')
|
|
|
|
li.appendChild(brBeforeLocation)
|
|
|
|
|
|
|
|
const textnodeLocation = document.createTextNode(location)
|
|
|
|
li.appendChild(textnodeLocation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list.appendChild(li)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function displayErrorMessage({ data, list }) {
|
|
|
|
console.error(data)
|
2021-12-09 19:46:19 +01:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(data, 'response') &&
|
2021-12-09 19:02:49 +01:00
|
|
|
Object.prototype.hasOwnProperty.call(data.response, 'errors') &&
|
|
|
|
data.response.errors.length > 0 &&
|
|
|
|
Object.prototype.hasOwnProperty.call(data.response.errors[0], 'code') &&
|
2021-12-09 19:46:19 +01:00
|
|
|
data.response.errors[0].code === 'group_not_found'
|
|
|
|
) {
|
2021-12-09 19:02:49 +01:00
|
|
|
list.children[1].style.display = 'block'
|
|
|
|
} else {
|
|
|
|
list.children[0].style.display = 'block'
|
|
|
|
}
|
2021-05-05 10:02:57 +02:00
|
|
|
}
|