mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
118 lines
3.6 KiB
TypeScript
118 lines
3.6 KiB
TypeScript
import React, { useEffect, useState } from 'react'
|
|
import { GetServerSideProps } from 'next'
|
|
import Footer from '../../../../components/footer'
|
|
import Header from '../../../../components/header'
|
|
import Calendar from '../../../../components/calendar'
|
|
import withSession, { isAdminSession } from '../../../../lib/session'
|
|
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
|
import { BookingDocument } from '../../../../db/booking'
|
|
import { getBookingStatus } from '../../../../helpers/booking'
|
|
import { dateFormatFrontend } from '../../../../helpers/date'
|
|
import { BOOKING_STATUS } from '../../../../db/enums'
|
|
|
|
export const getServerSideProps: GetServerSideProps = withSession(
|
|
async (context) => {
|
|
const { req, res } = context
|
|
|
|
console.error('here')
|
|
const adminUser = isAdminSession(req, res)
|
|
|
|
if (!adminUser) {
|
|
return { props: {} }
|
|
}
|
|
|
|
const result = await getServerSideBooking(context)
|
|
return {
|
|
...result,
|
|
// TODO: have a closer look at this type issue. Seems like a bug
|
|
// @ts-ignore
|
|
props: { ...result.props, user: adminUser },
|
|
}
|
|
}
|
|
)
|
|
|
|
async function patchBooking(uuid: string, bookingData: any) {
|
|
const response = await fetch(`/api/admin/booking/${uuid}`, {
|
|
method: 'PATCH',
|
|
mode: 'cors',
|
|
cache: 'no-cache',
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
referrerPolicy: 'no-referrer',
|
|
body: JSON.stringify({ ...bookingData }),
|
|
})
|
|
return response.json()
|
|
}
|
|
|
|
export default function ShowBookingAdmin({
|
|
booking: bookingProp,
|
|
}: {
|
|
booking: BookingDocument
|
|
}) {
|
|
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 onStoreBooking = async (confirmed: boolean) => {
|
|
try {
|
|
setStoringBookingError(null)
|
|
setStoringBooking(true)
|
|
const updatedBooking = await patchBooking(booking.uuid, {
|
|
status: confirmed ? BOOKING_STATUS.CONFIRMED : BOOKING_STATUS.REJECTED,
|
|
})
|
|
setBooking(updatedBooking)
|
|
} catch (error) {
|
|
setStoringBookingError('Buchung konnte nicht gespeichert werden.')
|
|
console.error('Failed to store booking', error)
|
|
}
|
|
setStoringBooking(false)
|
|
}
|
|
|
|
return (
|
|
<div className="mx-3 flex flex-col min-h-screen">
|
|
<Header />
|
|
<main className="flex-grow">
|
|
<h2 className="text-3xl">Buchung {booking.uuid}</h2>
|
|
<Calendar />
|
|
<div>
|
|
<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>
|
|
{storingBookingError && (
|
|
<div className="error-message flex-grow">{storingBookingError}</div>
|
|
)}
|
|
<div className="my-6">
|
|
<button
|
|
onClick={() => onStoreBooking(true)}
|
|
className="btn btn-blue"
|
|
disabled={storingBooking}
|
|
>
|
|
Buchung Bestätigen
|
|
</button>
|
|
<button
|
|
onClick={() => onStoreBooking(false)}
|
|
className="btn btn-red"
|
|
disabled={storingBooking}
|
|
>
|
|
Buchung Abweisen
|
|
</button>
|
|
</div>
|
|
</main>
|
|
|
|
<Footer />
|
|
</div>
|
|
)
|
|
}
|