setIsAllEventsOpen(true)}
+ className={`justify-end rounded-[100px] text-sm font-bold ${isToday ? "text-slate-200 bg-[#9353d3] border-2 border-slate-300" : "bg-slate-800 text-white dark:text-white"}`}
>
{day}
-
-
- {showEvents(safeEvents, cellDate, handleOpen)}
+
+
+ {showEvents(safeEvents, cellDate, handleOpen, isMobile)}
+ {hasMoreEvents && (
+
+ )}
+
-
-
+
setIsAllEventsOpen(false)}
+ size="md"
+ >
+
+ Eventi del {day} {monthNames[date.getMonth()]}
+
+
+ {todayEvents.map((event, index) => (
+
{
+ handleOpen(event);
+ setIsAllEventsOpen(false);
+ }}
+ >
+
+
+ {formatEventTime(event)}
+
+ {" - "}
+ {event.title.toString()}
+
+
+ {formatDate(new Date(event.dtstart))}
+
+
+ ))}
+
+
+
+
+
);
};
diff --git a/client/components/calendar/eventAdder.tsx b/client/components/calendar/eventAdder.tsx
index c4d042f..80bcb71 100644
--- a/client/components/calendar/eventAdder.tsx
+++ b/client/components/calendar/eventAdder.tsx
@@ -1,6 +1,6 @@
"use client";
-import React, { useState, useContext } from "react";
+import React, { useState, useContext, useEffect } from "react";
import {
Button,
Modal,
@@ -11,6 +11,13 @@ import {
Input,
Textarea,
Switch,
+ Autocomplete,
+ AutocompleteItem,
+ Avatar,
+ Dropdown,
+ DropdownTrigger,
+ DropdownMenu,
+ DropdownItem,
} from "@nextui-org/react";
import RepetitionMenu from "@/components/calendar/repetitionMenu";
import EventDatePicker from "@/components/calendar/eventDatePicker";
@@ -18,7 +25,7 @@ import {
SelfieEvent,
SelfieNotification,
FrequencyType,
- Person,
+ People,
} from "@/helpers/types";
import NotificationMenu from "./notificationMenu";
const EVENTS_API_URL = "/api/events";
@@ -32,7 +39,7 @@ async function createEvent(event: SelfieEvent): Promise
{
"Content-Type": "application/json",
},
body: JSON.stringify({ event: event }),
- cache: "no-store", // This ensures fresh data on every request
+ cache: "no-store",
});
if (res.status === 401) {
@@ -48,75 +55,135 @@ async function createEvent(event: SelfieEvent): Promise {
return true;
}
-export default function EventAdder() {
- const [isOpen, setIsOpen] = useState(false);
- const [eventData, setEventData] = useState>({
+const initialEvent = {
+ title: "",
+ summary: "",
+ status: "confirmed",
+ transp: "OPAQUE",
+ dtstart: new Date(),
+ dtend: new Date(),
+ dtstamp: new Date().toISOString(),
+ categories: [""],
+ location: "",
+ description: "",
+ URL: "",
+ participants: [] as People,
+ rrule: {
+ freq: "weekly" as FrequencyType,
+ interval: 1,
+ bymonth: 1,
+ bymonthday: 1,
+ },
+ notification: {
title: "",
- summary: "",
- status: "confirmed",
- transp: "OPAQUE",
- dtstart: new Date(),
- dtend: new Date(),
- dtstamp: new Date().toISOString(),
- categories: [""],
- location: "",
description: "",
- URL: "",
- participants: [] as Person[],
- rrule: {
- freq: "weekly",
- interval: 1,
- bymonth: 1,
- bymonthday: 1,
- },
- notification: {
- title: "",
- description: "",
- type: "",
- repetition: {
- freq: "",
- interval: 1,
- },
- fromDate: new Date(),
+ type: "",
+ repetition: {
+ freq: "",
+ interval: 0,
},
- });
+ fromDate: new Date(),
+ },
+};
+
+interface EventAdderProps {
+ friends: People;
+}
+
+const EventAdder: React.FC = ({
+ friends,
+}) => {
+ const [isOpen, setIsOpen] = useState(false);
+ const [eventData, setEventData] = useState>(initialEvent);
const [repeatEvent, setRepeatEvent] = useState(false);
const [allDayEvent, setAllDayEvent] = useState(false);
const [notifications, setNotifications] = useState(false);
+ const [isError, setIsError] = useState(false);
+ const [notificationError, setNotificationError] = useState(false);
const { reloadEvents, setReloadEvents } = useContext(reloadContext) as any;
+ const availableFriends = friends.filter(friend =>
+ !eventData.participants?.some(participant => participant.email === friend.email)
+ );
+
+ useEffect(() => {
+ setEventData((prev: any) => ({
+ ...prev,
+ notification: {
+ ...prev.notification,
+ title: prev.notification?.title || "",
+ description: prev.notification?.description || "",
+ type: "",
+ fromDate: new Date(),
+ repetition: {
+ freq: "",
+ interval: 0,
+ },
+ }
+ }));
+ console.log(eventData.notification);
+ }, [eventData.dtstart, eventData.dtend]);
+
const handleOpen = () => {
setIsOpen(true);
};
- const handleClose = () => {
- setIsOpen(false);
- };
-
const handleInputChange = (
e:
| React.ChangeEvent
| { target: { name: string; value: any } },
) => {
- var { name, value } = e.target;
- if (name.startsWith("notification.")) {
- const notificationField = name.split(".")[1];
- if (name.endsWith("fromDate")) {
- value = new Date(value).toISOString();
- }
- setEventData((prev: any) => ({
- ...prev,
- notification: {
- ...prev.notification,
- [notificationField]: value,
- },
- }));
- console.log("name: ", name, "value: ", value);
+ const { name, value } = e.target;
+
+ if (name.startsWith("title")) {
+ setIsError(false);
+ }
+
+ if (name.startsWith("notification")) {
+ const [_, notificationField, repetitionField] = name.split(".");
+
+ setEventData((prev: any) => {
+ // Se stiamo gestendo un campo di repetition
+ if (notificationField === "repetition") {
+ return {
+ ...prev,
+ notification: {
+ ...prev.notification,
+ repetition: {
+ ...prev.notification.repetition,
+ [repetitionField]: value
+ }
+ }
+ };
+ }
+
+ // Se stiamo gestendo fromDate
+ if (notificationField === "fromDate") {
+ return {
+ ...prev,
+ notification: {
+ ...prev.notification,
+ fromDate: new Date(value).toISOString()
+ }
+ };
+ }
+
+ // Per tutti gli altri campi della notification
+ return {
+ ...prev,
+ notification: {
+ ...prev.notification,
+ [notificationField]: value
+ }
+ };
+ });
} else {
- setEventData((prev) => ({ ...prev, [name]: value }));
+ // Per tutti i campi non correlati alla notification
+ setEventData(prev => ({ ...prev, [name]: value }));
}
};
+
const handleDateChange = (start: Date | string, end: Date | string) => {
setEventData((prev) => ({
...prev,
@@ -155,37 +222,76 @@ export default function EventAdder() {
}));
};
+ const handleParticipantSelect = (friend: any) => {
+ setEventData((prev) => ({
+ ...prev,
+ participants: [...(prev.participants || []), friend],
+ }));
+ };
+
+ const handleRemoveParticipant = (friendToRemove: any) => {
+ setEventData((prev) => ({
+ ...prev,
+ participants: (prev.participants || []).filter(
+ (friend) => friend.email !== friendToRemove.email
+ ),
+ }));
+ };
+
+ const handleRemoveAllParticipants = () => {
+ setEventData((prev) => ({
+ ...prev,
+ participants: [],
+ }));
+ };
+
+ const handleExit = () => {
+ setIsOpen(false);
+ setIsError(false);
+ setNotificationError(false);
+ setEventData(initialEvent);
+ }
+
+ const handleSave = () => {
+ if (eventData.title === "") {
+ setIsError(true);
+ } else {
+ setIsError(false);
+ }
+ };
+
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
- const newEvent: SelfieEvent = {
- ...eventData,
- sequence: 0,
- categories: eventData.categories || [],
- participants: eventData.participants || [],
- } as SelfieEvent;
-
- try {
- console.log("newEvent: ", newEvent);
- const success = await createEvent(newEvent);
- console.log("success: ", success);
- if (success) {
- console.log("Event created successfully");
- handleClose();
- } else {
- console.error("Failed to create event");
+ console.log(isError, notificationError);
+ if (!isError && !notificationError) {
+ const newEvent: SelfieEvent = {
+ ...eventData,
+ sequence: 0,
+ categories: eventData.categories || [],
+ participants: eventData.participants || [],
+ } as SelfieEvent;
+
+ try {
+ const success = await createEvent(newEvent);
+ if (success) {
+ console.log("Event created successfully");
+ handleExit();
+ } else {
+ console.error("Failed to create event");
+ }
+ } catch (error) {
+ console.error("Error submitting event", error);
}
- } catch (error) {
- console.error("Error submitting event", error);
- }
- setReloadEvents(true);
+ setReloadEvents(true);
+ }
};
return (
<>