Skip to content

Commit

Permalink
Added ButtonItem
Browse files Browse the repository at this point in the history
  • Loading branch information
paales committed Jan 29, 2024
1 parent 43f5039 commit ea1189f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { productLink } from '@graphcommerce/magento-product'
import { Button, ButtonProps } from '@mui/material'
import { extractAdvancedProps } from '../../utils'
import { ButtonItemContentType } from './types'
import { useQuery } from '@graphcommerce/graphql'
import { StoreConfigDocument } from '@graphcommerce/magento-store'

/**
* Page Builder ButtonItem component.
Expand All @@ -13,6 +15,8 @@ import { ButtonItemContentType } from './types'
export const ButtonItem: ButtonItemContentType['component'] = (props) => {
const [cssProps, cssClasses, additional] = extractAdvancedProps(props)

const linkBase = useQuery(StoreConfigDocument).data?.storeConfig?.secure_base_link_url

const { buttonType, link, openInNewTab = false, buttonText, linkType } = additional

const buttonProps: ButtonProps<'a'> = {
Expand All @@ -23,9 +27,11 @@ export const ButtonItem: ButtonItemContentType['component'] = (props) => {

if (!link) return null

const relativeUrl = linkBase ? link.replace(linkBase, '').trim() : link

return (
<Button
href={linkType === 'product' ? productLink({ url_key: link }) : link}
href={linkType === 'product' ? productLink({ url_key: relativeUrl }) : relativeUrl}
// sx={sx}
{...buttonProps}
target={openInNewTab ? '_blank' : undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { productLink } from '@graphcommerce/magento-product'
import { Button, ButtonProps, SxProps, Theme } from '@mui/material'
import { ButtonLinkProps } from './getButtonLinkProps'
import { useQuery } from '@graphcommerce/graphql'
import { StoreConfigDocument } from '@graphcommerce/magento-store'

type ButtonLinkComponentProps = ButtonLinkProps & {
sx?: SxProps<Theme>
Expand All @@ -12,20 +14,24 @@ function isButton(props: any): props is ButtonLinkComponentProps {
}

export function ButtonLink(props: ButtonLinkComponentProps) {
if (!isButton(props)) return null
// if (!isButton(props)) return null

const { buttonType, link, openInNewTab = false, buttonText, linkType, sx } = props

const linkBase = useQuery(StoreConfigDocument).data?.storeConfig?.secure_base_link_url

const buttonProps: ButtonProps<'a'> = {
variant: buttonType === 'link' ? 'text' : 'pill',
color: buttonType === 'primary' ? 'primary' : 'secondary',
}

if (!link) return null

const relativeUrl = linkBase ? link.replace(linkBase, '').trim() : link

return (
<Button
href={linkType === 'product' ? productLink({ url_key: link }) : link}
href={linkType === 'product' ? productLink({ url_key: relativeUrl }) : relativeUrl}
sx={sx}
{...buttonProps}
target={openInNewTab ? '_blank' : undefined}
Expand Down

0 comments on commit ea1189f

Please sign in to comment.