Merged in fix/SW-2068-correct-public-url-from-my-stays-overview-page (pull request #1694)

feat(SW-2068): Link to Correct Public URL from within My Stays Overview Page

* feat(SW-2068): Link to Correct Public URL from within My Stays Overview Page

- Added language parameter to previous and upcoming stays queries.
- Updated Client components to utilize the new language hook.
- Refactored EmptyUpcomingStays component to dynamically generate links based on the current language.
- Adjusted user input validation to include optional language parameter.

* refactor(SW-2068): Update EmptyUpcomingStays components to use PUBLIC_URL + add utility to handle TLD based on language

* chore(SW-2068): Clarify TLD

* feat(SW-2068): documentation for getTldForLanguage

* refactor(SW-2068): Simplify booking URL construction in updateStaysBookingUrl

* refactor(SW-2068): Remove incorrect TLD update logic from booking URL construction

* refactor(SW-2068): Centralize booking URL paths using myBookingPath constant

* refactor(SW-2068): Streamline search params in booking URL construction logic in updateStaysBookingUrl


Approved-by: Christian Andolf
This commit is contained in:
Chuma Mcphoy (We Ahead)
2025-04-02 12:44:02 +00:00
parent 67905198c6
commit 56d5ad77d1
6 changed files with 109 additions and 89 deletions

View File

@@ -1,17 +1,23 @@
import { MaterialIcon } from "@scandic-hotels/design-system/Icons"
import { homeHrefs } from "@/constants/homeHrefs"
import { env } from "@/env/server"
import Link from "@/components/TempDesignSystem/Link"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getIntl } from "@/i18n"
import { getLang } from "@/i18n/serverContext"
import { getCurrentWebUrl } from "@/utils/url"
import styles from "./emptyUpcomingStays.module.css"
export default async function EmptyUpcomingStaysBlock() {
const intl = await getIntl()
const lang = getLang()
const href = env.HIDE_FOR_NEXT_RELEASE
? getCurrentWebUrl("/", lang)
: `/${lang}`
return (
<section className={styles.container}>
<div className={styles.titleContainer}>
@@ -28,11 +34,7 @@ export default async function EmptyUpcomingStaysBlock() {
</span>
</Title>
</div>
<Link
href={homeHrefs[env.NODE_ENV][getLang()]}
className={styles.link}
color="peach80"
>
<Link href={href} className={styles.link} color="peach80">
{intl.formatMessage({ id: "Get inspired" })}
<MaterialIcon icon="arrow_forward" color="CurrentColor" />
</Link>

View File

@@ -1,17 +1,23 @@
import { MaterialIcon } from "@scandic-hotels/design-system/Icons"
import { homeHrefs } from "@/constants/homeHrefs"
import { env } from "@/env/server"
import Link from "@/components/TempDesignSystem/Link"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getIntl } from "@/i18n"
import { getLang } from "@/i18n/serverContext"
import { getCurrentWebUrl } from "@/utils/url"
import styles from "./emptyUpcomingStays.module.css"
export default async function EmptyUpcomingStaysBlock() {
const intl = await getIntl()
const lang = getLang()
const href = env.HIDE_FOR_NEXT_RELEASE
? getCurrentWebUrl("/", lang)
: `/${lang}`
return (
<section className={styles.container}>
<div className={styles.titleContainer}>
@@ -28,14 +34,9 @@ export default async function EmptyUpcomingStaysBlock() {
</span>
</Title>
</div>
<Link
href={homeHrefs[env.NODE_ENV][getLang()]}
className={styles.link}
color="peach80"
>
<Link href={href} className={styles.link} color="peach80">
{intl.formatMessage({ id: "Get inspired" })}
<MaterialIcon color="CurrentColor" icon="arrow_forward" />
<MaterialIcon icon="arrow_forward" color="CurrentColor" />
</Link>
</section>
)

View File

@@ -1,26 +1,37 @@
import type { LangRoute } from "@/types/routes"
export const myBookingPath: LangRoute = {
da: "/hotelreservation/min-booking",
de: "/hotelreservation/my-booking",
en: "/hotelreservation/my-booking",
fi: "/varaa-hotelli/varauksesi",
no: "/hotelreservation/my-booking",
sv: "/hotelreservation/din-bokning",
}
export const myBooking = {
development: {
da: "https://stage.scandichotels.dk/hotelreservation/min-booking",
de: "https://stage.scandichotels.de/hotelreservation/my-booking",
en: "https://stage.scandichotels.com/hotelreservation/my-booking",
fi: "https://stage.scandichotels.fi/varaa-hotelli/varauksesi",
no: "https://stage.scandichotels.no/hotelreservation/my-booking",
sv: "https://stage.scandichotels.se/hotelreservation/din-bokning",
da: `https://stage.scandichotels.dk${myBookingPath.da}`,
de: `https://stage.scandichotels.de${myBookingPath.de}`,
en: `https://stage.scandichotels.com${myBookingPath.en}`,
fi: `https://stage.scandichotels.fi${myBookingPath.fi}`,
no: `https://stage.scandichotels.no${myBookingPath.no}`,
sv: `https://stage.scandichotels.se${myBookingPath.sv}`,
},
production: {
da: "https://www.scandichotels.dk/hotelreservation/min-booking",
de: "https://www.scandichotels.de/hotelreservation/my-booking",
en: "https://www.scandichotels.com/hotelreservation/my-booking",
fi: "https://www.scandichotels.fi/varaa-hotelli/varauksesi",
no: "https://www.scandichotels.no/hotelreservation/my-booking",
sv: "https://www.scandichotels.se/hotelreservation/din-bokning",
da: `https://www.scandichotels.dk${myBookingPath.da}`,
de: `https://www.scandichotels.de${myBookingPath.de}`,
en: `https://www.scandichotels.com${myBookingPath.en}`,
fi: `https://www.scandichotels.fi${myBookingPath.fi}`,
no: `https://www.scandichotels.no${myBookingPath.no}`,
sv: `https://www.scandichotels.se${myBookingPath.sv}`,
},
test: {
da: "https://test.scandichotels.dk/hotelreservation/min-booking",
de: "https://test.scandichotels.de/hotelreservation/my-booking",
en: "https://test.scandichotels.com/hotelreservation/my-booking",
fi: "https://test.scandichotels.fi/varaa-hotelli/varauksesi",
no: "https://test.scandichotels.no/hotelreservation/my-booking",
sv: "https://test.scandichotels.se/hotelreservation/din-bokning",
da: `https://test.scandichotels.dk${myBookingPath.da}`,
de: `https://test.scandichotels.de${myBookingPath.de}`,
en: `https://test.scandichotels.com${myBookingPath.en}`,
fi: `https://test.scandichotels.fi${myBookingPath.fi}`,
no: `https://test.scandichotels.no${myBookingPath.no}`,
sv: `https://test.scandichotels.se${myBookingPath.sv}`,
},
}

View File

@@ -449,7 +449,7 @@ export const userQueryRouter = router({
}),
stays: router({
previous: protectedProcedure
previous: languageProtectedProcedure
.input(staysInput)
.query(async ({ ctx, input }) => {
const { limit, cursor, lang } = input
@@ -543,7 +543,7 @@ export const userQueryRouter = router({
}
}),
upcoming: protectedProcedure
upcoming: languageProtectedProcedure
.input(staysInput)
.query(async ({ ctx, input }) => {
const { limit, cursor, lang } = input

View File

@@ -1,13 +1,15 @@
import { metrics } from "@opentelemetry/api"
import { homeHrefs } from "@/constants/homeHrefs"
import { Lang } from "@/constants/languages"
import { myBookingPath } from "@/constants/myBooking"
import { myStay } from "@/constants/routes/myStay"
import { env } from "@/env/server"
import * as api from "@/lib/api"
import { getCurrentWebUrl } from "@/utils/url"
import { encrypt } from "../utils/encryption"
import type { Lang } from "@/constants/languages"
import type { FriendTransaction, Stay } from "./output"
const meter = metrics.getMeter("trpc.user")
@@ -68,62 +70,23 @@ async function updateStaysBookingUrl(
apiJson.data.attributes.lastName
const encryptedBookingValue = encrypt(originalString)
// Construct URL using myStay route and append encrypted value
const bookingUrlPath = encryptedBookingValue
? `${myStay[lang]}?RefId=${encryptedBookingValue}`
: `${myStay[lang]}?bookingId=${d.attributes.confirmationNumber}&lastName=${apiJson.data.attributes.lastName}`
// Get base URL with fallback for ephemeral environments (like deploy previews).
const baseUrl = env.PUBLIC_URL || "https://www.scandichotels.com"
// Construct full URL with domain
const domain = homeHrefs[env.NODE_ENV][lang]
let bookingUrl = new URL(bookingUrlPath, domain)
// Construct Booking URL.
const bookingUrl = env.HIDE_FOR_NEXT_RELEASE
? new URL(getCurrentWebUrl(myBookingPath[lang], lang))
: new URL(myStay[lang], baseUrl)
// Update TLD based on language
if (lang !== Lang.en)
bookingUrl.host = bookingUrl.host.replace(".com", `.${lang}`)
if (env.HIDE_FOR_NEXT_RELEASE) {
// Temporary Url, domain and lang support for current web
bookingUrl = new URL(
"/hotelreservation/my-booking",
env.PUBLIC_URL || "https://www.scandichotels.com" // fallback to production for ephemeral envs (like deploy previews)
// Add search parameters.
if (encryptedBookingValue) {
bookingUrl.searchParams.set("RefId", encryptedBookingValue)
} else {
bookingUrl.searchParams.set("lastName", apiJson.data.attributes.lastName)
bookingUrl.searchParams.set(
"bookingId",
d.attributes.confirmationNumber.toString()
)
switch (lang) {
case Lang.sv:
bookingUrl.host = bookingUrl.host.replace(".com", ".se")
bookingUrl.pathname = "/hotelreservation/din-bokning"
break
case Lang.no:
bookingUrl.host = bookingUrl.host.replace(".com", ".no")
bookingUrl.pathname = "/hotelreservation/my-booking"
break
case Lang.da:
bookingUrl.host = bookingUrl.host.replace(".com", ".dk")
bookingUrl.pathname = "/hotelreservation/min-booking"
break
case Lang.fi:
bookingUrl.host = bookingUrl.host.replace(".com", ".fi")
bookingUrl.pathname = "/varaa-hotelli/varauksesi"
break
case Lang.de:
bookingUrl.host = bookingUrl.host.replace(".com", ".de")
bookingUrl.pathname = "/hotelreservation/my-booking"
break
default:
break
}
if (!!encryptedBookingValue) {
bookingUrl.searchParams.set("RefId", encryptedBookingValue)
} else {
bookingUrl.searchParams.set(
"lastName",
apiJson.data.attributes.lastName
)
bookingUrl.searchParams.set(
"bookingId",
d.attributes.confirmationNumber
)
}
}
return {

View File

@@ -1,3 +1,6 @@
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
import type { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
import type {
Child,
@@ -164,3 +167,43 @@ export function convertObjToSearchParams<T>(
return bookingSearchParams
}
/**
* Returns the TLD (top-level domain) for a given language.
* @param lang - The language to get the TLD for
* @returns The TLD for the given language
*/
export function getTldForLanguage(lang: Lang): string {
switch (lang) {
case Lang.sv:
return "se"
case Lang.no:
return "no"
case Lang.da:
return "dk"
case Lang.fi:
return "fi"
case Lang.de:
return "de"
default:
return "com"
}
}
/**
* Constructs a URL with the correct TLD (top-level domain) based on lang, for current web.
* @param path - The path to append to the URL
* @param lang - The language to use for TLD
* @returns The complete URL with language-specific TLD
*/
export function getCurrentWebUrl(path: string, lang: Lang): string {
const baseUrl = env.PUBLIC_URL || "https://www.scandichotels.com" // Fallback for ephemeral environments (e.g. deploy previews).
const tld = getTldForLanguage(lang)
const url = new URL(path, baseUrl)
if (tld !== "com") {
url.host = url.host.replace(".com", `.${tld}`)
}
return url.toString()
}