feat(BOOK-212): Refactored LoginButton and added successful tracking functionality
Approved-by: Linus Flood
This commit is contained in:
@@ -1,31 +1,34 @@
|
||||
'use client'
|
||||
|
||||
import { login } from '@scandic-hotels/common/constants/routes/handleAuth'
|
||||
import Link, { type LinkProps } from '../OldDSLink'
|
||||
|
||||
import type { Lang } from '@scandic-hotels/common/constants/language'
|
||||
import type { PropsWithChildren } from 'react'
|
||||
import ButtonLink, { ButtonLinkProps } from '../ButtonLink'
|
||||
|
||||
interface LoginButtonProps
|
||||
extends React.PropsWithChildren<Omit<ButtonLinkProps, 'href'>> {
|
||||
lang: Lang
|
||||
redirectTo: string | null
|
||||
loginPosition: string
|
||||
}
|
||||
|
||||
export function LoginButton({
|
||||
lang,
|
||||
redirectTo,
|
||||
trackingId,
|
||||
children,
|
||||
loginPosition,
|
||||
...props
|
||||
}: PropsWithChildren<
|
||||
{
|
||||
lang: Lang
|
||||
redirectTo: string | null
|
||||
trackingId: string
|
||||
} & Omit<LinkProps, 'href'>
|
||||
>) {
|
||||
const href = redirectTo
|
||||
? `${login[lang]}?redirectTo=${encodeURIComponent(redirectTo)}`
|
||||
: login[lang]
|
||||
}: LoginButtonProps) {
|
||||
let href = login[lang]
|
||||
|
||||
return (
|
||||
<Link id={trackingId} href={href} prefetch={false} {...props}>
|
||||
{children}
|
||||
</Link>
|
||||
)
|
||||
if (redirectTo) {
|
||||
const [pathname, existingQuery] = redirectTo.split('?')
|
||||
const searchParams = new URLSearchParams(existingQuery)
|
||||
|
||||
searchParams.set('loginPosition', loginPosition)
|
||||
const redirectUrl = `${pathname}?${searchParams.toString()}`
|
||||
|
||||
href = `${href}?redirectTo=${encodeURIComponent(redirectUrl)}`
|
||||
}
|
||||
|
||||
return <ButtonLink href={href} prefetch={false} {...props} />
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user