From 8665dd5576ea1ce3fea1c3985852a051a8b8811a Mon Sep 17 00:00:00 2001 From: Andrei Duvakin Date: Tue, 28 Jan 2025 19:20:40 +0500 Subject: [PATCH] ._. --- WEB/app/src/app/components/сalendar.js | 50 +++++++++++++++++++++----- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/WEB/app/src/app/components/сalendar.js b/WEB/app/src/app/components/сalendar.js index 044074b..5425c44 100644 --- a/WEB/app/src/app/components/сalendar.js +++ b/WEB/app/src/app/components/сalendar.js @@ -5,6 +5,7 @@ import {useEffect, useState} from "react"; const Calendar = () => { const [currentDate, setCurrentDate] = useState(new Date()); const [birthdays, setBirthdays] = useState([]); + const [events, setEvents] = useState([]); const today = new Date(); const year = currentDate.getFullYear(); @@ -12,6 +13,7 @@ const Calendar = () => { useEffect(() => { fetchBirthdays(); + fetchEvents(); }, [year, month]); const fetchBirthdays = async () => { @@ -26,6 +28,24 @@ const Calendar = () => { } }; + const fetchEvents = async () => { + try { + const response = await fetch("http://localhost:5000/events"); + if (response.ok) { + const data = await response.json(); + setEvents(data); + } + } catch (error) { + console.error("Ошибка загрузки событий:", error); + } + }; + + const getEventColor = (eventCount) => { + if (eventCount >= 5) return "#FC4343"; + if (eventCount < 2) return "#89FC43"; + return "#F8FC43"; + }; + const getBirthdaysForDay = (day) => { return birthdays.filter((user) => { const birthday = new Date(user.birthday); @@ -33,6 +53,13 @@ const Calendar = () => { }); }; + const getEventsForDay = (day) => { + return events.filter(event => { + const eventDate = new Date(event.date); + return eventDate.getDate() === day && eventDate.getMonth() === month; + }); + }; + const prevMonth = () => setCurrentDate(new Date(year, month - 1, 1)); const nextMonth = () => setCurrentDate(new Date(year, month + 1, 1)); @@ -41,7 +68,7 @@ const Calendar = () => { const daysInMonth = new Date(year, month + 1, 0).getDate(); // Количество дней в месяце const startOffset = firstDayOfMonth === 0 ? 6 : firstDayOfMonth - 1; - const daysArray = Array.from({ length: daysInMonth }, (_, i) => i + 1); + const daysArray = Array.from({length: daysInMonth}, (_, i) => i + 1); return [...Array(startOffset).fill(null), ...daysArray]; }; @@ -52,7 +79,7 @@ const Calendar = () => {
-

{currentDate.toLocaleString("ru-RU", { month: "long", year: "numeric" })}

+

{currentDate.toLocaleString("ru-RU", {month: "long", year: "numeric"})}

@@ -60,8 +87,14 @@ const Calendar = () => { {weekDays.map((day) => (
{day}
))} - {days.map((day, index) => - day ? ( + {days.map((day, index) => { + if (!day) return
; + + const dayEvents = getEventsForDay(day); + const eventCount = dayEvents.length; + const backgroundColor = eventCount > 0 ? getEventColor(eventCount) : "transparent"; + + return (
{ ? "current-day" : "" }`} + style={{backgroundColor}} > {day} {getBirthdaysForDay(day).length > 0 && (
- {""} + 🎂
{getBirthdaysForDay(day).map(user => (
{user.last_name} {user.first_name}
@@ -84,10 +118,8 @@ const Calendar = () => {
)}
- ) : ( -
- ) - )} + ); + })}
);