vision refactor
This commit is contained in:
parent
6eb510e7d7
commit
35af854428
@ -242,7 +242,7 @@ export default function VisionPage() {
|
||||
};
|
||||
|
||||
// Handle save meeting
|
||||
const handleSaveMeeting = () => {
|
||||
const handleSaveMeeting = async () => {
|
||||
if (!meetingForm.type || !meetingForm.entityId || !meetingForm.start || !meetingForm.end) {
|
||||
toast({
|
||||
title: "Erreur",
|
||||
@ -252,75 +252,150 @@ export default function VisionPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
const startDate = new Date(meetingForm.start);
|
||||
const endDate = new Date(meetingForm.end);
|
||||
|
||||
// Create meetings based on recurrence
|
||||
const meetings: ScheduledMeeting[] = [];
|
||||
|
||||
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}`,
|
||||
if (!session?.user?.id) {
|
||||
toast({
|
||||
title: "Erreur",
|
||||
description: "Vous devez être connecté",
|
||||
variant: "destructive",
|
||||
});
|
||||
} else {
|
||||
// Recurring meetings - create for next 12 occurrences
|
||||
const recurrenceCount = 12;
|
||||
let currentDate = new Date(startDate);
|
||||
return;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
for (let i = 0; i < recurrenceCount; i++) {
|
||||
const dateStr = currentDate.toISOString().split('T')[0];
|
||||
const timeStr = meetingForm.allDay ? "" : currentDate.toTimeString().slice(0, 5);
|
||||
// Create events based on recurrence
|
||||
const eventsToCreate: Array<{ start: Date; end: Date }> = [];
|
||||
|
||||
if (meetingForm.recurrence === "none") {
|
||||
// Single event
|
||||
eventsToCreate.push({ start: startDate, end: endDate });
|
||||
} else {
|
||||
// Recurring events - create for next 12 occurrences
|
||||
const recurrenceCount = 12;
|
||||
let currentStart = new Date(startDate);
|
||||
|
||||
meetings.push({
|
||||
id: `${Date.now()}-${i}-${Math.random()}`,
|
||||
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,
|
||||
entityId: meetingForm.entityId,
|
||||
entityName: meetingForm.entityName,
|
||||
date: dateStr,
|
||||
time: timeStr,
|
||||
title: meetingForm.title || `${meetingForm.type === "group" ? "Groupe" : "Mission"}: ${meetingForm.entityName}`,
|
||||
});
|
||||
|
||||
// Calculate next occurrence
|
||||
if (meetingForm.recurrence === "daily") {
|
||||
currentDate.setDate(currentDate.getDate() + 1);
|
||||
} else if (meetingForm.recurrence === "weekly") {
|
||||
currentDate.setDate(currentDate.getDate() + 7);
|
||||
} else if (meetingForm.recurrence === "monthly") {
|
||||
currentDate.setMonth(currentDate.getMonth() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
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`,
|
||||
});
|
||||
setScheduledMeetings([...scheduledMeetings, ...meetings]);
|
||||
setShowMeetingDialog(false);
|
||||
setMeetingForm({
|
||||
type: "",
|
||||
entityId: "",
|
||||
entityName: "",
|
||||
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",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Handle delete meeting
|
||||
|
||||
Loading…
Reference in New Issue
Block a user