From bd3e8df4aa980e8b80aa88ea24cc434ce7bbee38 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 21 Jun 2024 20:01:43 +0200 Subject: [PATCH] Can change a reservation's dates --- .../AccommodationsReservationsApi.tsx | 17 +++++ .../AccommodationsReservationsRoute.tsx | 70 ++++++++++++++++--- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/geneit_app/src/api/accommodations/AccommodationsReservationsApi.tsx b/geneit_app/src/api/accommodations/AccommodationsReservationsApi.tsx index 3cb5a1a..20801f9 100644 --- a/geneit_app/src/api/accommodations/AccommodationsReservationsApi.tsx +++ b/geneit_app/src/api/accommodations/AccommodationsReservationsApi.tsx @@ -118,6 +118,23 @@ export class AccommodationsReservationsApi { return new AccommodationsReservationsList(data); } + /** + * Update a reservation + */ + static async Update( + family: Family, + r: UpdateAccommodationReservation + ): Promise { + await APIClient.exec({ + method: "PATCH", + uri: `/family/${family.family_id}/accommodations/reservation/${r.reservation_id}`, + jsonData: { + start: r.start, + end: r.end, + }, + }); + } + /** * Delete a reservation */ diff --git a/geneit_app/src/routes/family/accommodations/AccommodationsReservationsRoute.tsx b/geneit_app/src/routes/family/accommodations/AccommodationsReservationsRoute.tsx index 9d99e99..f36211c 100644 --- a/geneit_app/src/routes/family/accommodations/AccommodationsReservationsRoute.tsx +++ b/geneit_app/src/routes/family/accommodations/AccommodationsReservationsRoute.tsx @@ -5,6 +5,7 @@ import interactionPlugin from "@fullcalendar/interaction"; import listPlugin from "@fullcalendar/list"; import FullCalendar from "@fullcalendar/react"; import DeleteIcon from "@mui/icons-material/Delete"; +import EditIcon from "@mui/icons-material/Edit"; import { Alert, Avatar, @@ -172,6 +173,44 @@ export function AccommodationsReservationsRoute(): React.ReactElement { }); }; + const changeReservation = async (r: AccommodationReservation) => { + try { + const ac = accommodations.accommodations.get(r.accommodation_id); + if ( + ac?.need_validation && + !(await confirm( + "Voulez-vous vraiment changer cette réservation ? Celle-ci devra être de nouveau validée !" + )) + ) + return; + + const newResa = await updateReservation( + { + reservation_id: r.id, + accommodation_id: r.accommodation_id, + start: r.reservation_start, + end: r.reservation_end, + }, + false + ); + + if (!newResa) return; + + setActiveEvent(undefined); + loadingMessage.show("Mise à jour de la réservation en cours..."); + + await AccommodationsReservationsApi.Update(family.family, newResa); + + reload(); + snackbar("La réservation a été mise à jour avec succès !"); + } catch (e) { + console.error("Failed to update a reservation!", e); + alert("Échec de la mise à jour de la réservation!"); + } finally { + loadingMessage.hide(); + } + }; + const deleteReservation = async (r: AccommodationReservation) => { try { if ( @@ -428,16 +467,27 @@ export function AccommodationsReservationsRoute(): React.ReactElement { {user.user.id === activeEvent?.reservation.user_id && ( - - - deleteReservation(activeEvent?.reservation) - } - > - - - + <> + + + changeReservation(activeEvent?.reservation) + } + > + + + + + + deleteReservation(activeEvent?.reservation) + } + > + + + + )}