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 reqFromDate = bookingWidgetSearchData?.fromDate?.toString()
|
||||||
const reqToDate = bookingWidgetSearchData?.toDate?.toString()
|
const reqToDate = bookingWidgetSearchData?.toDate?.toString()
|
||||||
|
|
||||||
|
const parsedFromDate = reqFromDate ? dt(reqFromDate) : undefined
|
||||||
|
const parsedToDate = reqToDate ? dt(reqToDate) : undefined
|
||||||
|
|
||||||
|
const now = dt()
|
||||||
|
|
||||||
const isDateParamValid =
|
const isDateParamValid =
|
||||||
reqFromDate &&
|
parsedFromDate &&
|
||||||
reqToDate &&
|
parsedToDate &&
|
||||||
dt(reqFromDate).isAfter(dt().subtract(1, "day")) &&
|
parsedFromDate.isSameOrAfter(now, "day") &&
|
||||||
dt(reqToDate).isAfter(dt(reqFromDate))
|
parsedToDate.isAfter(parsedFromDate)
|
||||||
|
|
||||||
const selectedLocation = bookingWidgetSearchData
|
const selectedLocation = bookingWidgetSearchData
|
||||||
? getLocationObj(
|
? 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
|
// 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.
|
// This is specifically to handle timezones falling in different dates.
|
||||||
fromDate: isDateParamValid
|
fromDate: isDateParamValid
|
||||||
? dt(bookingWidgetSearchData?.fromDate).format("YYYY-M-D")
|
? parsedFromDate.format("YYYY-MM-DD")
|
||||||
: dt().utc().format("YYYY-M-D"),
|
: now.utc().format("YYYY-MM-DD"),
|
||||||
toDate: isDateParamValid
|
toDate: isDateParamValid
|
||||||
? dt(bookingWidgetSearchData?.toDate).format("YYYY-M-D")
|
? parsedToDate.format("YYYY-MM-DD")
|
||||||
: dt().utc().add(1, "day").format("YYYY-M-D"),
|
: now.utc().add(1, "day").format("YYYY-MM-DD"),
|
||||||
},
|
},
|
||||||
bookingCode: "",
|
bookingCode: "",
|
||||||
redemption: false,
|
redemption: false,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import "dayjs/locale/sv"
|
|||||||
|
|
||||||
import d from "dayjs"
|
import d from "dayjs"
|
||||||
import advancedFormat from "dayjs/plugin/advancedFormat"
|
import advancedFormat from "dayjs/plugin/advancedFormat"
|
||||||
|
import isSameOrAfter from "dayjs/plugin/isSameOrAfter"
|
||||||
import isToday from "dayjs/plugin/isToday"
|
import isToday from "dayjs/plugin/isToday"
|
||||||
import relativeTime from "dayjs/plugin/relativeTime"
|
import relativeTime from "dayjs/plugin/relativeTime"
|
||||||
import timezone from "dayjs/plugin/timezone"
|
import timezone from "dayjs/plugin/timezone"
|
||||||
@@ -62,5 +63,6 @@ d.extend(isToday)
|
|||||||
d.extend(relativeTime)
|
d.extend(relativeTime)
|
||||||
d.extend(timezone)
|
d.extend(timezone)
|
||||||
d.extend(utc)
|
d.extend(utc)
|
||||||
|
d.extend(isSameOrAfter)
|
||||||
|
|
||||||
export const dt = d
|
export const dt = d
|
||||||
|
|||||||
@@ -19,18 +19,12 @@ function normalizeDate(date: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const middleware: NextMiddleware = (request) => {
|
export const middleware: NextMiddleware = (request) => {
|
||||||
const url = request.nextUrl
|
const url = request.nextUrl.clone()
|
||||||
const { searchParams } = url
|
const { searchParams } = url
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!!(
|
legacyDatePattern.test(searchParams.get("fromDate")!) ||
|
||||||
searchParams.has("fromDate") &&
|
legacyDatePattern.test(searchParams.get("toDate")!)
|
||||||
searchParams.get("fromDate")!.match(legacyDatePattern)
|
|
||||||
) ||
|
|
||||||
!!(
|
|
||||||
searchParams.has("toDate") &&
|
|
||||||
searchParams.get("toDate")!.match(legacyDatePattern)
|
|
||||||
)
|
|
||||||
) {
|
) {
|
||||||
const fromDate = searchParams.get("fromDate")!
|
const fromDate = searchParams.get("fromDate")!
|
||||||
url.searchParams.set("fromDate", normalizeDate(fromDate))
|
url.searchParams.set("fromDate", normalizeDate(fromDate))
|
||||||
|
|||||||
Reference in New Issue
Block a user