import { useState } from "react" import { useIntl } from "react-intl" import { trpc } from "@/lib/trpc/client" import { toast } from "@/components/TempDesignSystem/Toasts" import useLang from "@/hooks/useLang" import type { CancelStayProps } from ".." export default function useCancelStay({ booking, setBookingStatus, handleCloseModal, handleBackToManageStay, }: Omit) { const intl = useIntl() const lang = useLang() const [currentStep, setCurrentStep] = useState(1) const [isLoading, setIsLoading] = useState(false) const cancelStay = trpc.booking.cancel.useMutation({ onMutate: () => setIsLoading(true), onSuccess: (result) => { if (!result) { toast.error( intl.formatMessage({ id: "Something went wrong. Please try again later.", }) ) return } setBookingStatus() toast.success( intl.formatMessage( { id: "Your stay was cancelled. Cancellation cost: 0 {currency}. We’re sorry to see that the plans didn’t work out", }, { currency: booking.currencyCode } ) ) }, onError: () => { toast.error( intl.formatMessage({ id: "Something went wrong. Please try again later.", }) ) }, onSettled: () => { handleCloseModal() }, }) function handleCancelStay() { if (!booking.confirmationNumber) { toast.error( intl.formatMessage({ id: "Something went wrong. Please try again later.", }) ) return } cancelStay.mutate({ confirmationNumber: booking.confirmationNumber, language: lang, }) } function handleCloseCancelStay() { setCurrentStep(1) setIsLoading(false) handleBackToManageStay() } return { currentStep, isLoading, handleCancelStay, handleCloseCancelStay, handleBack: () => setCurrentStep(1), handleForward: () => setCurrentStep(2), } }