import React, { useEffect, useState } from 'react' import { getBookingByUUIDAsJSON } from '../../db/index' import Header from '../../components/header' import Footer from '../../components/footer' import { dateFormatFrontend } from '../../helpers/date' import { BOOKING_STATUS } from '../../db/bookingStatus' export async function getServerSideProps(context) { const { uuid } = context.params const booking = await getBookingByUUIDAsJSON(uuid) // TODO: hack, not sure why _id is not serilizable const bookingJSON = JSON.parse(JSON.stringify(booking)) return { props: { booking: bookingJSON }, } } function getBookingStatus(booking) { switch (booking.status) { case BOOKING_STATUS.REQUESTED: return 'In Bearbeitung' case BOOKING_STATUS.CONFIRMED: return 'BestÃĪtigt' case BOOKING_STATUS.REJECTED: return 'Abgewiesen' case BOOKING_STATUS.CANCELED: return 'Storniert' default: return 'Unbekannt - bitte kontaktieren Sie uns!' } } async function cancelBooking(booking) { const response = await fetch(`/api/booking/${booking.uuid}`, { method: 'PATCH', mode: 'cors', cache: 'no-cache', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', }, referrerPolicy: 'no-referrer', body: JSON.stringify({ status: BOOKING_STATUS.CANCELED }), }) return response.json() } export default function Booking({ booking: bookingProp }) { const [booking, setBooking] = useState(bookingProp) // 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 { const updatedBooking = await cancelBooking(booking) setBooking(updatedBooking) } catch (e) { alert( 'Die Buchung konnte nicht storniert werden. Kontaktieren Sie uns bitt per E-Mail!' ) } } return (