From 4db3eadaeb1660c7bbe4c4bb6a3d2bf7c2436b13 Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Mon, 23 Nov 2020 23:49:13 +0100 Subject: [PATCH] add an admin overview page --- lib/getServerSideProps.ts | 28 +++++++++++- pages/admin/index.tsx | 91 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 pages/admin/index.tsx diff --git a/lib/getServerSideProps.ts b/lib/getServerSideProps.ts index bfafe67..432d187 100644 --- a/lib/getServerSideProps.ts +++ b/lib/getServerSideProps.ts @@ -1,4 +1,6 @@ -import { getBookingByUUID } from '../db/index' +import { startOfYear } from 'date-fns' +import { nowInTz } from '../helpers/date' +import { getBookingByUUID, getBookings } from '../db/index' export interface ServerSideBooking { props: { @@ -6,6 +8,30 @@ export interface ServerSideBooking { } } +export interface ServerSideRecentBooking { + props: { + bookings: object[] + } +} + +export const getServerSideRecentBookings = async (): Promise< + ServerSideRecentBooking +> => { + const bookings = await getBookings({ + startDateGreaterThan: startOfYear(nowInTz()).toISOString(), + }) + + // TODO: hack, not sure why _id is not serilizable + const bookingsJSON = JSON.parse( + JSON.stringify(bookings.map((b) => b.toJSON())) + ) as object[] + return { + props: { + bookings: bookingsJSON, + }, + } +} + export const getServerSideBooking = async ( context: any ): Promise => { diff --git a/pages/admin/index.tsx b/pages/admin/index.tsx new file mode 100644 index 0000000..a28b231 --- /dev/null +++ b/pages/admin/index.tsx @@ -0,0 +1,91 @@ +import React from 'react' +import Head from 'next/head' +import Link from 'next/link' +import Footer from '../../components/footer' +import Header from '../../components/header' +import { daysFormatFrontend } from '../../helpers/date' +import withSession, { isAdminSession, redirectToLogin } from '../../lib/session' + +import { getServerSideRecentBookings } from '../../lib/getServerSideProps' + +export const getServerSideProps = withSession(async (context) => { + const { req, res } = context + + const adminUser = isAdminSession(req) + + if (!adminUser) { + redirectToLogin(req, res) + return { props: {} } + } + + const serverSideRecentBookingProps = await getServerSideRecentBookings() + return { + props: { + ...serverSideRecentBookingProps.props, + user: adminUser, + }, + } +}) + +export default function AdminRecentBookings({ bookings }) { + return ( + <> + + Pfadi Bussle Admin + + + +
+
+ {bookings.map((booking: any) => ( +
+
+

+ + Booking {booking.uuid} + +

+

+ Last updated {new Date(booking.updatedAt).toLocaleTimeString()} +

+
+
+
+
+
+ Buchungszeitraum +
+
+ {daysFormatFrontend(booking.days)} +
+
+
+
Bucher
+
+ {booking.booker.name} +
+
+
+
Email
+
+ {booking.booker.email} +
+
+
+
Status
+
+ {booking.status} +
+
+
+
+
+ ))} +
+