/** * Booking view: the travel agency that made this booking (Pro "Travel * Agencies" module). Shown only while the module is enabled; an editor can * link the booking to an agency or remove the link. * * @package Yatra */ import React, { useEffect, useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Briefcase } from "lucide-react"; import { __ } from "../../lib/i18n"; import { apiClient } from "../../lib/api-client"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; import { Button } from "../ui/button"; import { Select } from "../ui/select"; import { useToast } from "../ui/toast"; interface Props { bookingId: number; canEdit: boolean; } export const TravelAgencyPanel: React.FC = ({ bookingId, canEdit }) => { const { showToast } = useToast(); const queryClient = useQueryClient(); const [editing, setEditing] = useState(false); const [choice, setChoice] = useState("0"); const [saving, setSaving] = useState(false); const { data, isLoading, isError } = useQuery({ queryKey: ["booking-travel-agency", bookingId], queryFn: async () => await apiClient.get(`/bookings/${bookingId}/travel-agency`), enabled: bookingId > 0, }); const agency = (data as any)?.data?.agency ?? null; const agencies: { id: number; name: string }[] = (data as any)?.data?.agencies ?? []; useEffect(() => { setChoice(agency ? String(agency.id) : "0"); }, [agency?.id]); if (isLoading || (!agency && !canEdit && !isError)) { return null; } // Never say "not booked through an agency" when we simply couldn't ask. if (isError) { return ( {__("Travel Agency", "yatra")} {__("The travel agency for this booking could not be loaded.", "yatra")} ); } const save = async () => { setSaving(true); try { await apiClient.post(`/bookings/${bookingId}/travel-agency`, { agency_id: Number(choice) }); showToast(__("Saved.", "yatra"), "success"); setEditing(false); queryClient.invalidateQueries({ queryKey: ["booking-travel-agency", bookingId] }); queryClient.invalidateQueries({ queryKey: ["booking-activity", bookingId] }); } catch (e: any) { showToast(e?.message || __("Could not be saved.", "yatra"), "error"); } finally { setSaving(false); } }; return ( {__("Travel Agency", "yatra")} {!editing ? ( <> {agency ? (
{agency.name}
{agency.contact_name ? `${agency.contact_name} · ` : ""} {agency.email}
{agency.billing_email && agency.billing_email !== agency.email ? (
{__("Billing:", "yatra")} {agency.billing_email}
) : null}
{__("The contact details on this booking are the guest's.", "yatra")}
) : (
{__("Not booked through an agency.", "yatra")}
)} {canEdit && agencies.length > 0 ? ( ) : null} ) : (
)}
); }; export default TravelAgencyPanel;