Skip to content

Commit

Permalink
Merge pull request #26 from Ayon95/refactor_use_delete_booking_hook
Browse files Browse the repository at this point in the history
Refactor useDeleteBooking hook
  • Loading branch information
Ayon95 authored May 3, 2024
2 parents b1ed214 + 132dff6 commit 34517db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/features/bookings/BookingDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useNavigate } from 'react-router-dom';
import styled from 'styled-components';
import { HiArrowLeft, HiOutlineHomeModern } from 'react-icons/hi2';

Expand Down Expand Up @@ -34,9 +35,20 @@ function BookingDetails({ booking }: BookingProps) {
closeModal: closeConfirmDeleteModal,
} = useModal();

const navigate = useNavigate();
const deleteBookingMutation = useDeleteBooking();
const checkOutBookingMutation = useCheckOutBooking();

function handleDeleteBooking() {
deleteBookingMutation.mutate(booking.id, {
onSuccess: () => {
setTimeout(() => {
navigate('/bookings', { replace: true });
}, 1000);
},
});
}

return (
<>
<Container>
Expand Down Expand Up @@ -130,7 +142,7 @@ function BookingDetails({ booking }: BookingProps) {
{shouldShowConfirmDeleteModal && (
<ConfirmDeleteModal
resourceName={`booking ${booking.id}`}
onConfirmDelete={() => deleteBookingMutation.mutate(booking.id)}
onConfirmDelete={handleDeleteBooking}
onCloseModal={closeConfirmDeleteModal}
isDeleting={deleteBookingMutation.isLoading}
/>
Expand Down
8 changes: 0 additions & 8 deletions src/features/bookings/hooks/useDeleteBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ import toast from 'react-hot-toast';

import { deleteBooking } from '@/services/apiBookings';
import { BOOKINGS_QUERY_KEY } from '@/utils/constants';
import { useLocation, useNavigate } from 'react-router-dom';

export function useDeleteBooking() {
const location = useLocation();
const navigate = useNavigate();
const queryClient = useQueryClient();

return useMutation({
mutationFn: deleteBooking,
onSuccess: () => {
toast.success('Booking deleted successfully!');
queryClient.invalidateQueries({ queryKey: [BOOKINGS_QUERY_KEY] });
if (location.pathname.includes('bookings/')) {
setTimeout(() => {
navigate('/bookings', { replace: true });
}, 1000);
}
},
onError: (error: Error) => toast.error(error.message),
});
Expand Down

0 comments on commit 34517db

Please sign in to comment.