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 17:14:55 +00:00
|
|
|
import { useSeriesStore } from '../store/index'
|
2021-10-15 13:37:54 +00:00
|
|
|
import 'regenerator-runtime/runtime'
|
2021-03-11 20:24:44 +00:00
|
|
|
|
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])
|
2021-05-15 18:14:41 +00:00
|
|
|
} 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 = () => {
|
2021-10-20 14:33:06 +00:00
|
|
|
const [series, episodes, setSeries, setEpisodes] = useSeriesStore(store => [store.series, store.episodes, store.setSeries, store.setEpisodes])
|
2021-10-15 13:37:54 +00:00
|
|
|
const [loading, setLoading] = useState(!!series.length)
|
2021-10-11 11:48:17 +00:00
|
|
|
|
2021-10-11 17:14:55 +00:00
|
|
|
|
2021-10-11 11:48:17 +00:00
|
|
|
async function fetchData() {
|
2021-10-11 17:14:55 +00:00
|
|
|
if (!series.length) {
|
|
|
|
setLoading(true)
|
2021-10-11 11:48:17 +00:00
|
|
|
|
2021-10-11 17:14:55 +00:00
|
|
|
const { data: responseData } = await axios.get(
|
|
|
|
`${config.EVENTS_API_URL}/events`
|
|
|
|
)
|
2021-10-11 11:48:17 +00:00
|
|
|
|
2021-10-11 17:14:55 +00:00
|
|
|
setSeries(responseData)
|
2021-10-20 14:33:06 +00:00
|
|
|
// setEpisodes()
|
|
|
|
|
|
|
|
// console.log({ episodes })
|
2021-10-11 11:48:17 +00:00
|
|
|
|
2021-10-11 17:14:55 +00:00
|
|
|
setLoading(false)
|
|
|
|
}
|
2021-10-11 11:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchData()
|
|
|
|
}, [])
|
|
|
|
|
2021-10-11 17:14:55 +00:00
|
|
|
return { loading, data: series }
|
2021-10-11 11:48:17 +00:00
|
|
|
}
|