From 36c26f0ea09409af55e19489da3f285ca28120d1 Mon Sep 17 00:00:00 2001 From: superfaz <16510828+superfaz@users.noreply.github.com> Date: Fri, 11 Aug 2023 16:41:32 +0200 Subject: [PATCH] Add component for personal event cancelation --- front/src/My/MyEventView.js | 96 ++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/front/src/My/MyEventView.js b/front/src/My/MyEventView.js index 9d536a08..63763f38 100644 --- a/front/src/My/MyEventView.js +++ b/front/src/My/MyEventView.js @@ -1,11 +1,16 @@ import dayjs from "dayjs"; -import { useLoaderData, useParams } from "react-router-dom"; -import { useApiFetch, useDefinition } from "../api"; +import { useLoaderData, useParams, useRevalidator } from "react-router-dom"; +import { apiFetch, useApiFetch, useDefinition } from "../api"; import EntityDetail from "../Generic/EntityDetail"; import EntityList from "../Generic/EntityList"; import PageView from "../Generic/PageView"; import Sections from "../Generic/Sections"; import Section from "../Generic/Section"; +import { useDispatch } from "react-redux"; +import { addError } from "../Alerts/slice"; +import { disconnect } from "../Authentication"; +import { EventExtraMenu } from "../Events/components"; +import Modal from "../Generic/Modal"; function EventViewDetail() { const definition = useDefinition("EventForView"); @@ -28,26 +33,81 @@ function StatusList({ eventId }) { ); } +export function EventModals({ entity, next, onStatusChange }) { + if (next.length === 0) { + return null; + } + + return next.map((status, index) => { + switch (status.displayName) { + case "Canceled": + return ( + onStatusChange(status)} + /> + ); + case "Approved": + case "Rejected": + default: + return null; + } + }); +} + export function MyEventView({ backTo = null, full = false }) { + const revalidator = useRevalidator(); + const dispatch = useDispatch(); const { eventId } = useParams(); const entity = useLoaderData(); + const [, next] = useApiFetch(["My", "Events", eventId, "Statuses/Next"], { method: "GET" }, []); + + function handleStatusChange(newStatus) { + apiFetch(["my", "events", eventId, "statuses"], { + method: "POST", + body: JSON.stringify({ from: entity.currentStatus.identifier, to: newStatus.identifier }), + }) + .then(() => { + entity.currentStatus = newStatus; + revalidator.revalidate(); + }) + .catch((err) => { + if (err !== null && err.message === "401") { + dispatch(disconnect()); + } else { + console.error(err); + dispatch(addError("Can't send this request", err.from)); + } + }); + } return ( - - -
}> - Detail -
-
}> - Statuses -
-
-
+ <> + } + > + +
}> + Detail +
+
}> + Statuses +
+
+
+ + ); }