/** * "Need to change or cancel?" on the customer's booking page. * * Sends a request to the operator (POST /customers/my-bookings/{id}/requests). * Nothing about the booking changes until the operator acts on it, which the * copy says plainly so customers don't assume an instant cancellation/refund. */ import React, { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { Inbox, CheckCircle2 } from "lucide-react"; import { __ } from "../../lib/i18n"; import { apiClient } from "../../lib/api-client"; import { API_ENDPOINTS } from "../../lib/api-endpoints"; import { formatDate } from "./utils"; export interface CustomerBookingRequest { id: string; type: "cancel" | "change"; type_label: string; message: string; status: "open" | "resolved"; created_at: string; } interface Props { bookingId: number; requests: CustomerBookingRequest[]; canRequest: boolean; unavailableReason: string; enabled: boolean; } const BookingRequestPanel: React.FC = ({ bookingId, requests, canRequest, unavailableReason, enabled, }) => { const qc = useQueryClient(); const [open, setOpen] = useState(false); const [type, setType] = useState<"change" | "cancel">("change"); const [message, setMessage] = useState(""); const [feedback, setFeedback] = useState<{ ok: boolean; text: string } | null>(null); const send = useMutation({ mutationFn: async () => { const res: any = await apiClient.post( `${API_ENDPOINTS.CUSTOMER_MY_BOOKING(bookingId)}/requests`, { type, message: message.trim() }, ); if (res && res.success === false) throw new Error(res.message); return res; }, onSuccess: (res: any) => { setFeedback({ ok: true, text: res?.message || __("Your request has been sent.", "yatra") }); setOpen(false); setMessage(""); qc.invalidateQueries({ queryKey: ["booking-details"] }); // prefix: the list keys it by the id as it came in (number or string) }, onError: (e: any) => setFeedback({ ok: false, text: e?.response?.data?.message || e?.message || __("Your request could not be sent. Please try again or contact us.", "yatra"), }), }); const list = Array.isArray(requests) ? requests : []; // The operator switched requests off and none were sent before: say nothing. if (!enabled && list.length === 0) return null; const hasOpen = (t: string) => list.some((r) => r.status === "open" && r.type === t); return (

{__("Need to change or cancel?", "yatra")}

{list.length > 0 && ( )} {feedback && (
{feedback.ok && } {feedback.text}
)} {!canRequest ? (

{unavailableReason}

) : hasOpen("change") && hasOpen("cancel") ? (

{__("We have your requests and will reply by email.", "yatra")}

) : !open ? ( <>

{__( "Send us a request and we'll get back to you. Your booking stays as it is until we confirm.", "yatra", )}

{/* A div, like the other account actions: themes restyle