fix: normalize format of dates when setting them in middleware
This commit is contained in:
@@ -66,11 +66,16 @@ export default function BookingWidgetClient({
|
||||
const reqFromDate = bookingWidgetSearchData?.fromDate?.toString()
|
||||
const reqToDate = bookingWidgetSearchData?.toDate?.toString()
|
||||
|
||||
const parsedFromDate = reqFromDate ? dt(reqFromDate) : undefined
|
||||
const parsedToDate = reqToDate ? dt(reqToDate) : undefined
|
||||
|
||||
const now = dt()
|
||||
|
||||
const isDateParamValid =
|
||||
reqFromDate &&
|
||||
reqToDate &&
|
||||
dt(reqFromDate).isAfter(dt().subtract(1, "day")) &&
|
||||
dt(reqToDate).isAfter(dt(reqFromDate))
|
||||
parsedFromDate &&
|
||||
parsedToDate &&
|
||||
parsedFromDate.isSameOrAfter(now, "day") &&
|
||||
parsedToDate.isAfter(parsedFromDate)
|
||||
|
||||
const selectedLocation = bookingWidgetSearchData
|
||||
? getLocationObj(
|
||||
@@ -97,11 +102,11 @@ export default function BookingWidgetClient({
|
||||
// UTC is required to handle requests from far away timezones https://scandichotels.atlassian.net/browse/SWAP-6375 & PET-507
|
||||
// This is specifically to handle timezones falling in different dates.
|
||||
fromDate: isDateParamValid
|
||||
? dt(bookingWidgetSearchData?.fromDate).format("YYYY-M-D")
|
||||
: dt().utc().format("YYYY-M-D"),
|
||||
? parsedFromDate.format("YYYY-MM-DD")
|
||||
: now.utc().format("YYYY-MM-DD"),
|
||||
toDate: isDateParamValid
|
||||
? dt(bookingWidgetSearchData?.toDate).format("YYYY-M-D")
|
||||
: dt().utc().add(1, "day").format("YYYY-M-D"),
|
||||
? parsedToDate.format("YYYY-MM-DD")
|
||||
: now.utc().add(1, "day").format("YYYY-MM-DD"),
|
||||
},
|
||||
bookingCode: "",
|
||||
redemption: false,
|
||||
|
||||
@@ -5,6 +5,7 @@ import "dayjs/locale/sv"
|
||||
|
||||
import d from "dayjs"
|
||||
import advancedFormat from "dayjs/plugin/advancedFormat"
|
||||
import isSameOrAfter from "dayjs/plugin/isSameOrAfter"
|
||||
import isToday from "dayjs/plugin/isToday"
|
||||
import relativeTime from "dayjs/plugin/relativeTime"
|
||||
import timezone from "dayjs/plugin/timezone"
|
||||
@@ -62,5 +63,6 @@ d.extend(isToday)
|
||||
d.extend(relativeTime)
|
||||
d.extend(timezone)
|
||||
d.extend(utc)
|
||||
d.extend(isSameOrAfter)
|
||||
|
||||
export const dt = d
|
||||
|
||||
@@ -19,18 +19,12 @@ function normalizeDate(date: string): string {
|
||||
}
|
||||
|
||||
export const middleware: NextMiddleware = (request) => {
|
||||
const url = request.nextUrl
|
||||
const url = request.nextUrl.clone()
|
||||
const { searchParams } = url
|
||||
|
||||
if (
|
||||
!!(
|
||||
searchParams.has("fromDate") &&
|
||||
searchParams.get("fromDate")!.match(legacyDatePattern)
|
||||
) ||
|
||||
!!(
|
||||
searchParams.has("toDate") &&
|
||||
searchParams.get("toDate")!.match(legacyDatePattern)
|
||||
)
|
||||
legacyDatePattern.test(searchParams.get("fromDate")!) ||
|
||||
legacyDatePattern.test(searchParams.get("toDate")!)
|
||||
) {
|
||||
const fromDate = searchParams.get("fromDate")!
|
||||
url.searchParams.set("fromDate", normalizeDate(fromDate))
|
||||
|
||||
Reference in New Issue
Block a user