Files
Homarr/components/calendar/CalendarComponent.tsx

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-05-03 19:52:09 +02:00
import {
Group,
Indicator,
Popover,
Box,
Container,
Text,
Avatar,
ActionIcon,
Center,
} from '@mantine/core';
2022-05-01 14:47:06 +02:00
import { useEffect, useState } from 'react';
2022-04-30 21:36:46 +02:00
import { Calendar } from '@mantine/dates';
2022-04-30 21:39:59 +02:00
import dayjs from 'dayjs';
2022-04-30 21:36:46 +02:00
import MediaDisplay from './MediaDisplay';
import { medias } from './mediaExample';
import { useConfig } from '../../tools/state';
2022-04-30 21:36:46 +02:00
2022-05-01 14:47:06 +02:00
async function GetCalendars(endDate: Date) {
2022-04-30 21:36:46 +02:00
// Load context
const { config, addService, removeService, setConfig } = useConfig();
2022-05-01 14:47:06 +02:00
// Load services that have the type to "Sonarr" or "Radarr"
const sonarrServices = config.services.filter((service) => service.type === 'Sonarr');
const radarrServices = config.services.filter((service) => service.type === 'Radarr');
2022-05-01 14:47:06 +02:00
// Merge the two arrays
const allServices = [...sonarrServices, ...radarrServices];
// Load the calendars for each service
const Calendars = await Promise.all(
allServices.map((service) =>
fetch(`${service.url}/api/v3/calendar?end=${endDate}?apikey=${service.apiKey}`)
)
);
2022-04-30 21:36:46 +02:00
}
export default function CalendarComponent(props: any) {
const [opened, setOpened] = useState(false);
2022-05-01 14:47:06 +02:00
// const [medias, setMedias] = useState();
2022-04-30 21:36:46 +02:00
const dates = medias.map((media) => media.inCinemas);
const parsedDates = dates.map((date) => dayjs(date));
2022-05-01 14:47:06 +02:00
// useEffect(() => {
// const { services } = props;
// // Get the url and API key for each service
// const serviceUrls = services.map((service: serviceItem) => {
// return {
// url: service.url,
// apiKey: service.apiKey,
// };
// });
// // Get the medias from each service
// const promises = serviceUrls.map((service: serviceItem) => {
// return fetch(`${service.url}/api/v3/calendar`, {
// method: 'GET',
// headers: {
// 'Content-Type': 'application/json',
// 'X-Api-Key': service.apiKey,
// },
// });
// });
// // Wait for all the promises to resolve
// Promise.all(promises).then((responses) => {
// // Get the medias from each service
// const medias = responses.map((response) => {
// return response.json();
// });
// // Set the medias
// setMedias(medias);
// }
// );
// }, []);
2022-04-30 21:36:46 +02:00
return (
<Calendar
onChange={(day: any) => {}}
2022-04-30 21:39:59 +02:00
renderDay={(renderdate) => <DayComponent renderdate={renderdate} parsedDates={parsedDates} />}
2022-04-30 21:36:46 +02:00
/>
);
}
function DayComponent(props: any) {
const { renderdate, parsedDates }: { renderdate: Date; parsedDates: dayjs.Dayjs[] } = props;
const [opened, setOpened] = useState(false);
const day = renderdate.getDate();
const match = parsedDates.findIndex((date) => date.isSame(dayjs(renderdate), 'day'));
if (match > -1) {
return (
2022-05-03 19:52:09 +02:00
<Box
2022-04-30 21:36:46 +02:00
onClick={() => {
setOpened(true);
}}
2022-05-03 19:52:09 +02:00
style={{ height: '100%', width: '100%' }}
2022-04-30 21:36:46 +02:00
>
2022-05-03 19:52:09 +02:00
<Center>
<Indicator size={10} color="red">
<Popover
position="left"
width={700}
onClose={() => setOpened(false)}
opened={opened}
target={day}
children={<MediaDisplay media={medias[match]} />}
/>
</Indicator>
</Center>
</Box>
2022-04-30 21:36:46 +02:00
);
}
return <div>{day}</div>;
}