extract getServerSideBooking

This commit is contained in:
Thomas Ruoff
2020-10-28 23:35:31 +01:00
parent e1ebc86ca1
commit 84026c2576
3 changed files with 34 additions and 40 deletions

25
lib/getServerSideProps.ts Normal file
View File

@@ -0,0 +1,25 @@
import { GetServerSideProps } from 'next'
import { getBookingByUUID } from '../db/index'
export const getServerSideBooking: GetServerSideProps = async (context) => {
const {
res,
params: { uuid: uuids },
} = context
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
const booking = await getBookingByUUID(uuid)
if (!booking) {
res.statusCode = 404
res.end()
return { props: {} }
}
await booking.populate('booker').execPopulate()
// TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON()))
return {
props: { booking: bookingJSON },
}
}

View File

@@ -7,16 +7,17 @@ import Select from '../../../../components/select'
import { AdditionalCost, BillDocument } from '../../../../db/bill' import { AdditionalCost, BillDocument } from '../../../../db/bill'
import { BookingDocument } from '../../../../db/booking' import { BookingDocument } from '../../../../db/booking'
import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums' import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums'
import { getBookingByUUID, getMilageMax } from '../../../../db/index' import { getMilageMax } from '../../../../db/index'
import { dateFormatFrontend } from '../../../../helpers/date' import { dateFormatFrontend } from '../../../../helpers/date'
import { getBillTotal } from '../../../../helpers/bill' import { getBillTotal } from '../../../../helpers/bill'
import { getBookingStatus } from '../../../../helpers/booking' import { getBookingStatus } from '../../../../helpers/booking'
import authenticate from '../../../../lib/authenticate' import authenticate from '../../../../lib/authenticate'
import withSession from '../../../../lib/session' import withSession from '../../../../lib/session'
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
export const getServerSideProps: GetServerSideProps = withSession( export const getServerSideProps: GetServerSideProps = withSession(
async ({ req, res, params }) => { async (context) => {
const { uuid: uuids } = params const { req, res, params } = context
const authenticatedUser = authenticate(req, res) const authenticatedUser = authenticate(req, res)
if (!authenticatedUser) { if (!authenticatedUser) {
@@ -28,22 +29,11 @@ export const getServerSideProps: GetServerSideProps = withSession(
req.session.set('user', authenticatedUser) req.session.set('user', authenticatedUser)
await req.session.save() await req.session.save()
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
const booking = await getBookingByUUID(uuid)
if (!booking) {
res.statusCode = 404
res.end()
return { props: {} }
}
const milageMax = await getMilageMax() const milageMax = await getMilageMax()
await booking.populate('booker').populate('bill').execPopulate() const result = await getServerSideBooking(context)
// TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON()))
return { return {
props: { booking: bookingJSON, milageMax }, ...result,
props: { ...result.props, milageMax },
} }
} }
) )

View File

@@ -1,34 +1,13 @@
import { GetServerSideProps } from 'next'
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 { getServerSideBooking } from '../../../lib/getServerSideProps'
import { BookingDocument } from '../../../db/booking' import { BookingDocument } from '../../../db/booking'
import { BOOKING_STATUS } from '../../../db/enums' import { BOOKING_STATUS } from '../../../db/enums'
import { getBookingByUUID } from '../../../db/index'
import { dateFormatFrontend } from '../../../helpers/date' import { dateFormatFrontend } from '../../../helpers/date'
import { getBookingStatus } from '../../../helpers/booking' import { getBookingStatus } from '../../../helpers/booking'
export const getServerSideProps: GetServerSideProps = async (context) => { export const getServerSideProps = getServerSideBooking
const {
res,
params: { uuid: uuids },
} = context
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
const booking = await getBookingByUUID(uuid)
await booking.populate('booker').execPopulate()
if (!booking) {
res.statusCode = 404
res.end()
return { props: {} }
}
// TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON()))
return {
props: { booking: bookingJSON },
}
}
async function cancelBooking(booking: BookingDocument) { async function cancelBooking(booking: BookingDocument) {
const response = await fetch(`/api/booking/${booking.uuid}`, { const response = await fetch(`/api/booking/${booking.uuid}`, {