2022-01-09 12:11:24 +01:00
|
|
|
import Formatter from './formatter.js'
|
2024-08-04 18:26:30 +02:00
|
|
|
import { createAnchorElement, createImageElement } from './html-creator.js'
|
2021-05-05 10:02:57 +02:00
|
|
|
|
2022-06-03 23:48:19 +02:00
|
|
|
export function clearEventsList(container) {
|
|
|
|
const list = container.querySelector('ul')
|
|
|
|
list.replaceChildren()
|
|
|
|
}
|
|
|
|
|
2024-04-05 23:26:11 +02:00
|
|
|
export function displayEvents({ events, document, container, maxEventsCount }) {
|
2022-06-03 23:48:19 +02:00
|
|
|
hideLoadingIndicator(container)
|
|
|
|
|
|
|
|
const isShortOffsetNameShown =
|
|
|
|
window.MOBILIZON_CONNECTOR.isShortOffsetNameShown
|
|
|
|
const locale = window.MOBILIZON_CONNECTOR.locale
|
|
|
|
const timeZone = window.MOBILIZON_CONNECTOR.timeZone
|
|
|
|
|
2021-05-05 10:02:57 +02:00
|
|
|
const eventsCount = Math.min(maxEventsCount, events.length)
|
2022-06-03 23:48:19 +02:00
|
|
|
const list = container.querySelector('ul')
|
2021-05-05 10:02:57 +02:00
|
|
|
for (let i = 0; i < eventsCount; i++) {
|
|
|
|
const li = document.createElement('li')
|
2024-08-04 18:26:30 +02:00
|
|
|
li.style.lineHeight = '150%'
|
|
|
|
li.style.marginTop = '20px'
|
2024-05-14 10:22:40 +02:00
|
|
|
|
2024-08-04 18:26:30 +02:00
|
|
|
if (events[i].picture) {
|
|
|
|
const img = createImageElement({
|
|
|
|
document,
|
|
|
|
alt: events[i].picture.alt ? events[i].picture.alt : '',
|
|
|
|
src: events[i].picture.base64 ? events[i].picture.base64 : '',
|
|
|
|
})
|
|
|
|
img.style.display = 'block'
|
|
|
|
img.style.maxWidth = '100%'
|
|
|
|
li.appendChild(img)
|
|
|
|
}
|
2021-05-05 10:02:57 +02:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 23:48:19 +02:00
|
|
|
export function displayErrorMessage({ data, container }) {
|
|
|
|
hideLoadingIndicator(container)
|
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'
|
|
|
|
) {
|
2022-06-03 23:48:19 +02:00
|
|
|
const message = container.querySelector('.group-not-found')
|
|
|
|
message.style.display = 'block'
|
2021-12-09 19:02:49 +01:00
|
|
|
} else {
|
2022-06-03 23:48:19 +02:00
|
|
|
const message = container.querySelector('.general-error')
|
|
|
|
message.style.display = 'block'
|
|
|
|
console.error(data)
|
2021-12-09 19:02:49 +01:00
|
|
|
}
|
2021-05-05 10:02:57 +02:00
|
|
|
}
|
2022-06-03 23:48:19 +02:00
|
|
|
|
|
|
|
export function showLoadingIndicator(container) {
|
|
|
|
const indicator = container.querySelector('.loading-indicator')
|
|
|
|
indicator.style.display = 'block'
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideLoadingIndicator(container) {
|
|
|
|
const indicator = container.querySelector('.loading-indicator')
|
|
|
|
indicator.style.display = 'none'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hideErrorMessages(container) {
|
|
|
|
container.querySelector('.group-not-found').style.display = 'none'
|
|
|
|
container.querySelector('.general-error').style.display = 'none'
|
|
|
|
}
|