Merged in chore/move-enter-details (pull request #2778)
Chore/move enter details Approved-by: Anton Gunnarsson
This commit is contained in:
28
packages/common/hooks/useLazyPathname.ts
Normal file
28
packages/common/hooks/useLazyPathname.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
"use client"
|
||||
|
||||
import { usePathname, useSearchParams } from "next/navigation"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
/*** This hook is used to get the current pathname (as reflected in window.location.href) of the page. During ssr, the value from usePathname()
|
||||
* is the value return from NextResponse.rewrite() (e.g. the path from the app directory) instead of the actual pathname from the URL.
|
||||
*/
|
||||
export function useLazyPathname({ includeSearchParams = false } = {}) {
|
||||
const pathName = usePathname()
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
const [updatedPathName, setUpdatedPathName] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!includeSearchParams) {
|
||||
setUpdatedPathName(pathName)
|
||||
} else {
|
||||
const updatedPathname = searchParams.size
|
||||
? `${pathName}?${searchParams.toString()}`
|
||||
: pathName
|
||||
|
||||
setUpdatedPathName(updatedPathname)
|
||||
}
|
||||
}, [pathName, searchParams, includeSearchParams])
|
||||
|
||||
return updatedPathName
|
||||
}
|
||||
23
packages/common/hooks/usePhoneNumberParsing.ts
Normal file
23
packages/common/hooks/usePhoneNumberParsing.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
"use client"
|
||||
|
||||
import parsePhoneNumberFromString from "libphonenumber-js"
|
||||
|
||||
export function usePhoneNumberParsing(
|
||||
initialPhoneNumber?: string,
|
||||
initialPhoneNumberCC?: string
|
||||
) {
|
||||
const parsedInitialPhoneNumber = initialPhoneNumber
|
||||
? parsePhoneNumberFromString(initialPhoneNumber)
|
||||
: undefined
|
||||
|
||||
let phoneNumberCC = initialPhoneNumberCC
|
||||
if (parsedInitialPhoneNumber && !phoneNumberCC) {
|
||||
phoneNumberCC = parsedInitialPhoneNumber.country ?? ""
|
||||
}
|
||||
|
||||
const phoneNumber = parsedInitialPhoneNumber?.isValid()
|
||||
? parsedInitialPhoneNumber.nationalNumber
|
||||
: initialPhoneNumber
|
||||
|
||||
return { phoneNumber, phoneNumberCC: phoneNumberCC?.toLowerCase() }
|
||||
}
|
||||
Reference in New Issue
Block a user