Add delete event method
This commit is contained in:
@@ -7,6 +7,8 @@ import EventTask from "../../components/eventTask/EventTask.jsx";
|
||||
import Button from "../../components/ui/button/button.jsx";
|
||||
import Modal from "../../components/ui/modal/modal.jsx";
|
||||
import { AuthContext } from "../../contexts/auth/AuthContext.js";
|
||||
import deleteEvent from "../../utils/events/deleteEvent.js";
|
||||
import { useNavigate } from "react-router";
|
||||
|
||||
|
||||
function EventDetail() {
|
||||
@@ -14,6 +16,7 @@ function EventDetail() {
|
||||
const params = useParams();
|
||||
const id = params.id;
|
||||
const { user } = useContext(AuthContext)
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [event, setEvent] = useState(null);
|
||||
@@ -30,6 +33,15 @@ function EventDetail() {
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
const delEvent = async () => {
|
||||
const result = await deleteEvent(id);
|
||||
|
||||
if (result.status === 200 || result.status === 204) {
|
||||
setOpen(false);
|
||||
navigate("/events");
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={styles.allContent}>
|
||||
<div className={`glassCard`}>
|
||||
@@ -59,7 +71,7 @@ function EventDetail() {
|
||||
<div className={styles.modalContent}>
|
||||
<p>Etes-vous sur de vouloir supprimer l'événement {event?.name} ?</p>
|
||||
|
||||
<Button variant={"default"}>Confirmer</Button>
|
||||
<Button variant={"default"} onClick={delEvent}>Confirmer</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import getXSRFToken from "../getXSRF.js";
|
||||
|
||||
export default async function deleteEvent(eventId) {
|
||||
const csrfToken = await getXSRFToken();
|
||||
|
||||
try {
|
||||
const response = await fetch(`${import.meta.env.VITE_API_URL}/api/events/${eventId}`, {
|
||||
method: 'DELETE',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-XSRF-TOKEN': csrfToken,
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: eventId,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 422) {
|
||||
return {
|
||||
status: 422,
|
||||
errors: data.errors
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Erreur HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
return { status: response.status, data };
|
||||
|
||||
} catch (error) {
|
||||
return { status: "error", error };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user