fix(SW-3691): Setup one prettier config for whole repo * Setup prettierrc in root and remove other configs Approved-by: Joakim Jäderberg Approved-by: Linus Flood
195 lines
4.8 KiB
TypeScript
195 lines
4.8 KiB
TypeScript
import { logger } from "@scandic-hotels/common/logger"
|
|
|
|
import type { IntlShape } from "react-intl"
|
|
import type { AlternateOpeningHours, OpeningHours } from "./openingHoursTypes"
|
|
|
|
export function getGroupedOpeningHours(
|
|
openingHours: OpeningHours | AlternateOpeningHours,
|
|
intl: IntlShape
|
|
): string[] {
|
|
const closedMsg = intl.formatMessage({
|
|
id: "openingHours.closed",
|
|
defaultMessage: "Closed",
|
|
})
|
|
const alwaysOpenMsg = intl.formatMessage({
|
|
id: "openingHours.alwaysOpen",
|
|
defaultMessage: "Always open",
|
|
})
|
|
|
|
// In order
|
|
const weekdayDefinitions = [
|
|
{
|
|
key: "monday",
|
|
label: intl.formatMessage({
|
|
id: "common.monday",
|
|
defaultMessage: "Monday",
|
|
}),
|
|
},
|
|
{
|
|
key: "tuesday",
|
|
label: intl.formatMessage({
|
|
id: "common.tuesday",
|
|
defaultMessage: "Tuesday",
|
|
}),
|
|
},
|
|
{
|
|
key: "wednesday",
|
|
label: intl.formatMessage({
|
|
id: "common.wednesday",
|
|
defaultMessage: "Wednesday",
|
|
}),
|
|
},
|
|
{
|
|
key: "thursday",
|
|
label: intl.formatMessage({
|
|
id: "common.thursday",
|
|
defaultMessage: "Thursday",
|
|
}),
|
|
},
|
|
{
|
|
key: "friday",
|
|
label: intl.formatMessage({
|
|
id: "common.friday",
|
|
defaultMessage: "Friday",
|
|
}),
|
|
},
|
|
{
|
|
key: "saturday",
|
|
label: intl.formatMessage({
|
|
id: "common.saturday",
|
|
defaultMessage: "Saturday",
|
|
}),
|
|
},
|
|
{
|
|
key: "sunday",
|
|
label: intl.formatMessage({
|
|
id: "common.sunday",
|
|
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
|
|
}
|
|
|
|
export function getTranslatedName(name: string, intl: IntlShape) {
|
|
const lowercaseName = name.toLowerCase()
|
|
switch (lowercaseName) {
|
|
case "breakfast":
|
|
return intl.formatMessage({
|
|
id: "common.breakfast",
|
|
defaultMessage: "Breakfast",
|
|
})
|
|
case "brunch":
|
|
return intl.formatMessage({
|
|
id: "common.brunch",
|
|
defaultMessage: "Brunch",
|
|
})
|
|
case "after work":
|
|
return intl.formatMessage({
|
|
id: "common.afterwork",
|
|
defaultMessage: "After Work",
|
|
})
|
|
case "cafe":
|
|
return intl.formatMessage({
|
|
id: "common.cafe",
|
|
defaultMessage: "Café",
|
|
})
|
|
case "lunch":
|
|
return intl.formatMessage({
|
|
id: "common.lunch",
|
|
defaultMessage: "Lunch",
|
|
})
|
|
case "dinner":
|
|
return intl.formatMessage({
|
|
id: "common.dinner",
|
|
defaultMessage: "Dinner",
|
|
})
|
|
case "bar":
|
|
return intl.formatMessage({
|
|
id: "common.bar",
|
|
defaultMessage: "Bar",
|
|
})
|
|
case "snacks & drinks":
|
|
return intl.formatMessage({
|
|
id: "common.snacks&drinks",
|
|
defaultMessage: "Snacks & drinks",
|
|
})
|
|
case "takeaway":
|
|
return intl.formatMessage({
|
|
id: "common.takeaway",
|
|
defaultMessage: "Takeaway",
|
|
})
|
|
case "changes":
|
|
return intl.formatMessage({
|
|
id: "common.changes",
|
|
defaultMessage: "Changes",
|
|
})
|
|
case "live events":
|
|
return intl.formatMessage({
|
|
id: "common.liveEvents",
|
|
defaultMessage: "Live events",
|
|
})
|
|
case "terrace":
|
|
return intl.formatMessage({
|
|
id: "common.terrace",
|
|
defaultMessage: "Terrace",
|
|
})
|
|
default:
|
|
logger.warn(`Unsupported name given: ${name}`)
|
|
|
|
return intl.formatMessage({
|
|
id: "common.NA",
|
|
defaultMessage: "N/A",
|
|
})
|
|
}
|
|
}
|