feat(SW-1453): added city listing component * feat(SW-1453): added city listing component Approved-by: Christian Andolf Approved-by: Fredrik Thorsson
111 lines
3.1 KiB
TypeScript
111 lines
3.1 KiB
TypeScript
import { GetDestinationCityListData } from "@/lib/graphql/Query/DestinationCityPage/DestinationCityListData.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
|
|
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
|
|
|
|
import { destinationCityListDataSchema } from "../destinationCityPage/output"
|
|
import {
|
|
getCityListDataCounter,
|
|
getCityListDataFailCounter,
|
|
getCityListDataSuccessCounter,
|
|
} from "./telemetry"
|
|
|
|
import type { System } from "@/types/requests/system"
|
|
import type { GetDestinationCityListDataResponse } from "@/types/trpc/routers/contentstack/destinationCityPage"
|
|
import type { GetDestinationCountryPageRefsSchema } from "@/types/trpc/routers/contentstack/destinationCountryPage"
|
|
import type { Lang } from "@/constants/languages"
|
|
|
|
export function generatePageTags(
|
|
validatedData: GetDestinationCountryPageRefsSchema,
|
|
lang: Lang
|
|
): string[] {
|
|
const connections = getConnections(validatedData)
|
|
return [
|
|
generateTagsFromSystem(lang, connections),
|
|
generateTag(lang, validatedData.destination_country_page.system.uid),
|
|
].flat()
|
|
}
|
|
|
|
export function getConnections({
|
|
destination_country_page,
|
|
}: GetDestinationCountryPageRefsSchema) {
|
|
const connections: System["system"][] = [destination_country_page.system]
|
|
|
|
if (destination_country_page.sidepeek_content) {
|
|
destination_country_page.sidepeek_content.content.embedded_itemsConnection.edges.forEach(
|
|
({ node }) => {
|
|
connections.push(node.system)
|
|
}
|
|
)
|
|
}
|
|
|
|
return connections
|
|
}
|
|
|
|
export async function getCityListDataByCityIdentifier(
|
|
lang: Lang,
|
|
cityIdentifier: string
|
|
) {
|
|
getCityListDataCounter.add(1, { lang, cityIdentifier })
|
|
console.info(
|
|
"contentstack.cityListData start",
|
|
JSON.stringify({ query: { lang, cityIdentifier } })
|
|
)
|
|
const tag = `${lang}:city_list_data:${cityIdentifier}`
|
|
const response = await request<GetDestinationCityListDataResponse>(
|
|
GetDestinationCityListData,
|
|
{
|
|
locale: lang,
|
|
cityIdentifier,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [tag],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
getCityListDataFailCounter.add(1, {
|
|
lang,
|
|
cityIdentifier,
|
|
error_type: "not_found",
|
|
error: `Destination city page not found for cityIdentifier: ${cityIdentifier}`,
|
|
})
|
|
console.error(
|
|
"contentstack.cityListData not found error",
|
|
JSON.stringify({ query: { lang, cityIdentifier } })
|
|
)
|
|
return null
|
|
}
|
|
|
|
const validatedResponse = destinationCityListDataSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedResponse.success) {
|
|
getCityListDataFailCounter.add(1, {
|
|
lang,
|
|
cityIdentifier,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedResponse.error),
|
|
})
|
|
console.error(
|
|
"contentstack.cityListData validation error",
|
|
JSON.stringify({
|
|
query: { lang, cityIdentifier },
|
|
error: validatedResponse.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getCityListDataSuccessCounter.add(1, { lang, cityIdentifier })
|
|
console.info(
|
|
"contentstack.cityListData success",
|
|
JSON.stringify({ query: { lang, cityIdentifier } })
|
|
)
|
|
|
|
return validatedResponse.data
|
|
}
|