add confirm and reject buttons to adming/booking

This commit is contained in:
Thomas Ruoff
2020-11-02 23:25:00 +01:00
parent 8c252cca2b
commit cb9932240d
2 changed files with 30 additions and 11 deletions

View File

@@ -1,15 +1,16 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import Footer from '../../../../components/footer' import Footer from '../../../../components/footer'
import Header from '../../../../components/header' import Header from '../../../../components/header'
import Input from '../../../../components/input'
import { getServerSideBooking } from '../../../../lib/getServerSideProps' import { getServerSideBooking } from '../../../../lib/getServerSideProps'
import { BookingDocument } from '../../../../db/booking' import { BookingDocument } from '../../../../db/booking'
import { getBookingStatus } from '../../../../helpers/booking' import { getBookingStatus } from '../../../../helpers/booking'
import { dateFormatFrontend } from '../../../../helpers/date'
import { BOOKING_STATUS } from '../../../../db/enums'
export const getServerSideProps = getServerSideBooking export const getServerSideProps = getServerSideBooking
async function storeBooking(booking: BookingDocument) { async function patchBooking(uuid: string, bookingData: any) {
const response = await fetch(`/api/admin/booking/${booking.uuid}`, { const response = await fetch(`/api/admin/booking/${uuid}`, {
method: 'PATCH', method: 'PATCH',
mode: 'cors', mode: 'cors',
cache: 'no-cache', cache: 'no-cache',
@@ -18,7 +19,7 @@ async function storeBooking(booking: BookingDocument) {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
referrerPolicy: 'no-referrer', referrerPolicy: 'no-referrer',
body: JSON.stringify({ ...booking }), body: JSON.stringify({ ...bookingData }),
}) })
return response.json() return response.json()
} }
@@ -35,11 +36,13 @@ export default function ShowBookingAdmin({
// in case the props change, update the internal state // in case the props change, update the internal state
useEffect(() => setBooking(bookingProp), [bookingProp]) useEffect(() => setBooking(bookingProp), [bookingProp])
const onStoreBooking = async () => { const onStoreBooking = async (confirmed: boolean) => {
try { try {
setStoringBookingError(null) setStoringBookingError(null)
setStoringBooking(true) setStoringBooking(true)
const updatedBooking = await storeBooking(booking) const updatedBooking = await patchBooking(booking.uuid, {
status: confirmed ? BOOKING_STATUS.CONFIRMED : BOOKING_STATUS.REJECTED,
})
setBooking(updatedBooking) setBooking(updatedBooking)
} catch (error) { } catch (error) {
setStoringBookingError('Buchung konnte nicht gespeichert werden.') setStoringBookingError('Buchung konnte nicht gespeichert werden.')
@@ -54,20 +57,33 @@ export default function ShowBookingAdmin({
<main className="flex-grow"> <main className="flex-grow">
<h2 className="text-3xl">Buchung {booking.uuid}</h2> <h2 className="text-3xl">Buchung {booking.uuid}</h2>
<div> <div>
<strong>Buchungsstatus:</strong> {getBookingStatus(booking)} <strong>Buchungszeitraum:</strong>{' '}
{dateFormatFrontend(new Date(booking.startDate))} -{' '}
{dateFormatFrontend(new Date(booking.endDate))}
</div>
<div>
<strong>Bucher:</strong> {booking.booker.name}
</div>
<div>
<strong>Buchungsstatus:</strong> {getBookingStatus(booking.status)}
</div> </div>
<Input label="Von" type="date" value={booking.startDate} readOnly />
<Input label="Bis" type="date" value={booking.endDate} readOnly />
{storingBookingError && ( {storingBookingError && (
<div className="error-message flex-grow">{storingBookingError}</div> <div className="error-message flex-grow">{storingBookingError}</div>
)} )}
<div className="my-6"> <div className="my-6">
<button <button
onClick={onStoreBooking} onClick={() => onStoreBooking(true)}
className="btn btn-blue" className="btn btn-blue"
disabled={storingBooking} disabled={storingBooking}
> >
Buchung Speichern Buchung Bestätigen
</button>
<button
onClick={() => onStoreBooking(false)}
className="btn btn-red"
disabled={storingBooking}
>
Buchung Abweisen
</button> </button>
</div> </div>
</main> </main>

View File

@@ -23,6 +23,9 @@ export default withSession(async function bookingHandler(req, res) {
// FIXME: validate all the things // FIXME: validate all the things
booking.set(req.body) booking.set(req.body)
await booking.save() await booking.save()
await booking.populate('booker').execPopulate()
res.status(200).json(booking.toJSON()) res.status(200).json(booking.toJSON())
break break
default: default: