diff --git a/lib/getServerSideProps.ts b/lib/getServerSideProps.ts new file mode 100644 index 0000000..92b019b --- /dev/null +++ b/lib/getServerSideProps.ts @@ -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 }, + } +} diff --git a/pages/admin/booking/[uuid]/bill.tsx b/pages/admin/booking/[uuid]/bill.tsx index f10062d..8b1fbe7 100644 --- a/pages/admin/booking/[uuid]/bill.tsx +++ b/pages/admin/booking/[uuid]/bill.tsx @@ -7,16 +7,17 @@ import Select from '../../../../components/select' import { AdditionalCost, BillDocument } from '../../../../db/bill' import { BookingDocument } from '../../../../db/booking' 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 { getBillTotal } from '../../../../helpers/bill' import { getBookingStatus } from '../../../../helpers/booking' import authenticate from '../../../../lib/authenticate' import withSession from '../../../../lib/session' +import { getServerSideBooking } from '../../../../lib/getServerSideProps' export const getServerSideProps: GetServerSideProps = withSession( - async ({ req, res, params }) => { - const { uuid: uuids } = params + async (context) => { + const { req, res, params } = context const authenticatedUser = authenticate(req, res) if (!authenticatedUser) { @@ -28,22 +29,11 @@ export const getServerSideProps: GetServerSideProps = withSession( req.session.set('user', authenticatedUser) 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() - await booking.populate('booker').populate('bill').execPopulate() - - // TODO: hack, not sure why _id is not serilizable - const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON())) + const result = await getServerSideBooking(context) return { - props: { booking: bookingJSON, milageMax }, + ...result, + props: { ...result.props, milageMax }, } } ) diff --git a/pages/booking/[uuid]/index.tsx b/pages/booking/[uuid]/index.tsx index 55725b2..c1533cb 100644 --- a/pages/booking/[uuid]/index.tsx +++ b/pages/booking/[uuid]/index.tsx @@ -1,34 +1,13 @@ -import { GetServerSideProps } from 'next' import React, { useEffect, useState } from 'react' import Footer from '../../../components/footer' import Header from '../../../components/header' +import { getServerSideBooking } from '../../../lib/getServerSideProps' import { BookingDocument } from '../../../db/booking' import { BOOKING_STATUS } from '../../../db/enums' -import { getBookingByUUID } from '../../../db/index' import { dateFormatFrontend } from '../../../helpers/date' import { getBookingStatus } from '../../../helpers/booking' -export const getServerSideProps: GetServerSideProps = async (context) => { - 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 }, - } -} +export const getServerSideProps = getServerSideBooking async function cancelBooking(booking: BookingDocument) { const response = await fetch(`/api/booking/${booking.uuid}`, {