Files
web/apps/scandic-web/components/OpeningHours/utils.ts
Anton Gunnarsson 002d093af4 Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
feat(SW-2863): Move contentstack router to trpc package

* Add exports to packages and lint rule to prevent relative imports

* Add env to trpc package

* Add eslint to trpc package

* Apply lint rules

* Use direct imports from trpc package

* Add lint-staged config to trpc

* Move lang enum to common

* Restructure trpc package folder structure

* WIP first step

* update internal imports in trpc

* Fix most errors in scandic-web

Just 100 left...

* Move Props type out of trpc

* Fix CategorizedFilters types

* Move more schemas in hotel router

* Fix deps

* fix getNonContentstackUrls

* Fix import error

* Fix entry error handling

* Fix generateMetadata metrics

* Fix alertType enum

* Fix duplicated types

* lint:fix

* Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package

* Fix broken imports

* Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package


Approved-by: Linus Flood
2025-06-26 07:53:01 +00:00

111 lines
2.7 KiB
TypeScript

import type { RestaurantOpeningHours } from "@scandic-hotels/trpc/types/hotel"
import type { IntlShape } from "react-intl"
export function getGroupedOpeningHours(
openingHours: RestaurantOpeningHours,
intl: IntlShape
): string[] {
const closedMsg = intl.formatMessage({
defaultMessage: "Closed",
})
const alwaysOpenMsg = intl.formatMessage({
defaultMessage: "Always open",
})
// In order
const weekdayDefinitions = [
{
key: "monday",
label: intl.formatMessage({
defaultMessage: "Monday",
}),
},
{
key: "tuesday",
label: intl.formatMessage({
defaultMessage: "Tuesday",
}),
},
{
key: "wednesday",
label: intl.formatMessage({
defaultMessage: "Wednesday",
}),
},
{
key: "thursday",
label: intl.formatMessage({
defaultMessage: "Thursday",
}),
},
{
key: "friday",
label: intl.formatMessage({
defaultMessage: "Friday",
}),
},
{
key: "saturday",
label: intl.formatMessage({
defaultMessage: "Saturday",
}),
},
{
key: "sunday",
label: intl.formatMessage({
defaultMessage: "Sunday",
}),
},
] as const
const groupedOpeningHours: string[] = []
let rangeWeekdays: string[] = []
let rangeValue = ""
for (let i = 0, n = weekdayDefinitions.length; i < n; ++i) {
const weekdayDefinition = weekdayDefinitions[i]
const weekday = openingHours[weekdayDefinition.key]
const label = weekdayDefinition.label
if (weekday) {
let newValue = null
if (weekday.alwaysOpen) {
newValue = alwaysOpenMsg
} else if (weekday.isClosed) {
newValue = closedMsg
} else if (weekday.openingTime && weekday.closingTime) {
newValue = `${weekday.openingTime}-${weekday.closingTime}`
}
if (newValue !== null) {
if (rangeValue === newValue) {
if (rangeWeekdays.length > 1) {
rangeWeekdays.splice(-1, 1, label) // Replace last element
} else {
rangeWeekdays.push(label)
}
} else {
if (rangeValue) {
groupedOpeningHours.push(
`${rangeWeekdays.join("-")}: ${rangeValue}`
)
}
rangeValue = newValue
rangeWeekdays = [label]
}
}
if (rangeValue && i === n - 1) {
// Flush everything at the end
groupedOpeningHours.push(`${rangeWeekdays.join("-")}: ${rangeValue}`)
}
} else if (rangeValue) {
groupedOpeningHours.push(`${rangeWeekdays.join("-")}: ${rangeValue}`)
rangeValue = ""
rangeWeekdays = []
}
}
return groupedOpeningHours
}