mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 23:17:12 +01:00
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import React, { useEffect, useState } from 'react'
|
|
import { getServerSideBooking } from '../../../lib/getServerSideProps'
|
|
import { Booking, BookingStatus } from '@prisma/client'
|
|
import { dateFormatFrontend } from '../../../helpers/date'
|
|
import { log } from '../../../helpers/log'
|
|
import { getBookingStatus, cancelBooking } from '../../../helpers/booking'
|
|
|
|
export const getServerSideProps = getServerSideBooking
|
|
|
|
export default function ShowBooking({
|
|
booking: bookingProp,
|
|
}: {
|
|
booking: Booking
|
|
}) {
|
|
const [booking, setBooking] = useState(bookingProp)
|
|
const [storingBooking, setStoringBooking] = useState(false)
|
|
const [storingBookingError, setStoringBookingError] = useState(null)
|
|
|
|
// in case the props change, update the internal state
|
|
useEffect(() => setBooking(bookingProp), [bookingProp])
|
|
|
|
const onCancelBooking = async () => {
|
|
if (!confirm('Soll die Buchung wirklich storniert werden?')) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
setStoringBookingError(null)
|
|
setStoringBooking(true)
|
|
const updatedBooking = await cancelBooking(booking.uuid)
|
|
setBooking(updatedBooking)
|
|
} catch (error) {
|
|
setStoringBookingError(
|
|
'Buchung konnte nicht storniert werden. Schreiben Sie uns eine E-Mail!'
|
|
)
|
|
log.error('Failed to store booking', error)
|
|
}
|
|
setStoringBooking(false)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div>
|
|
<strong>Buchungsstatus:</strong> {getBookingStatus(booking.status)}
|
|
</div>
|
|
<div>
|
|
<strong>Buchungszeitraum:</strong> {dateFormatFrontend(new Date(booking.startDate))}-{dateFormatFrontend(new Date(booking.endDate))}
|
|
</div>
|
|
{storingBookingError && (
|
|
<div className="error-message flex-grow">{storingBookingError}</div>
|
|
)}
|
|
{([BookingStatus.CONFIRMED, BookingStatus.REQUESTED] as BookingStatus[]).includes(
|
|
booking.status
|
|
) && (
|
|
<div className="my-6">
|
|
<button
|
|
onClick={onCancelBooking}
|
|
className="btn btn-red"
|
|
disabled={storingBooking}
|
|
>
|
|
Buchung Stornieren
|
|
</button>
|
|
</div>
|
|
)}
|
|
</>
|
|
)
|
|
}
|