Skip to content

Commit

Permalink
get deletion working
Browse files Browse the repository at this point in the history
  • Loading branch information
choden-dev committed Nov 3, 2024
1 parent a9c7760 commit 55bf0cc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -74,7 +79,8 @@ const AdminEventViewContent = ({
fetchMoreEvents,
handleEditEvent,
eventPreviousData,
fetchEventToEdit
fetchEventToEdit,
handleDeleteEvent
}: {
mode: EventViewModes
setMode: (mode: EventViewModes) => void
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -164,7 +174,8 @@ const AdminEventView = ({
fetchMoreEvents,
eventPreviousData,
handleEditEvent,
fetchEventToEdit
fetchEventToEdit,
handleDeleteEvent
}: IAdminEventView) => {
const [mode, setMode] = useState<EventViewModes>("view-all-events")

Expand Down Expand Up @@ -193,10 +204,10 @@ const AdminEventView = ({
</Button>
</div>
</span>
{/** TODO: pass in delete handler */}
<AdminEventViewContent
setMode={setMode}
mode={mode}
handleDeleteEvent={handleDeleteEvent}
fetchEventToEdit={fetchEventToEdit}
handleEditEvent={handleEditEvent}
handlePostEvent={handlePostEvent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {
useCreateEventMutation,
useDeleteEventMutation,
useEditEventMutation
} from "@/services/Admin/AdminMutations"
import AdminEventView from "./AdminEventView"
Expand All @@ -25,6 +26,8 @@ const WrappedAdminEventView = () => {

const { mutateAsync: editEvent } = useEditEventMutation()

const { mutateAsync: deleteEvent } = useDeleteEventMutation()

const [eventPreviousData, setEventPreviousData] = useState<
Event | undefined
>()
Expand Down Expand Up @@ -64,6 +67,7 @@ const WrappedAdminEventView = () => {
handleEditEvent={async (eventId, newData) => {
await editEvent({ eventId, newData })
}}
handleDeleteEvent={deleteEvent}
eventPreviousData={eventPreviousData}
rawEvents={rawEvents || []}
hasMoreEvents={hasNextPage}
Expand Down
27 changes: 17 additions & 10 deletions client/src/services/Admin/AdminMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand All @@ -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
})
}
13 changes: 13 additions & 0 deletions client/src/services/Admin/AdminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 55bf0cc

Please sign in to comment.