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.

1.8 KB
Raw Blame
import type { Reservation } from '../../../types'
import { useTranslation } from '../../../i18n'
import { useTripStore } from '../../../store/tripStore'
import { useToast } from '../../shared/Toast'
import ConfirmDialog from '../../shared/ConfirmDialog'
import { useReservationDelete } from './useReservationDelete'
import { deleteBodyKey } from './bookingsModel'

/**
 * The two things a booking can have done to it without its editor: switching
 * its status, and deleting it after a question. The question is the returned
 * element, to be rendered once next to whatever asks it.
 *
 * `beforeDelete` runs once the question is answered and before the delete goes
 * out, so a detail showing that booking closes first.
 */
export function useBookingActions(tripId: number, onDelete: (id: number) => unknown, beforeDelete?: (r: Reservation) => void) {
  const { t } = useTranslation()
  const toast = useToast()
  const toggleReservationStatus = useTripStore(s => s.toggleReservationStatus)
  const budgetItems = useTripStore(s => s.budgetItems)
  const { pendingDelete, requestDelete, cancelDelete, confirmDelete } =
    useReservationDelete(onDelete, () => toast.error(t('reservations.toast.deleteError')), beforeDelete)

  const toggleStatus = (r: Reservation) => {
    toggleReservationStatus(tripId, r.id).catch(() => toast.error(t('reservations.toast.updateError')))
  }

  const confirmDialog = (
    <ConfirmDialog
      isOpen={!!pendingDelete}
      onClose={cancelDelete}
      onConfirm={confirmDelete}
      title={t('reservations.confirm.deleteTitle')}
      message={t(deleteBodyKey(pendingDelete?.id, budgetItems), { name: pendingDelete?.title ?? '' })}
      confirmLabel={t('common.delete')}
      cancelLabel={t('common.cancel')}
    />
  )

  return { toggleStatus, requestDelete, pendingDelete, confirmDialog }
}