From 00e35e6d96765a5c175e7b387589f03e2aefb637 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Tue, 25 Aug 2020 23:57:21 +0200 Subject: [PATCH] show booking details and cancel option --- pages/booking/[uuid].js | 113 +++++++++++++++++++++++++++++----------- styles/index.css | 8 +++ 2 files changed, 91 insertions(+), 30 deletions(-) diff --git a/pages/booking/[uuid].js b/pages/booking/[uuid].js index 5ed5023..87c2e6a 100644 --- a/pages/booking/[uuid].js +++ b/pages/booking/[uuid].js @@ -1,43 +1,96 @@ -import React, { useContext } from 'react' +import React, { useEffect, useState } from 'react' +import { getBookingByUUIDAsJSON } from '../../db/index' + +import Header from '../../components/header' import Footer from '../../components/footer' -// TODO: load booking somehow if we navigate to booking -// TODO: when storing data take loaded booking instead of fromData -// TODO: show state of booking and minimal data +import { dateFormatFrontend } from '../../helpers/date' -export default function Booking() { - const { state, storeData } = useContext(WizardContext) - const { dataStored, dataStoredLoaded } = state +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 Anfrage' + 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 (
+
-

Pfadi Bussle Buchung

- -

Vielen Dank für die Buchung!

-

Nach Prüfung bestätigen wir die Buchung bald per E-Mail!

- {!dataStoredLoaded && typeof dataStored !== 'boolean' && ( - <> -

- Sollen die eingegebenen Daten in Deinem Browser{' '} - für die nächste Buchung gespeichert werden? -

- - - +
)} - {dataStored === true && ( -

Ok, die Daten wurden für die nächste Buchung gespeichert.

- )} -
{JSON.stringify(state.booking, null, 4)}