From 55bf0cc311108cbdcbe9189d8e90854eed053f6d Mon Sep 17 00:00:00 2001 From: bcho892 Date: Sun, 3 Nov 2024 16:52:08 +1300 Subject: [PATCH] get deletion working --- .../Admin/AdminEventView/AdminEventView.tsx | 17 +++++++++--- .../AdminEventView/WrappedAdminEventView.tsx | 4 +++ client/src/services/Admin/AdminMutations.ts | 27 ++++++++++++------- client/src/services/Admin/AdminService.ts | 13 +++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/client/src/components/composite/Admin/AdminEventView/AdminEventView.tsx b/client/src/components/composite/Admin/AdminEventView/AdminEventView.tsx index b4f0bc05..62e18033 100644 --- a/client/src/components/composite/Admin/AdminEventView/AdminEventView.tsx +++ b/client/src/components/composite/Admin/AdminEventView/AdminEventView.tsx @@ -56,6 +56,11 @@ interface IAdminEventView { */ handleEditEvent?: (eventId: string, newData: EditEventBody) => void + /** + * Callback for when an event is deleted by admin + */ + handleDeleteEvent?: (eventId: string) => void + /** * Obtains the latest data for an event to edit, if `undefined` is passed * in then it means that no event should be edited @@ -74,7 +79,8 @@ const AdminEventViewContent = ({ fetchMoreEvents, handleEditEvent, eventPreviousData, - fetchEventToEdit + fetchEventToEdit, + handleDeleteEvent }: { mode: EventViewModes setMode: (mode: EventViewModes) => void @@ -127,6 +133,10 @@ const AdminEventViewContent = ({ return await generateImageLink(image) }} defaultData={eventPreviousData} + handleDeleteEvent={async () => { + await handleDeleteEvent?.(editedEventId) + setMode("view-all-events") + }} handlePostEvent={async (data) => { await handleEditEvent?.(editedEventId, data.data) setMode("view-all-events") @@ -164,7 +174,8 @@ const AdminEventView = ({ fetchMoreEvents, eventPreviousData, handleEditEvent, - fetchEventToEdit + fetchEventToEdit, + handleDeleteEvent }: IAdminEventView) => { const [mode, setMode] = useState("view-all-events") @@ -193,10 +204,10 @@ const AdminEventView = ({ - {/** TODO: pass in delete handler */} { const { mutateAsync: editEvent } = useEditEventMutation() + const { mutateAsync: deleteEvent } = useDeleteEventMutation() + const [eventPreviousData, setEventPreviousData] = useState< Event | undefined >() @@ -64,6 +67,7 @@ const WrappedAdminEventView = () => { handleEditEvent={async (eventId, newData) => { await editEvent({ eventId, newData }) }} + handleDeleteEvent={deleteEvent} eventPreviousData={eventPreviousData} rawEvents={rawEvents || []} hasMoreEvents={hasNextPage} diff --git a/client/src/services/Admin/AdminMutations.ts b/client/src/services/Admin/AdminMutations.ts index 6905ac19..e8259af1 100644 --- a/client/src/services/Admin/AdminMutations.ts +++ b/client/src/services/Admin/AdminMutations.ts @@ -172,16 +172,18 @@ export function useDeleteBookingMutation() { }) } +const invalidateEventsQuery = () => { + queryClient.invalidateQueries({ + queryKey: [ALL_EVENTS_QUERY_KEY] + }) +} + export function useCreateEventMutation() { return useMutation({ mutationKey: ["create-booking"], retry: false, mutationFn: AdminService.createEvent, - onSuccess: () => { - queryClient.invalidateQueries({ - queryKey: [ALL_EVENTS_QUERY_KEY] - }) - } + onSuccess: invalidateEventsQuery }) } @@ -190,10 +192,15 @@ export function useEditEventMutation() { mutationKey: ["edit-event"], retry: false, mutationFn: AdminService.editEvent, - onSuccess: () => { - queryClient.invalidateQueries({ - queryKey: [ALL_EVENTS_QUERY_KEY] - }) - } + onSuccess: invalidateEventsQuery + }) +} + +export function useDeleteEventMutation() { + return useMutation({ + mutationKey: ["delete-event"], + retry: false, + mutationFn: AdminService.deleteEvent, + onSuccess: invalidateEventsQuery }) } diff --git a/client/src/services/Admin/AdminService.ts b/client/src/services/Admin/AdminService.ts index 5f13aefb..48ff6321 100644 --- a/client/src/services/Admin/AdminService.ts +++ b/client/src/services/Admin/AdminService.ts @@ -237,6 +237,19 @@ const AdminService = { } return data?.data + }, + deleteEvent: async function (eventId: string) { + const { response } = await fetchClient.DELETE("/admin/events/{id}", { + params: { + path: { + id: eventId + } + } + }) + + if (!response.ok) { + throw new Error(`Failed to delete event with id ${eventId}`) + } } } as const