Skip to content

Commit

Permalink
Merge pull request #2317 from graphcommerce-org/fix/various
Browse files Browse the repository at this point in the history
Fix/various
  • Loading branch information
paales authored Jul 12, 2024
2 parents eb9a214 + 4219347 commit 216bfbe
Show file tree
Hide file tree
Showing 24 changed files with 832 additions and 87 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-ladybugs-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphcommerce/magento-customer": patch
---

useSignInForm now optionally expects an email
6 changes: 6 additions & 0 deletions .changeset/four-drinks-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphcommerce/googletagmanager": patch
"@graphcommerce/googleanalytics": patch
---

Moved import locations of Google Analytics and Google Tagmanager scripts to their recommended locations
5 changes: 5 additions & 0 deletions .changeset/violet-cherries-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/next-ui': patch
---

Added new plugin insertion points: `<DocumentHeadStart/>`, `<DocumentHeadEnd/>`, `<DocumentBodyEnd/>`, `<DocumentBodyStart/>` for the \_document.app to allow creating plugins for those locations to insert scripts etc.
8 changes: 8 additions & 0 deletions examples/magento-graphcms/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { LinguiDocumentProps, withLingui } from '@graphcommerce/lingui-next/server'
import {
DocumentBodyEnd,
DocumentBodyStart,
DocumentHeadEnd,
DocumentHeadStart,
EmotionCacheProps,
getCssFlagsInitScript,
normalizeLocale,
Expand All @@ -12,15 +16,19 @@ class Document extends NextDocument<EmotionCacheProps & LinguiDocumentProps> {
return (
<Html lang={normalizeLocale(this.props.locale)}>
<Head>
<DocumentHeadStart key='head-start' {...this.props} />
{getCssFlagsInitScript()}
{/* Inject MUI styles first to match with the prepend: true configuration. */}
<meta name='emotion-insertion-point' content='' />
{this.props.emotionStyleTags}
{this.props.linguiScriptTag}
<DocumentHeadEnd key='head-end' {...this.props} />
</Head>
<body>
<DocumentBodyStart key='body-start' {...this.props} />
<Main />
<NextScript />
<DocumentBodyEnd key='body-start' {...this.props} />
</body>
</Html>
)
Expand Down
21 changes: 0 additions & 21 deletions packages/googleanalytics/components/GoogleAnalyticsScript.tsx

This file was deleted.

1 change: 0 additions & 1 deletion packages/googleanalytics/components/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/googleanalytics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
33 changes: 23 additions & 10 deletions packages/googleanalytics/plugins/GoogleAnalyticsTag.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import type { PagesProps } from '@graphcommerce/framer-next-pages'
import type { IfConfig, PluginConfig, PluginProps } from '@graphcommerce/next-config'
import { GoogleAnalyticsScript } from '../components/GoogleAnalyticsScript'

export const component = 'FramerNextPages'
export const exported = '@graphcommerce/framer-next-pages'
export const ifConfig: IfConfig = 'googleAnalyticsId'
/* eslint-disable @next/next/no-document-import-in-page */
/* eslint-disable @next/next/next-script-for-ga */
/* eslint-disable react/no-danger */
import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
import { storefrontConfig } from '@graphcommerce/next-ui/server'
import type { DocumentProps } from 'next/document'

export const config: PluginConfig = {
type: 'component',
module: '@graphcommerce/framer-next-pages',
module: '@graphcommerce/next-ui/server',
ifConfig: 'googleAnalyticsId',
}

export function FramerNextPages(props: PluginProps<PagesProps>) {
export function DocumentHeadEnd(props: PluginProps<DocumentProps>) {
const { Prev, ...rest } = props

const id =
storefrontConfig(rest.locale)?.googleAnalyticsId ?? import.meta.graphCommerce.googleAnalyticsId

return (
<>
<GoogleAnalyticsScript />
<script async src='https://www.googletagmanager.com/gtag/js?id=G-E0275MGY12' />
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${id}');
`,
}}
/>
<Prev {...rest} />
</>
)
Expand Down

This file was deleted.

28 changes: 0 additions & 28 deletions packages/googletagmanager/components/GoogleTagManagerScript.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions packages/googletagmanager/components/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/googletagmanager/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './components'
export {}
60 changes: 50 additions & 10 deletions packages/googletagmanager/plugins/GoogleTagmanagerTag.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,62 @@
import type { PagesProps } from '@graphcommerce/framer-next-pages'
import type { IfConfig, PluginConfig, PluginProps } from '@graphcommerce/next-config'
import { GoogleTagManagerScript } from '../components/GoogleTagManagerScript'

export const component = 'FramerNextPages'
export const exported = '@graphcommerce/framer-next-pages'
export const ifConfig: IfConfig = 'googleTagmanagerId'
/* eslint-disable react/no-danger */
/* eslint-disable @next/next/next-script-for-ga */
/* eslint-disable @next/next/no-document-import-in-page */
/* eslint-disable @next/next/no-before-interactive-script-outside-document */
import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
import { storefrontConfig } from '@graphcommerce/next-ui/server'
import type { DocumentProps } from 'next/document'

export const config: PluginConfig = {
type: 'component',
module: '@graphcommerce/framer-next-pages',
module: '@graphcommerce/next-ui/server',
ifConfig: 'googleTagmanagerId',
}

export function FramerNextPages(props: PluginProps<PagesProps>) {
export function DocumentBodyStart(props: PluginProps<DocumentProps>) {
const { Prev, ...rest } = props

const id =
storefrontConfig(rest.locale)?.googleTagmanagerId ??
import.meta.graphCommerce.googleTagmanagerId

if (!id) return <Prev {...rest} />

return (
<>
<noscript>
{/* eslint-disable-next-line jsx-a11y/iframe-has-title */}
<iframe
src={`https://www.googletagmanager.com/ns.html?id=${id}`}
height='0'
width='0'
style={{ display: 'none', visibility: 'hidden' }}
/>
</noscript>
<Prev {...rest} />
</>
)
}

export function DocumentHeadEnd(props: PluginProps<DocumentProps>) {
const { Prev, ...rest } = props

const id =
storefrontConfig(rest.locale)?.googleTagmanagerId ??
import.meta.graphCommerce.googleTagmanagerId

return (
<>
<GoogleTagManagerScript />
<script
dangerouslySetInnerHTML={{
__html: `
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${id}');
`,
}}
/>
<Prev {...rest} />
</>
)
Expand Down
7 changes: 3 additions & 4 deletions packages/magento-customer/hooks/useSignInForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { signOut } from '../components/SignOutForm/signOut'
import { CustomerDocument } from './Customer.gql'

type UseSignInFormProps = {
email: string
email?: string
} & UseFormGraphQlOptions<SignInMutation, SignInMutationVariables>

/**
Expand All @@ -35,9 +35,8 @@ export function useSignInForm({ email, ...options }: UseSignInFormProps) {
*/
if (oldEmail && oldEmail !== email) signOut(client)

return options?.onBeforeSubmit
? options.onBeforeSubmit({ ...values, email })
: { ...values, email }
const newValues = email ? { ...values, email } : values
return options?.onBeforeSubmit ? options.onBeforeSubmit(newValues) : newValues
},
onComplete: (...args) => {
setCssFlag('in-context', true)
Expand Down
Loading

0 comments on commit 216bfbe

Please sign in to comment.