From 76b186ca0f244613daf5fef1de25ff8380773544 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 15 Jun 2024 00:55:29 +0200 Subject: [PATCH] Display the list of calendars on accommodations settings page --- .../AccommodationsCalendarURLApi.tsx | 26 ++++ .../accommodations/InstallCalendarDialog.tsx | 2 - .../AccommodationsSettingsRoute.tsx | 125 +++++++++++++++++- 3 files changed, 148 insertions(+), 5 deletions(-) diff --git a/geneit_app/src/api/accommodations/AccommodationsCalendarURLApi.tsx b/geneit_app/src/api/accommodations/AccommodationsCalendarURLApi.tsx index b898904..c263beb 100644 --- a/geneit_app/src/api/accommodations/AccommodationsCalendarURLApi.tsx +++ b/geneit_app/src/api/accommodations/AccommodationsCalendarURLApi.tsx @@ -40,4 +40,30 @@ export class AccommodationsCalendarURLApi { static CalendarURL(c: AccommodationCalendarURL): string { return `${APIClient.backendURL()}/acccommodations_calendar/${c.token}`; } + + /** + * Get accommodations calendars list + */ + static async GetList(family: Family): Promise { + return ( + await APIClient.exec({ + method: "GET", + uri: `/family/${family.family_id}/accommodations/reservations_calendars/list`, + }) + ).data; + } + + /** + * Delete an accommodation calendar + */ + static async Delete( + calendar: AccommodationCalendarURL + ): Promise { + return ( + await APIClient.exec({ + method: "DELETE", + uri: `/family/${calendar.family_id}/accommodations/reservations_calendars/${calendar.id}`, + }) + ).data; + } } diff --git a/geneit_app/src/dialogs/accommodations/InstallCalendarDialog.tsx b/geneit_app/src/dialogs/accommodations/InstallCalendarDialog.tsx index 2c6ac89..91a92fb 100644 --- a/geneit_app/src/dialogs/accommodations/InstallCalendarDialog.tsx +++ b/geneit_app/src/dialogs/accommodations/InstallCalendarDialog.tsx @@ -9,7 +9,6 @@ import { FormControl, IconButton, InputAdornment, - InputLabel, OutlinedInput, Typography, } from "@mui/material"; @@ -37,7 +36,6 @@ export function InstallCalendarDialog(p: {
- URL (); const [success, setSuccess] = React.useState(); + const [list, setList] = React.useState< + AccommodationCalendarURL[] | undefined + >(); + const family = useFamily(); const createCalendarURLDialog = useCreateAccommodationCalendarURL(); const calendarURLDialog = useInstallCalendarDialog(); + const load = async () => { + setList(await AccommodationsCalendarURLApi.GetList(family.family)); + }; + + const reload = () => { + key.current += 1; + setList(undefined); + }; + + const onRequestDelete = async (c: AccommodationCalendarURL) => { + setError(undefined); + setSuccess(undefined); + try { + if ( + !(await confirm( + `Voulez-vous vraiment supprimer le calendrier '${c.name}' ? Cette opération est définitive !` + )) + ) + return; + + loading.show("Suppression du calendrier en cours..."); + + await AccommodationsCalendarURLApi.Delete(c); + + snackbar("Le calendrier a été supprimé avec succès !"); + reload(); + } catch (e) { + console.error("Failed to delete accommodation!", e); + setError(`Échec de la suppression du logement! ${e}`); + } finally { + loading.hide(); + } + }; + const createCalendarURL = async () => { try { const newCal = await createCalendarURLDialog(); if (!newCal) return; - loading.show("Création du logement en cours..."); + loading.show("Création du calendrier en cours..."); const cal = await AccommodationsCalendarURLApi.Create( family.family, @@ -241,7 +287,7 @@ function AccommodationsCalURLsCard(): React.ReactElement { setSuccess("Le calendrier a été créé avec succès !"); - // TODO : reload URLS list + reload(); calendarURLDialog(cal); } catch (e) { @@ -274,7 +320,80 @@ function AccommodationsCalURLsCard(): React.ReactElement { > Créer un calendrier + +
+
+ + + list?.length === 0 ? ( + <> +

+ Vous n'avez créé aucun calendrier pour le moment ! +

+ + ) : ( + <> + {list?.map((c) => ( + + ))} + + ) + } + /> ); } + +function CalendarItem(p: { + c: AccommodationCalendarURL; + onRequestDelete: (c: AccommodationCalendarURL) => void; +}): React.ReactElement { + const accommodations = useAccommodations(); + + const installCal = useInstallCalendarDialog(); + + return ( + + + + + {p.c.name} + + + {p.c.accommodation_id + ? accommodations.accommodations.get(p.c.accommodation_id)?.name + : "Tous les logements"} + + + Créé il y a +
+ Utilisé il y a +
+
+ + + + + + +
+ ); +}