Skip to content

Commit

Permalink
feat(GCOM-1446): cleanup, replace FullPageMessage with ApolloCartErro…
Browse files Browse the repository at this point in the history
…rFullPage error handling
  • Loading branch information
Giovanni-Schroevers committed Aug 23, 2024
1 parent a946a0b commit b6b7e7f
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 79 deletions.
4 changes: 2 additions & 2 deletions examples/magento-graphcms/components/Layout/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type FooterProps = FooterQueryFragment

export function Footer(props: FooterProps) {
const { footer } = props
const enabled = useCheckoutGuestEnabled()
const cartEnabled = useCheckoutGuestEnabled()

return (
<FooterBase
Expand Down Expand Up @@ -48,7 +48,7 @@ export function Footer(props: FooterProps) {
{link.title}
</Link>
))}
{import.meta.graphCommerce.magentoVersion >= 247 && enabled && (
{import.meta.graphCommerce.magentoVersion >= 247 && cartEnabled && (
<Link href='/guest/orderstatus' color='textPrimary' underline='always'>
<Trans>Order status</Trans>
</Link>
Expand Down
1 change: 0 additions & 1 deletion examples/magento-graphcms/pages/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ function CartPage() {
)}
</LayoutTitle>
</LayoutOverlayHeader>

<WaitForQueries
waitFor={cart}
fallback={
Expand Down
4 changes: 0 additions & 4 deletions examples/magento-graphcms/pages/checkout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
EmptyCart,
getCheckoutIsDisabled,
useCartQuery,
useCheckoutShouldLoginToContinue,
} from '@graphcommerce/magento-cart'
import { ShippingPageDocument } from '@graphcommerce/magento-cart-checkout'
import { EmailForm } from '@graphcommerce/magento-cart-email'
Expand All @@ -23,7 +22,6 @@ import {
} from '@graphcommerce/magento-cart-shipping-address'
import { ShippingMethodForm } from '@graphcommerce/magento-cart-shipping-method'
import { CustomerDocument, useCustomerQuery } from '@graphcommerce/magento-customer'
import { UnauthenticatedFullPageMessage } from '@graphcommerce/magento-customer/components/WaitForCustomer/UnauthenticatedFullPageMessage'
import { PageMeta, StoreConfigDocument } from '@graphcommerce/magento-store'
import {
FormActions,
Expand Down Expand Up @@ -54,8 +52,6 @@ function ShippingPage() {
typeof shippingPage.data?.cart !== 'undefined' &&
(shippingPage.data.cart?.items?.length ?? 0) > 0

if (useCheckoutShouldLoginToContinue()) return <UnauthenticatedFullPageMessage />

return (
<>
<PageMeta title={i18n._(/* i18n */ 'Shipping')} metaRobots={['noindex']} />
Expand Down
4 changes: 0 additions & 4 deletions examples/magento-graphcms/pages/checkout/payment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
EmptyCart,
getCheckoutIsDisabled,
useCartQuery,
useCheckoutShouldLoginToContinue,
} from '@graphcommerce/magento-cart'
import { BillingPageDocument } from '@graphcommerce/magento-cart-checkout'
import { CouponAccordion } from '@graphcommerce/magento-cart-coupon'
Expand All @@ -20,7 +19,6 @@ import {
PaymentMethodActionCardListForm,
PaymentMethodContextProvider,
} from '@graphcommerce/magento-cart-payment-method'
import { UnauthenticatedFullPageMessage } from '@graphcommerce/magento-customer/components/WaitForCustomer/UnauthenticatedFullPageMessage'
import { SubscribeToNewsletter } from '@graphcommerce/magento-newsletter'
import { PageMeta, StoreConfigDocument } from '@graphcommerce/magento-store'
import {
Expand Down Expand Up @@ -49,8 +47,6 @@ function PaymentPage() {
const cartExists =
typeof billingPage.data?.cart !== 'undefined' && (billingPage.data.cart?.items?.length ?? 0) > 0

if (useCheckoutShouldLoginToContinue()) return <UnauthenticatedFullPageMessage />

return (
<ComposedForm>
<PageMeta title={i18n._(/* i18n */ 'Payment')} metaRobots={['noindex']} />
Expand Down
4 changes: 1 addition & 3 deletions packages/magento-cart/hooks/useCartQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ export function useCartQuery<Q, V extends { cartId: string; [index: string]: unk
skip: queryOptions.skip || !cartId || shouldLoginToContinue,
})

// Heeft dit voorkeur over het openen van de login overlay?
if (shouldLoginToContinue && !queryOptions?.skip) {
console.log(document)
return {
...query,
error: new ApolloError({
graphQLErrors: [
new GraphQLError('oepsie', {
new GraphQLError('Action can not be performed by the current user', {
extensions: { category: 'graphql-authorization' },
}),
],
Expand Down
5 changes: 2 additions & 3 deletions packages/magento-cart/hooks/useFormGqlMutationCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
UseFormGraphQlOptions,
} from '@graphcommerce/react-hook-form'
import { GraphQLError, Kind } from 'graphql'
import { isProtectedCartOperation } from '../link/isProtectedCartOperation'
import { CurrentCartIdDocument } from './CurrentCartId.gql'
import { useCartIdCreate } from './useCartIdCreate'
import { useCartShouldLoginToContinue } from './useCartPermissions'
import { isProtectedCartOperation } from '../link/isProtectedCartOperation'

export function useFormGqlMutationCart<
Q extends Record<string, unknown>,
Expand Down Expand Up @@ -57,12 +57,11 @@ export function useFormGqlMutationCart<
)

if (shouldLoginToContinue && result.formState.isSubmitted && shouldBlockOperation) {
console.log(document)
return {
...result,
error: new ApolloError({
graphQLErrors: [
new GraphQLError('oepsie', {
new GraphQLError('Action can not be performed by the current user', {
extensions: { category: 'graphql-authorization' },
}),
],
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { mergeErrors, WaitForQueries, WaitForQueriesProps } from '@graphcommerce/ecommerce-ui'
import { FullPageMessage, FullPageMessageProps } from '@graphcommerce/next-ui'
import { FullPageMessage, FullPageMessageProps, iconPerson, IconSvg } from '@graphcommerce/next-ui'
import { Trans } from '@lingui/react'
import { CircularProgress } from '@mui/material'
import { Button, CircularProgress } from '@mui/material'
import React from 'react'
import { useCustomerSession } from '../../hooks/useCustomerSession'
import { ApolloCustomerErrorFullPage } from '../ApolloCustomerError/ApolloCustomerErrorFullPage'
import { UnauthenticatedFullPageMessage } from './UnauthenticatedFullPageMessage'

type WaitForCustomerProps = Omit<WaitForQueriesProps, 'fallback' | 'waitFor'> &
Pick<FullPageMessageProps, 'disableMargin'> & {
Expand Down Expand Up @@ -65,7 +64,22 @@ export function WaitForCustomer(props: WaitForCustomerProps) {
}
>
{!session.loggedIn &&
(unauthenticated ?? <UnauthenticatedFullPageMessage disableMargin={disableMargin} />)}
(unauthenticated ?? (
<FullPageMessage
icon={<IconSvg src={iconPerson} size='xxl' />}
title={<Trans id='You must sign in to continue' />}
button={
<Button href='/account/signin' variant='pill' color='secondary' size='large'>
{!session.valid ? (
<Trans id='Sign in' />
) : (
<Trans id='Sign in or create an account!' />
)}
</Button>
}
disableMargin={disableMargin}
/>
))}
{session.loggedIn && error && <ApolloCustomerErrorFullPage error={error} />}
{session.loggedIn && !error && children}
</WaitForQueries>
Expand Down
9 changes: 3 additions & 6 deletions packages/magento-customer/link/customerLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { NextRouter } from 'next/router'
import { signOut } from '../components/SignOutForm/signOut'
import { CustomerTokenDocument } from '../hooks'

export type PushRouter = Pick<NextRouter, 'push' | 'events' | 'locale'>
export type PushRouter = Pick<NextRouter, 'push' | 'events'>

declare module '@apollo/client' {
interface DefaultContext {
Expand Down Expand Up @@ -128,8 +128,5 @@ const customerErrorLink = (router: PushRouter) =>
})
})

export const customerLink = (router: PushRouter) => {
const links = [addTokenHeader, customerErrorLink(router)]

return ApolloLink.from(links)
}
export const customerLink = (router: PushRouter) =>
ApolloLink.from([addTokenHeader, customerErrorLink(router)])
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function GraphQLProvider(props: PluginProps<GraphQLProviderProps>) {
const push = useEventCallback<NextRouter['push']>((...args) => router.push(...args))

const customerLinkMemo = useMemo(
() => customerLink({ push, events: router.events, locale: router.locale }),
[push, router.events, router.locale],
() => customerLink({ push, events: router.events }),
[push, router.events],
)

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/magento-customer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/node_modules", "**/.*/"],
"include": ["**/*.ts", "**/*.tsx", "../magento-cart/link/isProtectedCartOperation.ts"],
"include": ["**/*.ts", "**/*.tsx"],
"extends": "@graphcommerce/typescript-config-pwa/nextjs.json"
}
2 changes: 1 addition & 1 deletion packages/magento-graphql/graphqlErrorByCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function graphqlErrorByCategory(
props: GraphQLErrorByCategoryProps | GraphQLErrorByCategoryPropsNoExtract,
): [ApolloError | undefined, GraphQLError | undefined] {
const { category, error, extract = true, mask } = props

if (!error) return [error, undefined]

const newError = new ApolloError({
Expand All @@ -47,7 +48,6 @@ export function graphqlErrorByCategory(

const graphqlError = error.graphQLErrors.find((err) => err?.extensions?.category === category)
if (mask && graphqlError) {
if (graphqlError.extensions?.category) graphqlError.extensions.category = 'masked'
graphqlError.message = mask
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useCartEnabled, useCartShouldLoginToContinue } from '@graphcommerce/magento-cart'
import { useCartEnabled } from '@graphcommerce/magento-cart'
import { Fab, FabProps, iconShoppingBag, iconCheckmark } from '@graphcommerce/next-ui'
import { i18n } from '@lingui/core'
import { t } from '@lingui/macro'
import { SxProps, Theme } from '@mui/material'
import { useRouter } from 'next/router'
import {
useAddProductsToCartAction,
UseAddProductsToCartActionProps,
Expand All @@ -19,27 +17,11 @@ export function AddProductsToCartFab(props: AddProductsToCartFabProps) {
const { icon = iconShoppingBag, product, sku, ...rest } = props
const { showSuccess, ...action } = useAddProductsToCartAction(props)

const router = useRouter()
const cartEnabled = useCartEnabled()
const shouldLoginToContinue = useCartShouldLoginToContinue()

if (!cartEnabled) return null

return shouldLoginToContinue ? (
<Fab
icon={showSuccess && !action.loading ? iconCheckmark : icon}
aria-label={i18n._(/* i18n*/ `Add to Cart`)}
onClick={async (e) => {
e.preventDefault()
e.stopPropagation()
await router.push('/account/signin')
}}
onMouseDown={(e) => {
e.preventDefault()
e.stopPropagation()
}}
/>
) : (
return (
<Fab
type='submit'
{...rest}
Expand Down
1 change: 0 additions & 1 deletion packagesDev/next-config/src/withGraphCommerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function domains(config: GraphCommerceConfig): DomainLocale[] {
*/
export function withGraphCommerce(nextConfig: NextConfig, cwd: string): NextConfig {
graphcommerceConfig ??= loadConfig(cwd)

const importMetaPaths = configToImportMeta(graphcommerceConfig)

const { storefront } = graphcommerceConfig
Expand Down

0 comments on commit b6b7e7f

Please sign in to comment.