Skip to content

Commit

Permalink
allow admins to delete bookings on front end (#552)
Browse files Browse the repository at this point in the history
* implement deletion

* add loading indicator for deleting
  • Loading branch information
choden-dev authored Jun 29, 2024
1 parent 070ac4c commit 1a76221
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface IAdminBookingView {
* ]
* ```
*/
rowOperations?: TableRowOperation[]
rowOperation?: [TableRowOperation]

/**
* used to fetch the data once the last page of the table has been reached
Expand Down Expand Up @@ -86,7 +86,7 @@ const AsyncWrappedAdminBookingCreationPopup = lazy(
*/
export const AdminBookingView = ({
data,
rowOperations,
rowOperation,
dateRange,
handleDateRangeChange,
isUpdating
Expand Down Expand Up @@ -153,10 +153,10 @@ export const AdminBookingView = ({
</Button>
</span>
</span>
<Table<BookingMemberColumnFormat, "multiple-operations">
<Table<BookingMemberColumnFormat, "single-operation">
data={data || [defaultData]}
operationType="multiple-operations"
rowOperations={rowOperations}
operationType="single-operation"
rowOperations={rowOperation}
// Make sure that this is smaller than the amount we fetch in the `AdminService` for better UX
showPerPage={32}
groupSameRows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DateUtils } from "components/utils/DateUtils"
import { AdminBookingViewContext } from "./AdminBookingViewContext"
import { useContext, useMemo } from "react"
import { Timestamp } from "firebase/firestore"
import { TableRowOperation } from "components/generic/ReusableTable/TableUtils"
import { useDeleteBookingMutation } from "services/Admin/AdminMutations"

/**
* Should be wrapped the `AdminBookingViewProvider`
Expand All @@ -14,7 +16,7 @@ const WrappedAdminBookingView = () => {
handleSelectedDateChange
} = useContext(AdminBookingViewContext)

const { data, isLoading } = useAdminBookingsQuery(
const { data, isLoading: isFetchingUsers } = useAdminBookingsQuery(
Timestamp.fromDate(DateUtils.convertLocalDateToUTCDate(startDate)),
Timestamp.fromDate(DateUtils.convertLocalDateToUTCDate(endDate))
)
Expand All @@ -24,7 +26,7 @@ const WrappedAdminBookingView = () => {
const newData: BookingMemberColumnFormat = {
uid: ""
}
newData.uid = user.uid
newData.uid = user.bookingId
newData.Date = DateUtils.formattedNzDate(
new Date(DateUtils.timestampMilliseconds(date.date))
)
Expand All @@ -44,10 +46,34 @@ const WrappedAdminBookingView = () => {
),
[dataList]
)

const { mutateAsync: deleteBooking, isPending: isDeletingBooking } =
useDeleteBookingMutation()
const rowOperations: [TableRowOperation] = [
{
name: "delete booking",
handler: (bookingId: string) => {
const matchingBooking = sortedData?.find(
(data) => data.uid === bookingId
)
if (
confirm(
`Are you SURE you want to delete the booking for the user ${matchingBooking?.Email} on the date ${matchingBooking?.Date}?
This can NOT be undone!
`
)
) {
deleteBooking(bookingId)
}
}
}
]

return (
<AdminBookingView
isUpdating={isLoading}
isUpdating={isFetchingUsers || isDeletingBooking}
data={sortedData}
rowOperation={rowOperations}
dateRange={{ startDate, endDate }}
handleDateRangeChange={handleSelectedDateChange}
/>
Expand Down
1 change: 1 addition & 0 deletions client/src/components/generic/ReusableTable/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const OperationButton = <
<div className="flex h-full items-center px-2">
<h5
data-testid="single-operation-button"
className="text-red cursor-pointer font-bold"
onClick={() => rowOperations && rowOperations[0]?.handler(uid)}
>
X
Expand Down
34 changes: 32 additions & 2 deletions client/src/models/__generated__/schema.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions client/src/services/Admin/AdminMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,16 @@ export function useAddUserToBookingMutation() {
}
})
}

export function useDeleteBookingMutation() {
return useMutation({
mutationKey: ["delete-booking"],
retry: false,
mutationFn: AdminService.deleteBooking,
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [ALL_BOOKINGS_BETWEEN_RANGE_QUERY]
})
}
})
}
11 changes: 10 additions & 1 deletion client/src/services/Admin/AdminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ const AdminService = {
})
if (!response.ok) throw new Error(`Failed to promote ${uid}`)
},

deleteBooking: async function (id: string) {
const { response } = await fetchClient.POST("/admin/bookings/delete", {
body: {
bookingID: id
}
})
if (!response.ok) {
throw new Error(`Failed to delete booking with id ${id}`)
}
},
getBookingsBetweenDateRange: async function ({
startDate = Timestamp.fromDate(new Date(Date.now())),
endDate = Timestamp.fromDate(new Date(Date.now()))
Expand Down
32 changes: 30 additions & 2 deletions server/src/middleware/__generated__/routes.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 90 additions & 3 deletions server/src/middleware/__generated__/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/src/service-layer/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class AdminController extends Controller {
}

@SuccessResponse("200", "Booking deleted successfuly")
// TODO: Refactor this to be a DELETE request
@Post("/bookings/delete")
public async removeBooking(
@Body() requestBody: DeleteBookingRequest
Expand Down
Loading

0 comments on commit 1a76221

Please sign in to comment.