Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

3.2 KB
Raw Blame
import { useMemo, useState } from 'react';

import type { Day, Place } from '../../types';
import { toggledTraveler } from './bookingFormModel';
import {
  type CheckInEndField,
  endTimeBeforeStartTime,
  isEndBeforeStart,
  type ReservationFields,
  reservationSaveData,
  type ReservationSaveOptions,
  startParts,
  tripDateBounds,
  withHotelEnd,
  withHotelPlace,
  withHotelStart,
  withLinkedPlace,
  withLinkedStopDay,
  withStartDate,
  withStartTime,
} from './reservationFormModel';
import { useStayPin } from './useStayPin';

/**
 * The booking form behind the desktop ReservationModal and the phone's booking
 * sheet: the fields, the travellers and the files picked before saving, the date
 * rules and the place and hotel picks. Each shell decides when to fill it and what
 * to do around a save.
 */
export function useReservationForm<F extends ReservationFields & CheckInEndField>(
  emptyForm: F,
  days: Day[],
  places: Pick<Place, 'id' | 'name' | 'address'>[]
) {
  const [form, setForm] = useState<F>(emptyForm);
  const [isSaving, setIsSaving] = useState(false);
  const [pendingFiles, setPendingFiles] = useState<File[]>([]);
  // Travelers assigned to this booking (#1517), seeded on open and persisted after the save resolves.
  const [travelerIds, setTravelerIds] = useState<Set<number>>(new Set());

  const set = (field: keyof F, value: string | number) => setForm((prev) => ({ ...prev, [field]: value }));
  const toggleTraveler = (id: number) => setTravelerIds((prev) => toggledTraveler(prev, id));
  // Where an imported stay will land on the map, looked up while it is reviewed.
  const stayPin = useStayPin();

  // Restrict non-hotel booking dates to the trip's span (#1662). Hotels already
  // constrain to trip days via their day dropdowns.
  const dateBounds = useMemo(() => tripDateBounds(days || []), [days]);
  const start = startParts(form.reservation_time);

  return {
    form,
    setForm,
    set,
    isSaving,
    setIsSaving,
    pendingFiles,
    setPendingFiles,
    travelerIds,
    setTravelerIds,
    toggleTraveler,
    isEndBeforeStart: isEndBeforeStart(form),
    endTimeBeforeStartTime: endTimeBeforeStartTime(form),
    dateBounds,
    startDate: start.date,
    startTime: start.time,
    setStartDate: (date: string | null | undefined) =>
      set('reservation_time', withStartDate(form.reservation_time, date)),
    setStartTime: (time: string, fallbackDate: string) =>
      set('reservation_time', withStartTime(form.reservation_time, time, fallbackDate)),
    /** An undated booking takes the day of the stop it was just linked to. */
    takeStopDay: (dayDate: string) => setForm((prev) => withLinkedStopDay(prev, dayDate)),
    pickPlace: (value: string | number) => setForm((prev) => withLinkedPlace(prev, value, places)),
    pickHotelPlace: (value: string | number) => setForm((prev) => withHotelPlace(prev, value, places)),
    pickHotelStart: (value: string | number) => setForm((prev) => withHotelStart(prev, value, days)),
    pickHotelEnd: (value: string | number) => setForm((prev) => withHotelEnd(prev, value, days)),
    stayPin,
    saveData: (opts: ReservationSaveOptions) => reservationSaveData(form, { ...opts, venuePin: stayPin.match }),
  };
}