feat(SW-664): Hotel listing component and queries for content pages

This commit is contained in:
Erik Tiekstra
2024-12-11 14:46:38 +01:00
parent 118f1afafa
commit 3939bf7cdc
32 changed files with 989 additions and 140 deletions

View File

@@ -0,0 +1,73 @@
import { revalidateTag } from "next/cache"
import { headers } from "next/headers"
import { z } from "zod"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
import { badRequest, internalServerError, notFound } from "@/server/errors/next"
import { generateHotelUrlTag } from "@/utils/generateTag"
import type { NextRequest } from "next/server"
const validateJsonBody = z.object({
data: z.object({
content_type: z.object({
uid: z.literal("hotel_page"),
}),
entry: z.object({
hotel_page_id: z.string(),
locale: z.nativeEnum(Lang),
publish_details: z.object({ locale: z.nativeEnum(Lang) }).optional(),
}),
}),
})
export async function POST(request: NextRequest) {
try {
const headersList = headers()
const secret = headersList.get("x-revalidate-secret")
if (secret !== env.REVALIDATE_SECRET) {
console.error(`Invalid Secret`)
console.error({ secret })
return badRequest({ revalidated: false, now: Date.now() })
}
const data = await request.json()
const validatedData = validateJsonBody.safeParse(data)
if (!validatedData.success) {
console.error("Bad validation for `validatedData` in hotel revalidation")
console.error(validatedData.error)
return internalServerError({ revalidated: false, now: Date.now() })
}
const {
data: {
data: { content_type, entry },
},
} = validatedData
// The publish_details.locale is the locale that the entry is published in, regardless if it is "localized" or not
const locale = entry.publish_details?.locale ?? entry.locale
let tag = ""
if (content_type.uid === "hotel_page") {
const tag = generateHotelUrlTag(locale, entry.hotel_page_id)
} else {
console.error(
`Invalid content_type, received ${content_type.uid}, expected "hotel_page"`
)
return notFound({ revalidated: false, now: Date.now() })
}
console.info(`Revalidating hotel url tag: ${tag}`)
revalidateTag(tag)
return Response.json({ revalidated: true, now: Date.now() })
} catch (error) {
console.error("Failed to revalidate tag(s) for hotel")
console.error(error)
return internalServerError({ revalidated: false, now: Date.now() })
}
}