stream/src/hooks/data.js

120 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-10-11 11:48:17 +00:00
import { useEffect, useState } from 'preact/hooks'
2021-03-11 20:24:44 +00:00
import axios from 'axios'
2021-03-29 16:03:46 +00:00
import ICAL from 'ical.js'
2021-03-11 20:24:44 +00:00
import config from '../data/config'
2021-10-11 11:48:17 +00:00
export const useEventCalendar = () => {
2021-03-24 15:24:34 +00:00
const [data, setData] = useState([])
2021-03-11 20:24:44 +00:00
const [loading, setLoading] = useState(true)
async function fetchData() {
setLoading(true)
2021-05-26 10:59:12 +00:00
const { data: responseData } = await axios.get(
`https://cloud.undersco.re/remote.php/dav/public-calendars/${config.calendarId}/?export`
)
2021-03-29 16:03:46 +00:00
const jCalData = ICAL.parse(responseData)
const comp = new ICAL.Component(jCalData)
const vevents = comp.getAllSubcomponents('vevent')
const calEvents = vevents
2021-05-16 23:08:25 +00:00
.filter(
vevent =>
vevent.getFirstPropertyValue('status') === null ||
(vevent.getFirstPropertyValue('status') &&
vevent.getFirstPropertyValue('status').toUpperCase() ===
2021-10-11 11:48:17 +00:00
'CONFIRMED')
2021-05-16 23:08:25 +00:00
)
.map(vevent => {
const event = new ICAL.Event(vevent)
return event
})
2021-03-29 16:03:46 +00:00
.sort((a, b) => a.startDate.toJSDate() - b.startDate.toJSDate())
2021-03-24 15:24:34 +00:00
await Promise.all(
2021-03-29 16:03:46 +00:00
calEvents.map(async calItem => {
const url = calItem.component.getAllProperties('url')[0]
if (url) {
const id = url
.getFirstValue()
.split('/')
.pop()
2021-03-24 15:24:34 +00:00
const {
data: {
account,
category,
channel,
embedPath,
language,
state,
previewPath,
views,
duration,
},
} = await axios.get(`https://tv.undersco.re/api/v1/videos/${id}`)
const item = {
title: calItem.summary,
account,
category,
channel,
description: calItem.description,
embedPath,
language,
state,
previewPath,
views,
2021-03-29 16:03:46 +00:00
start: calItem.startDate.toJSDate(),
end: calItem.endDate.toJSDate(),
2021-03-24 15:24:34 +00:00
id,
duration,
2021-03-29 16:03:46 +00:00
videoUrl: url.getFirstValue(),
2021-03-24 15:24:34 +00:00
}
setData(arr => [...arr, item])
} else {
const item = {
title: calItem.summary,
description: calItem.description,
start: calItem.startDate.toJSDate(),
end: calItem.endDate.toJSDate(),
}
setData(arr => [...arr, item])
2021-03-24 15:24:34 +00:00
}
})
)
2021-03-11 20:24:44 +00:00
setLoading(false)
}
useEffect(() => {
fetchData()
}, [])
return { loading, data }
}
2021-10-11 11:48:17 +00:00
export const useEventApi = () => {
const [data, setData] = useState([])
const [loading, setLoading] = useState(true)
async function fetchData() {
setLoading(true)
const { data: responseData } = await axios.get(
`${config.EVENTS_API_URL}/events`
)
setData(responseData)
setLoading(false)
}
useEffect(() => {
fetchData()
}, [])
return { loading, data }
}