Merged in feat/sw3370-move-phone-utils (pull request #2743)
feat(SW-3370): Move phone utils to common * Move phone utils to common * Update lock file Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@scandic-hotels/common/utils/phone"
|
||||
import { Divider } from "@scandic-hotels/design-system/Divider"
|
||||
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
|
||||
import DateSelect from "@scandic-hotels/design-system/Form/Date"
|
||||
@@ -9,10 +10,7 @@ import Phone from "@scandic-hotels/design-system/Form/Phone"
|
||||
import { Select } from "@scandic-hotels/design-system/Select"
|
||||
import { Typography } from "@scandic-hotels/design-system/Typography"
|
||||
|
||||
import {
|
||||
getDefaultCountryFromLang,
|
||||
getLocalizedLanguageOptions,
|
||||
} from "@/constants/languages"
|
||||
import { getLocalizedLanguageOptions } from "@/constants/languages"
|
||||
|
||||
import Input from "@/components/TempDesignSystem/Form/Input"
|
||||
import PasswordInput from "@/components/TempDesignSystem/Form/PasswordInput"
|
||||
|
||||
@@ -7,20 +7,22 @@ import { useIntl } from "react-intl"
|
||||
|
||||
import { profile } from "@scandic-hotels/common/constants/routes/myPages"
|
||||
import { logger } from "@scandic-hotels/common/logger"
|
||||
import {
|
||||
formatPhoneNumber,
|
||||
getDefaultCountryFromLang,
|
||||
} from "@scandic-hotels/common/utils/phone"
|
||||
import { OldDSButton as Button } from "@scandic-hotels/design-system/OldDSButton"
|
||||
import Title from "@scandic-hotels/design-system/Title"
|
||||
import { toast } from "@scandic-hotels/design-system/Toast"
|
||||
import { trpc } from "@scandic-hotels/trpc/client"
|
||||
import { langToApiLang } from "@scandic-hotels/trpc/constants/apiLang"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@/constants/languages"
|
||||
import { logout } from "@/constants/routes/handleAuth"
|
||||
|
||||
import { editProfile } from "@/actions/editProfile"
|
||||
import Dialog from "@/components/Dialog"
|
||||
import ChangeNameDisclaimer from "@/components/MyPages/Profile/ChangeNameDisclaimer"
|
||||
import usePhoneNumberParsing from "@/hooks/usePhoneNumberParsing"
|
||||
import { formatPhoneNumber } from "@/utils/phone"
|
||||
|
||||
import FormContent from "./FormContent"
|
||||
import { type EditProfileSchema, editProfileSchema } from "./schema"
|
||||
|
||||
@@ -7,6 +7,10 @@ import { FormProvider, useForm } from "react-hook-form"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { logger } from "@scandic-hotels/common/logger"
|
||||
import {
|
||||
formatPhoneNumber,
|
||||
getDefaultCountryFromLang,
|
||||
} from "@scandic-hotels/common/utils/phone"
|
||||
import { Button } from "@scandic-hotels/design-system/Button"
|
||||
import Checkbox from "@scandic-hotels/design-system/Form/Checkbox"
|
||||
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
|
||||
@@ -22,7 +26,6 @@ import {
|
||||
signUpSchema,
|
||||
} from "@scandic-hotels/trpc/routers/user/schemas"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@/constants/languages"
|
||||
import {
|
||||
membershipTermsAndConditions,
|
||||
privacyPolicy,
|
||||
@@ -34,7 +37,6 @@ import { useFormTracking } from "@/components/TrackingSDK/useFormTracking"
|
||||
import useLang from "@/hooks/useLang"
|
||||
import { getFormattedCountryList } from "@/utils/countries"
|
||||
import { getErrorMessage } from "@/utils/getErrorMessage"
|
||||
import { formatPhoneNumber } from "@/utils/phone"
|
||||
|
||||
// import { type SignUpSchema, signUpSchema } from "./schema"
|
||||
import styles from "./form.module.css"
|
||||
|
||||
@@ -4,11 +4,11 @@ import { useCallback, useEffect, useMemo } from "react"
|
||||
import { FormProvider, useForm } from "react-hook-form"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@scandic-hotels/common/utils/phone"
|
||||
import Footnote from "@scandic-hotels/design-system/Footnote"
|
||||
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
|
||||
import Phone from "@scandic-hotels/design-system/Form/Phone"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@/constants/languages"
|
||||
import { useEnterDetailsStore } from "@/stores/enter-details"
|
||||
|
||||
import SpecialRequests from "@/components/HotelReservation/EnterDetails/Details/SpecialRequests"
|
||||
|
||||
@@ -4,11 +4,11 @@ import { useCallback, useEffect } from "react"
|
||||
import { FormProvider, useForm } from "react-hook-form"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@scandic-hotels/common/utils/phone"
|
||||
import Footnote from "@scandic-hotels/design-system/Footnote"
|
||||
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
|
||||
import Phone from "@scandic-hotels/design-system/Form/Phone"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@/constants/languages"
|
||||
import { useEnterDetailsStore } from "@/stores/enter-details"
|
||||
|
||||
import SpecialRequests from "@/components/HotelReservation/EnterDetails/Details/SpecialRequests"
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import useStickyPosition from "@scandic-hotels/common/hooks/useStickyPosition"
|
||||
import { logger } from "@scandic-hotels/common/logger"
|
||||
import { trackEvent } from "@scandic-hotels/common/tracking/base"
|
||||
import { formatPhoneNumber } from "@scandic-hotels/common/utils/phone"
|
||||
import Body from "@scandic-hotels/design-system/Body"
|
||||
import { Button } from "@scandic-hotels/design-system/Button"
|
||||
import Checkbox from "@scandic-hotels/design-system/Form/Checkbox"
|
||||
@@ -34,7 +35,6 @@ import PaymentOption from "@/components/HotelReservation/PaymentOption"
|
||||
import { useAvailablePaymentOptions } from "@/hooks/booking/useAvailablePaymentOptions"
|
||||
import { useHandleBookingStatus } from "@/hooks/booking/useHandleBookingStatus"
|
||||
import useLang from "@/hooks/useLang"
|
||||
import { formatPhoneNumber } from "@/utils/phone"
|
||||
import { trackPaymentEvent } from "@/utils/tracking"
|
||||
import { trackGlaSaveCardAttempt } from "@/utils/tracking/myStay"
|
||||
|
||||
|
||||
@@ -3,12 +3,11 @@ import { useEffect } from "react"
|
||||
import { useFormContext } from "react-hook-form"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@scandic-hotels/common/utils/phone"
|
||||
import Body from "@scandic-hotels/design-system/Body"
|
||||
import CountrySelect from "@scandic-hotels/design-system/Form/Country"
|
||||
import Phone from "@scandic-hotels/design-system/Form/Phone"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@/constants/languages"
|
||||
|
||||
import Input from "@/components/TempDesignSystem/Form/Input"
|
||||
import useLang from "@/hooks/useLang"
|
||||
import { getFormattedCountryList } from "@/utils/countries"
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import { ApiLang } from "@scandic-hotels/trpc/constants/apiLang"
|
||||
|
||||
import type { CountryCode } from "libphonenumber-js"
|
||||
|
||||
export const languages: Record<Lang, string> = {
|
||||
[Lang.da]: "Dansk",
|
||||
[Lang.de]: "Deutsch",
|
||||
@@ -81,15 +79,3 @@ export function getLocalizedLanguageOptions(currentLang: Lang) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function getDefaultCountryFromLang(lang: Lang): Lowercase<CountryCode> {
|
||||
const countryMap: Record<Lang, Lowercase<CountryCode>> = {
|
||||
sv: "se",
|
||||
da: "dk",
|
||||
fi: "fi",
|
||||
no: "no",
|
||||
de: "de",
|
||||
en: "se", // Default to Sweden for English
|
||||
}
|
||||
return countryMap[lang] || "se"
|
||||
}
|
||||
|
||||
@@ -4,8 +4,7 @@ import { useContext } from "react"
|
||||
import { create, useStore } from "zustand"
|
||||
|
||||
import { dt } from "@scandic-hotels/common/dt"
|
||||
|
||||
import { getDefaultCountryFromLang } from "@/constants/languages"
|
||||
import { getDefaultCountryFromLang } from "@scandic-hotels/common/utils/phone"
|
||||
|
||||
import { DetailsContext } from "@/contexts/Details"
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import parsePhoneNumberFromString, { type CountryCode } from "libphonenumber-js"
|
||||
|
||||
import { logger } from "@scandic-hotels/common/logger"
|
||||
|
||||
export function formatPhoneNumber(
|
||||
phoneNumber: string,
|
||||
phoneNumberCC?: string | null
|
||||
): string {
|
||||
if (!phoneNumber) return ""
|
||||
|
||||
const normalized = phoneNumber.trim().replaceAll(/-|\s/g, "")
|
||||
|
||||
const parsedPhonenumber = phoneNumberCC?.length
|
||||
? parsePhoneNumberFromString(
|
||||
normalized,
|
||||
phoneNumberCC.toUpperCase() as CountryCode
|
||||
)
|
||||
: parsePhoneNumberFromString(normalized)
|
||||
|
||||
if (!parsedPhonenumber?.isValid()) {
|
||||
logger.warn(
|
||||
`Invalid phone number: ${phoneNumber} with country code: ${phoneNumberCC}`,
|
||||
parsedPhonenumber
|
||||
)
|
||||
}
|
||||
|
||||
return parsedPhonenumber?.number || normalized
|
||||
}
|
||||
Reference in New Issue
Block a user