vision refactor
This commit is contained in:
parent
6eb510e7d7
commit
35af854428
@ -242,7 +242,7 @@ export default function VisionPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Handle save meeting
|
// Handle save meeting
|
||||||
const handleSaveMeeting = () => {
|
const handleSaveMeeting = async () => {
|
||||||
if (!meetingForm.type || !meetingForm.entityId || !meetingForm.start || !meetingForm.end) {
|
if (!meetingForm.type || !meetingForm.entityId || !meetingForm.start || !meetingForm.end) {
|
||||||
toast({
|
toast({
|
||||||
title: "Erreur",
|
title: "Erreur",
|
||||||
@ -252,75 +252,150 @@ export default function VisionPage() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(meetingForm.start);
|
if (!session?.user?.id) {
|
||||||
const endDate = new Date(meetingForm.end);
|
toast({
|
||||||
|
title: "Erreur",
|
||||||
// Create meetings based on recurrence
|
description: "Vous devez être connecté",
|
||||||
const meetings: ScheduledMeeting[] = [];
|
variant: "destructive",
|
||||||
|
|
||||||
if (meetingForm.recurrence === "none") {
|
|
||||||
// Single meeting
|
|
||||||
const dateStr = startDate.toISOString().split('T')[0];
|
|
||||||
const timeStr = meetingForm.allDay ? "" : startDate.toTimeString().slice(0, 5);
|
|
||||||
|
|
||||||
meetings.push({
|
|
||||||
id: `${Date.now()}-${Math.random()}`,
|
|
||||||
type: meetingForm.type,
|
|
||||||
entityId: meetingForm.entityId,
|
|
||||||
entityName: meetingForm.entityName,
|
|
||||||
date: dateStr,
|
|
||||||
time: timeStr,
|
|
||||||
title: meetingForm.title || `${meetingForm.type === "group" ? "Groupe" : "Mission"}: ${meetingForm.entityName}`,
|
|
||||||
});
|
});
|
||||||
} else {
|
return;
|
||||||
// Recurring meetings - create for next 12 occurrences
|
}
|
||||||
const recurrenceCount = 12;
|
|
||||||
let currentDate = new Date(startDate);
|
try {
|
||||||
|
// Fetch user calendars to find the matching calendar
|
||||||
|
const calendarsRes = await fetch("/api/calendars");
|
||||||
|
if (!calendarsRes.ok) {
|
||||||
|
throw new Error("Impossible de charger les calendriers");
|
||||||
|
}
|
||||||
|
const calendars = await calendarsRes.json();
|
||||||
|
|
||||||
|
// Find the calendar for this mission or group
|
||||||
|
let targetCalendar = null;
|
||||||
|
if (meetingForm.type === "mission") {
|
||||||
|
// For missions, look for calendar with missionId or name starting with "Mission:"
|
||||||
|
targetCalendar = calendars.find((cal: any) =>
|
||||||
|
cal.missionId === meetingForm.entityId ||
|
||||||
|
cal.name === `Mission: ${meetingForm.entityName}`
|
||||||
|
);
|
||||||
|
} else if (meetingForm.type === "group") {
|
||||||
|
// For groups, look for calendar with name starting with "Groupe:"
|
||||||
|
targetCalendar = calendars.find((cal: any) =>
|
||||||
|
cal.name === `Groupe: ${meetingForm.entityName}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!targetCalendar) {
|
||||||
|
toast({
|
||||||
|
title: "Erreur",
|
||||||
|
description: `Calendrier ${meetingForm.type === "group" ? "du groupe" : "de la mission"} introuvable`,
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const startDate = new Date(meetingForm.start);
|
||||||
|
const endDate = new Date(meetingForm.end);
|
||||||
const timeDiff = endDate.getTime() - startDate.getTime();
|
const timeDiff = endDate.getTime() - startDate.getTime();
|
||||||
|
|
||||||
for (let i = 0; i < recurrenceCount; i++) {
|
// Create events based on recurrence
|
||||||
const dateStr = currentDate.toISOString().split('T')[0];
|
const eventsToCreate: Array<{ start: Date; end: Date }> = [];
|
||||||
const timeStr = meetingForm.allDay ? "" : currentDate.toTimeString().slice(0, 5);
|
|
||||||
|
|
||||||
meetings.push({
|
if (meetingForm.recurrence === "none") {
|
||||||
id: `${Date.now()}-${i}-${Math.random()}`,
|
// Single event
|
||||||
|
eventsToCreate.push({ start: startDate, end: endDate });
|
||||||
|
} else {
|
||||||
|
// Recurring events - create for next 12 occurrences
|
||||||
|
const recurrenceCount = 12;
|
||||||
|
let currentStart = new Date(startDate);
|
||||||
|
|
||||||
|
for (let i = 0; i < recurrenceCount; i++) {
|
||||||
|
const currentEnd = new Date(currentStart.getTime() + timeDiff);
|
||||||
|
eventsToCreate.push({
|
||||||
|
start: new Date(currentStart),
|
||||||
|
end: currentEnd
|
||||||
|
});
|
||||||
|
|
||||||
|
// Calculate next occurrence
|
||||||
|
if (meetingForm.recurrence === "daily") {
|
||||||
|
currentStart.setDate(currentStart.getDate() + 1);
|
||||||
|
} else if (meetingForm.recurrence === "weekly") {
|
||||||
|
currentStart.setDate(currentStart.getDate() + 7);
|
||||||
|
} else if (meetingForm.recurrence === "monthly") {
|
||||||
|
currentStart.setMonth(currentStart.getMonth() + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create all events via API
|
||||||
|
const eventPromises = eventsToCreate.map(async ({ start, end }) => {
|
||||||
|
const response = await fetch("/api/events", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
title: meetingForm.title || `Réunion ${meetingForm.type === "group" ? "du groupe" : "de la mission"}: ${meetingForm.entityName}`,
|
||||||
|
description: meetingForm.description || null,
|
||||||
|
start: start.toISOString(),
|
||||||
|
end: end.toISOString(),
|
||||||
|
allDay: meetingForm.allDay,
|
||||||
|
location: meetingForm.location || null,
|
||||||
|
calendarId: targetCalendar.id,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json();
|
||||||
|
throw new Error(error.error || "Erreur lors de la création de l'événement");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json();
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(eventPromises);
|
||||||
|
|
||||||
|
// Also update localStorage for Vision page display
|
||||||
|
const meetings: ScheduledMeeting[] = eventsToCreate.map(({ start }, index) => {
|
||||||
|
const dateStr = start.toISOString().split('T')[0];
|
||||||
|
const timeStr = meetingForm.allDay ? "" : start.toTimeString().slice(0, 5);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: `${Date.now()}-${index}-${Math.random()}`,
|
||||||
type: meetingForm.type,
|
type: meetingForm.type,
|
||||||
entityId: meetingForm.entityId,
|
entityId: meetingForm.entityId,
|
||||||
entityName: meetingForm.entityName,
|
entityName: meetingForm.entityName,
|
||||||
date: dateStr,
|
date: dateStr,
|
||||||
time: timeStr,
|
time: timeStr,
|
||||||
title: meetingForm.title || `${meetingForm.type === "group" ? "Groupe" : "Mission"}: ${meetingForm.entityName}`,
|
title: meetingForm.title || `${meetingForm.type === "group" ? "Groupe" : "Mission"}: ${meetingForm.entityName}`,
|
||||||
});
|
};
|
||||||
|
});
|
||||||
|
|
||||||
// Calculate next occurrence
|
setScheduledMeetings([...scheduledMeetings, ...meetings]);
|
||||||
if (meetingForm.recurrence === "daily") {
|
setShowMeetingDialog(false);
|
||||||
currentDate.setDate(currentDate.getDate() + 1);
|
setMeetingForm({
|
||||||
} else if (meetingForm.recurrence === "weekly") {
|
type: "",
|
||||||
currentDate.setDate(currentDate.getDate() + 7);
|
entityId: "",
|
||||||
} else if (meetingForm.recurrence === "monthly") {
|
entityName: "",
|
||||||
currentDate.setMonth(currentDate.getMonth() + 1);
|
title: "",
|
||||||
}
|
start: "",
|
||||||
}
|
end: "",
|
||||||
|
allDay: false,
|
||||||
|
location: "",
|
||||||
|
description: "",
|
||||||
|
recurrence: "none",
|
||||||
|
});
|
||||||
|
toast({
|
||||||
|
title: "Succès",
|
||||||
|
description: `${eventsToCreate.length} réunion${eventsToCreate.length > 1 ? 's' : ''} planifiée${eventsToCreate.length > 1 ? 's' : ''} avec succès`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error saving meeting:", error);
|
||||||
|
toast({
|
||||||
|
title: "Erreur",
|
||||||
|
description: error instanceof Error ? error.message : "Erreur lors de la planification de la réunion",
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setScheduledMeetings([...scheduledMeetings, ...meetings]);
|
|
||||||
setShowMeetingDialog(false);
|
|
||||||
setMeetingForm({
|
|
||||||
type: "",
|
|
||||||
entityId: "",
|
|
||||||
entityName: "",
|
|
||||||
title: "",
|
|
||||||
start: "",
|
|
||||||
end: "",
|
|
||||||
allDay: false,
|
|
||||||
location: "",
|
|
||||||
description: "",
|
|
||||||
recurrence: "none",
|
|
||||||
});
|
|
||||||
toast({
|
|
||||||
title: "Succès",
|
|
||||||
description: `${meetings.length} réunion${meetings.length > 1 ? 's' : ''} planifiée${meetings.length > 1 ? 's' : ''} avec succès`,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle delete meeting
|
// Handle delete meeting
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user