Feat(LOY-428): Previous Stays Redesign * feat(LOY-428): Previous stays WIP * fix(LOY-428): fix alignment issue * fix(LOY-428): css fixes & imagefallback prop value * fix(LOY-428): use css vars * fix(LOY-428): add unit test for relative time text * chore(LOY-428): remove else if conditions * fix(LOY-428): named exports & remove duplicate width/height setting * fix(LOY-428): better formatting of upcoming stays months text * fix(LOY-428): fewer typography wrappers Approved-by: Matilda Landström
58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import { dt } from "@scandic-hotels/common/dt"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
import type { IntlShape } from "react-intl"
|
|
|
|
export function getDaysUntilText(
|
|
checkinDate: string,
|
|
lang: Lang,
|
|
intl: IntlShape
|
|
): string {
|
|
const checkInDateTime = dt(checkinDate).locale(lang).startOf("day")
|
|
const now = dt().locale(lang).startOf("day")
|
|
const daysUntil = checkInDateTime.diff(now, "days")
|
|
|
|
// Handle past dates edge case.
|
|
if (daysUntil < 0) {
|
|
return dt(checkinDate).locale(lang).format("D MMM YYYY")
|
|
}
|
|
|
|
if (daysUntil === 0) {
|
|
return intl.formatMessage({
|
|
id: "nextStay.today",
|
|
defaultMessage: "Today",
|
|
})
|
|
}
|
|
|
|
if (daysUntil === 1) {
|
|
return intl.formatMessage({
|
|
id: "nextStay.tomorrow",
|
|
defaultMessage: "Tomorrow",
|
|
})
|
|
}
|
|
|
|
if (daysUntil > 1 && daysUntil <= 30) {
|
|
return intl.formatMessage(
|
|
{
|
|
id: "nextStay.inXDays",
|
|
defaultMessage: "In {days} days",
|
|
},
|
|
{
|
|
days: daysUntil,
|
|
}
|
|
)
|
|
}
|
|
|
|
// Use proper month calculation for dates beyond 30 days
|
|
const monthsUntil = checkInDateTime.diff(now, "months")
|
|
return intl.formatMessage(
|
|
{
|
|
id: "nextStay.inXMonths",
|
|
defaultMessage: "In {months, plural, one {# month} other { # months}}",
|
|
},
|
|
{
|
|
months: monthsUntil,
|
|
}
|
|
)
|
|
}
|