@graphcommerce/magento-cart-payment-method 2.106.0 → 2.106.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- import { AvailablePaymentMethod } from '@graphcommerce/graphql'
2
1
  import { ButtonProps } from '@graphcommerce/next-ui/Button'
3
2
  import { UseFormComposeOptions } from '@graphcommerce/react-hook-form'
4
3
  import React from 'react'
@@ -7,7 +6,7 @@ import { PaymentMethodContextFragment } from './PaymentMethodContext.gql'
7
6
  import { SelectedPaymentMethodFragment } from './SelectedPaymentMethod/SelectedPaymentMethod.gql'
8
7
 
9
8
  export type PaymentMethod = Partial<AvailablePaymentMethodFragment> &
10
- Pick<AvailablePaymentMethod, 'code' | 'title'> & {
9
+ Pick<AvailablePaymentMethodFragment, 'code' | 'title'> & {
11
10
  child: string
12
11
  preferred?: boolean
13
12
  selected?: SelectedPaymentMethodFragment
@@ -33,6 +32,7 @@ export interface PaymentModule {
33
32
  PaymentButton?: React.VFC<PaymentButtonProps>
34
33
  PaymentToggle?: React.VFC<PaymentToggleProps>
35
34
  expandMethods?: ExpandPaymentMethods
35
+ PaymentHandler?: React.VFC
36
36
  }
37
37
 
38
38
  export type PaymentMethodModules = { [code: string]: PaymentModule }
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.106.4](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/magento-cart-payment-method@2.106.3...@graphcommerce/magento-cart-payment-method@2.106.4) (2021-10-07)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * make sure if no payment method is filled in we get an error shown ([a203e57](https://github.com/ho-nl/m2-pwa/commit/a203e570caad0732427a178e8e8b10b4a15d676b))
12
+
13
+
14
+
15
+
16
+
6
17
  # [2.106.0](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/magento-cart-payment-method@2.105.11...@graphcommerce/magento-cart-payment-method@2.106.0) (2021-10-04)
7
18
 
8
19
 
@@ -1,6 +1,7 @@
1
- import { ApolloCartErrorAlert } from '@graphcommerce/magento-cart'
2
- import { Button, ButtonProps, FormRow } from '@graphcommerce/next-ui'
1
+ import { ApolloCartErrorAlert, ApolloCartErrorFullPage } from '@graphcommerce/magento-cart'
2
+ import { ApolloErrorFullPage, Button, ButtonProps, FormRow } from '@graphcommerce/next-ui'
3
3
  import { ComposedSubmit, ComposedSubmitRenderComponentProps } from '@graphcommerce/react-hook-form'
4
+ import { Dialog } from '@material-ui/core'
4
5
  import React from 'react'
5
6
  import { usePaymentMethodContext } from '../PaymentMethodContext/PaymentMethodContext'
6
7
 
@@ -52,6 +53,7 @@ export default function PaymentMethodButton(props: PaymentMethodButtonProps) {
52
53
  return (
53
54
  <ComposedSubmit
54
55
  render={({ submit, buttonState, error }) => {
56
+ const errorVal = buttonState.isSubmitting ? undefined : error
55
57
  const button = (
56
58
  <PaymentMethodButtonRenderer
57
59
  buttonProps={buttonProps}
@@ -65,10 +67,7 @@ export default function PaymentMethodButton(props: PaymentMethodButtonProps) {
65
67
  ) : (
66
68
  <>
67
69
  <FormRow>{button}</FormRow>
68
- <ApolloCartErrorAlert
69
- key='error'
70
- error={buttonState.isSubmitting ? undefined : error}
71
- />
70
+ <ApolloCartErrorAlert key='error' error={errorVal} />
72
71
  </>
73
72
  )
74
73
  }}
@@ -26,6 +26,7 @@ export default function PaymentMethodContextProvider(props: PaymentMethodContext
26
26
  const { modules, children } = props
27
27
 
28
28
  const context = useCartQuery(GetPaymentMethodContextDocument)
29
+
29
30
  const cartContext = context?.data?.cart
30
31
  const [methods, setMethods] = useState<PaymentMethod[]>([])
31
32
  const [selectedMethod, setSelectedMethod] = useState<PaymentMethod>()
@@ -51,7 +52,12 @@ export default function PaymentMethodContextProvider(props: PaymentMethodContext
51
52
  : Promise.resolve([]),
52
53
  ) ?? []
53
54
 
54
- setMethods((await Promise.all(promises)).flat(1).sort((a) => (a.preferred ? 1 : 0)))
55
+ const loaded = (await Promise.all(promises)).flat(1).sort((a) => (a.preferred ? 1 : 0))
56
+ const sortedMethods = loaded.sort((a, b) =>
57
+ !modules?.[a?.code] ? 0 : !modules?.[b?.code] ? -1 : 1,
58
+ )
59
+
60
+ setMethods(sortedMethods)
55
61
  })()
56
62
  }, [cartContext, modules])
57
63
 
@@ -66,6 +72,11 @@ export default function PaymentMethodContextProvider(props: PaymentMethodContext
66
72
  setSelectedModule,
67
73
  }}
68
74
  >
75
+ {Object.entries(modules).map(([method, module]) => {
76
+ const PaymentHandler = module.PaymentHandler
77
+ if (!PaymentHandler) return null
78
+ return <PaymentHandler key={method} />
79
+ })}
69
80
  {children}
70
81
  </paymentMethodContext.Provider>
71
82
  )
@@ -21,7 +21,7 @@ export default function PaymentMethodPlaceOrderNoop(props: PaymentPlaceOrderProp
21
21
 
22
22
  useEffect(() => {
23
23
  if (!data?.placeOrder?.order || error || !cartId) return
24
- clearCurrentCartId?.()
24
+ clearCurrentCartId()
25
25
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
26
26
  router.push({ pathname: '/checkout/success', query: { cartId } })
27
27
  }, [cartId, clearCurrentCartId, data?.placeOrder?.order, error, router])
@@ -8,14 +8,20 @@ import {
8
8
  SvgImage,
9
9
  ToggleButton,
10
10
  } from '@graphcommerce/next-ui'
11
- import { Controller, useForm, useFormPersist } from '@graphcommerce/react-hook-form'
12
- import { FormControl, makeStyles, Theme } from '@material-ui/core'
11
+ import {
12
+ Controller,
13
+ useForm,
14
+ useFormCompose,
15
+ UseFormComposeOptions,
16
+ useFormPersist,
17
+ } from '@graphcommerce/react-hook-form'
18
+ import { FormControl, FormHelperText, makeStyles, Theme } from '@material-ui/core'
13
19
  import clsx from 'clsx'
14
20
  import { m } from 'framer-motion'
15
21
  import React, { useEffect } from 'react'
16
22
  import { usePaymentMethodContext } from '../PaymentMethodContext/PaymentMethodContext'
17
23
 
18
- export type PaymentMethodTogglesProps = Record<string, unknown>
24
+ export type PaymentMethodTogglesProps = Pick<UseFormComposeOptions, 'step'>
19
25
 
20
26
  // function Content(props: PaymentMethod) {
21
27
  // const { code } = props
@@ -101,6 +107,7 @@ const useStyles = makeStyles(
101
107
  )
102
108
 
103
109
  export default function PaymentMethodToggles(props: PaymentMethodTogglesProps) {
110
+ const { step } = props
104
111
  const { methods, selectedMethod, setSelectedMethod, setSelectedModule, modules } =
105
112
  usePaymentMethodContext()
106
113
 
@@ -112,9 +119,12 @@ export default function PaymentMethodToggles(props: PaymentMethodTogglesProps) {
112
119
  })
113
120
  useFormPersist({ form, name: 'PaymentMethodToggle' })
114
121
 
115
- const { control, handleSubmit, watch, register, setValue } = form
122
+ const { control, handleSubmit, watch, register, setValue, formState } = form
123
+
116
124
  const submitHandler = handleSubmit(() => {})
117
125
 
126
+ useFormCompose({ form, step, submit: submitHandler, key: 'PaymentMethodToggles' })
127
+
118
128
  const paymentMethod = watch('paymentMethod')
119
129
  useEffect(() => {
120
130
  const [code, child] = paymentMethod?.split('___') ?? ['']
@@ -132,15 +142,11 @@ export default function PaymentMethodToggles(props: PaymentMethodTogglesProps) {
132
142
 
133
143
  if (!methods || methods.length < 1) return <></>
134
144
 
135
- const sortedMethods = [...methods].sort((a, b) =>
136
- !modules?.[a?.code] ? 0 : !modules?.[b?.code] ? -1 : 1,
137
- )
138
-
139
145
  return (
140
146
  <Form onSubmit={submitHandler} noValidate classes={{ root: classes.formRoot }}>
141
147
  <input type='hidden' {...register('code', { required: true })} required />
142
148
  <FormRow className={classes.root}>
143
- <ScrollerProvider scrollSnapType='none'>
149
+ <ScrollerProvider scrollSnapAlign='center'>
144
150
  <m.div className={classes.buttonContainer}>
145
151
  <ScrollerButton
146
152
  direction='left'
@@ -159,10 +165,9 @@ export default function PaymentMethodToggles(props: PaymentMethodTogglesProps) {
159
165
  name='paymentMethod'
160
166
  rules={{ required: 'Please select a payment method' }}
161
167
  render={({ field: { onChange, value, name, ref, onBlur } }) => (
162
- <Scroller className={classes.scrollerRoot} hideScrollbar>
163
- {sortedMethods?.map((pm) => {
168
+ <Scroller className={classes.scrollerRoot} hideScrollbar tabIndex={0}>
169
+ {methods?.map((pm) => {
164
170
  const buttonValue = `${pm.code}___${pm.child}`
165
-
166
171
  return (
167
172
  <ToggleButton
168
173
  name={name}
@@ -189,6 +194,12 @@ export default function PaymentMethodToggles(props: PaymentMethodTogglesProps) {
189
194
  </Scroller>
190
195
  )}
191
196
  />
197
+
198
+ {formState.errors.paymentMethod?.message && (
199
+ <FormHelperText id='my-helper-text' error>
200
+ {formState.errors.paymentMethod.message}
201
+ </FormHelperText>
202
+ )}
192
203
  </FormControl>
193
204
 
194
205
  <m.div className={clsx(classes.buttonContainer, classes.buttonContainerRight)}>
@@ -0,0 +1,7 @@
1
+ query UseCartLock($cartId: String!) {
2
+ cart(cart_id: $cartId) @client {
3
+ __typename
4
+ id
5
+ locked
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ extend type Mutation {
2
+ lockCart(lock: Boolean): Boolean
3
+ }
4
+
5
+ extend type Cart {
6
+ locked: Boolean!
7
+ }
package/hooks/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './useCartLock'
@@ -0,0 +1,33 @@
1
+ import { useApolloClient } from '@apollo/client'
2
+ import { useCartQuery, useCurrentCartId } from '@graphcommerce/magento-cart'
3
+ import { useCallback } from 'react'
4
+ import { UseCartLockDocument } from './UseCartLock.gql'
5
+
6
+ /**
7
+ * Locking a cart might is usefull in the following cases: We want to disable cart modifications
8
+ * while still keeping the cart active on the website.
9
+ *
10
+ * Todo: Block all operations on the cart while the cart is being blocked.
11
+ */
12
+ export function useCartLock() {
13
+ const { cache } = useApolloClient()
14
+ const cartId = useCurrentCartId()
15
+
16
+ const locked = useCartQuery(UseCartLockDocument).data?.cart?.locked ?? false
17
+
18
+ const lock = useCallback(
19
+ (locking: boolean) => {
20
+ if (!cartId) return
21
+
22
+ cache.writeQuery({
23
+ query: UseCartLockDocument,
24
+ data: { cart: { __typename: 'Cart', id: cartId, locked: locking } },
25
+ variables: { cartId },
26
+ broadcast: true,
27
+ })
28
+ },
29
+ [cache, cartId],
30
+ )
31
+
32
+ return { locked, lock }
33
+ }
package/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './Api/PaymentMethod'
2
+ export * from './hooks'
2
3
 
3
4
  export * from './PaymentMethodContext/PaymentMethodContext'
4
5
  export { default as PaymentMethodContextProvider } from './PaymentMethodContext/PaymentMethodContext'
@@ -15,4 +16,4 @@ export { default as PaymentMethodPlaceOrder } from './PaymentMethodPlaceOrder/Pa
15
16
  export { default as PaymentMethodPlaceOrderNoop } from './PaymentMethodPlaceOrderNoop/PaymentMethodPlaceOrderNoop'
16
17
 
17
18
  export * from './PaymentMethodToggles/PaymentMethodToggles'
18
- export { default as PaymentMethodToggle } from './PaymentMethodToggles/PaymentMethodToggles'
19
+ export { default as PaymentMethodToggles } from './PaymentMethodToggles/PaymentMethodToggles'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphcommerce/magento-cart-payment-method",
3
- "version": "2.106.0",
3
+ "version": "2.106.4",
4
4
  "sideEffects": false,
5
5
  "engines": {
6
6
  "node": "14.x"
@@ -17,20 +17,20 @@
17
17
  },
18
18
  "devDependencies": {
19
19
  "@graphcommerce/browserslist-config-pwa": "^3.0.1",
20
- "@graphcommerce/eslint-config-pwa": "^3.0.4",
20
+ "@graphcommerce/eslint-config-pwa": "^3.0.5",
21
21
  "@graphcommerce/prettier-config-pwa": "^3.0.2",
22
22
  "@graphcommerce/typescript-config-pwa": "^3.1.0",
23
23
  "@playwright/test": "^1.15.0"
24
24
  },
25
25
  "dependencies": {
26
- "@apollo/client": "^3.3.21",
27
- "@graphcommerce/framer-scroller": "^0.2.5",
28
- "@graphcommerce/graphql": "^2.103.4",
29
- "@graphcommerce/image": "^2.104.5",
30
- "@graphcommerce/magento-cart": "^3.0.13",
31
- "@graphcommerce/magento-store": "^3.0.11",
32
- "@graphcommerce/next-ui": "^3.1.3",
33
- "@graphcommerce/react-hook-form": "^2.102.4",
26
+ "@apollo/client": "^3.4.16",
27
+ "@graphcommerce/framer-scroller": "^0.2.7",
28
+ "@graphcommerce/graphql": "^2.103.5",
29
+ "@graphcommerce/image": "^2.104.6",
30
+ "@graphcommerce/magento-cart": "^3.1.3",
31
+ "@graphcommerce/magento-store": "^3.0.14",
32
+ "@graphcommerce/next-ui": "^3.1.6",
33
+ "@graphcommerce/react-hook-form": "^2.102.6",
34
34
  "@graphql-typed-document-node/core": "^3.1.0",
35
35
  "@material-ui/core": "^4.12.3",
36
36
  "@material-ui/lab": "^4.0.0-alpha.60",
@@ -41,5 +41,5 @@
41
41
  "react-dom": "^17.0.1",
42
42
  "type-fest": "^2.3.4"
43
43
  },
44
- "gitHead": "148ed6eb34bf3b3da72c1791c4195d8d141bc6e6"
44
+ "gitHead": "717bd3c1658e421c247c1f8b210935cdb7780197"
45
45
  }