Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow events to be deleted on edit view #822

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading