emm - use string instead of date for start/endDate

This commit is contained in:
Thomas Ruoff
2022-03-30 23:56:08 +02:00
parent 9f45eab6e7
commit c65d8df853
8 changed files with 13652 additions and 878 deletions

View File

@@ -15,8 +15,8 @@ export default function DateSelect() {
<> <>
<InputWrapper label="Datum" required> <InputWrapper label="Datum" required>
<Calendar <Calendar
start={dateFormatBackend(startDate)} start={startDate}
end={dateFormatBackend(endDate)} end={endDate}
onChange={onChange} onChange={onChange}
className="my-6 max-w-lg" className="my-6 max-w-lg"
/> />
@@ -28,7 +28,7 @@ export default function DateSelect() {
type="date" type="date"
className="" className=""
name="startDate" name="startDate"
value={dateFormatBackend(startDate) || ''} value={startDate || ''}
onChange={onChangeEvent} onChange={onChangeEvent}
min={today} min={today}
required required
@@ -41,10 +41,10 @@ export default function DateSelect() {
type="date" type="date"
className="" className=""
name="endDate" name="endDate"
value={dateFormatBackend(endDate) || ''} value={endDate || ''}
placeholder="Von" placeholder="Von"
onChange={onChangeEvent} onChange={onChangeEvent}
min={dateFormatBackend(startDate) || today} min={startDate || today}
/> />
</div> </div>
</div> </div>

View File

@@ -107,6 +107,8 @@ export default function MyCalendar({
return return
} }
const dateAsBackendFormat = dateFormatBackend(date);
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
@@ -122,15 +124,15 @@ export default function MyCalendar({
// when startDate missing or both are already set // when startDate missing or both are already set
if (!startDate || (!!startDate && !!endDate)) { if (!startDate || (!!startDate && !!endDate)) {
onChange({ startDate: date, endDate: null }) onChange({ startDate: dateAsBackendFormat, endDate: null })
return return
} }
// when startDate set, but end missing // when startDate set, but end missing
if (isAfter(date, startDate)) { if (isAfter(date, startDate)) {
onChange({ endDate: date }) onChange({ endDate: dateAsBackendFormat })
} else { } else {
onChange({ startDate: date, endDate: date }) onChange({ startDate: dateAsBackendFormat, endDate: dateAsBackendFormat })
} }
}} }}
tileClassName={tileClassName} tileClassName={tileClassName}

View File

@@ -5,7 +5,7 @@ import { clearBookingData, loadBookingData } from '../helpers/storage'
import { createBooking } from '../helpers/booking' import { createBooking } from '../helpers/booking'
import { Booking } from '../db/booking' import { Booking } from '../db/booking'
export type BookFormData = Omit<Booking, 'uuid' | 'calendarEventId' | 'start' | 'end'> export type BookFormData = Omit<Booking, 'uuid' | 'calendarEventId'>
type BookingProviderState = { type BookingProviderState = {
postData?: boolean postData?: boolean

View File

@@ -1,6 +1,6 @@
import * as mongoose from 'mongoose' import * as mongoose from 'mongoose'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { dateFormatBackend, getDays, nowInTz } from '../helpers/date' import { dateFormatBackend, getDays, nowInTz, dateParseBackend } from '../helpers/date'
import { createCalendarEvent, deleteCalendarEvent } from '../lib/googlecalendar' import { createCalendarEvent, deleteCalendarEvent } from '../lib/googlecalendar'
import { Bill } from './bill' import { Bill } from './bill'
@@ -17,11 +17,9 @@ export type Booking = {
city: string city: string
bill?: Bill bill?: Bill
// format YYYY-MM-DD // format YYYY-MM-DD
start: string, startDate: string,
startDate: Date
// format YYYY-MM-DD // format YYYY-MM-DD
end: string endDate: string,
endDate: Date
status?: BOOKING_STATUS status?: BOOKING_STATUS
purpose?: string purpose?: string
org?: string org?: string
@@ -58,12 +56,18 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
required: false, required: false,
}, },
startDate: { startDate: {
type: Date, type: String,
required: true, required: true,
validator: function (value: string): boolean {
return !!dateParseBackend(value);
},
}, },
endDate: { endDate: {
type: Date, type: String,
required: false, required: true,
validator: function (value: string): boolean {
return !!dateParseBackend(value);
},
}, },
days: { days: {
type: [String], type: [String],
@@ -102,16 +106,6 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
} }
) )
BookingSchema.virtual('start').get(function (): string {
const booking = this as BookingDocument
return dateFormatBackend(booking.startDate)
})
BookingSchema.virtual('end').get(function (): string {
const booking = this as BookingDocument
return dateFormatBackend(booking.endDate)
})
BookingSchema.pre('validate', function (next: () => void): void { BookingSchema.pre('validate', function (next: () => void): void {
const booking = this as BookingDocument const booking = this as BookingDocument
booking.days = getDays({ booking.days = getDays({

View File

@@ -68,6 +68,10 @@ export function dateParseFrontend(input: string): Date {
return dateParse(input, FRONTEND_FORMAT) return dateParse(input, FRONTEND_FORMAT)
} }
export function dateParseBackend(input: string): Date {
return dateParse(input, BACKEND_FORMAT)
}
export function nowInTz(timezone = 'Europe/Berlin'): Date { export function nowInTz(timezone = 'Europe/Berlin'): Date {
const now = new Date() const now = new Date()
return utcToZonedTime(now, timezone) return utcToZonedTime(now, timezone)

View File

@@ -49,9 +49,9 @@ export async function createCalendarEvent(booking: Booking): Promise<Booking> {
calendarId, calendarId,
requestBody: { requestBody: {
summary: getSummary(booking), summary: getSummary(booking),
// description, // TODO: description,
start: { date: booking.start }, start: { date: booking.startDate },
end: { date: booking.end }, end: { date: booking.endDate },
}, },
}, {}) }, {})

14468
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -38,7 +38,7 @@ function ShowBookingAdmin({ booking: bookingProp }: { booking: Booking }) {
return ( return (
<Layout> <Layout>
<h2 className="text-3xl">Buchung {booking.uuid}</h2> <h2 className="text-3xl">Buchung {booking.uuid}</h2>
<Calendar start={booking.start} end={booking.end} /> <Calendar start={booking.startDate} end={booking.endDate} />
<div> <div>
<strong>Buchungszeitraum:</strong> {daysFormatFrontend(booking.days)} <strong>Buchungszeitraum:</strong> {daysFormatFrontend(booking.days)}
</div> </div>