@graphcommerce/magento-cart-payment-method 3.2.0 → 3.3.0

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,5 +1,5 @@
1
1
  import { ApolloClient } from '@graphcommerce/graphql'
2
- import { LinkOrButtonProps } from '@graphcommerce/next-ui'
2
+ import { ActionCardProps, LinkOrButtonProps } from '@graphcommerce/next-ui'
3
3
  import { UseFormComposeOptions } from '@graphcommerce/react-hook-form'
4
4
  import React from 'react'
5
5
  import { AvailablePaymentMethodFragment } from './AvailablePaymentMethod/AvailablePaymentMethod.gql'
@@ -10,7 +10,7 @@ export type PaymentMethod = Partial<AvailablePaymentMethodFragment> &
10
10
  Pick<AvailablePaymentMethodFragment, 'code' | 'title'> & {
11
11
  child: string
12
12
  preferred?: boolean
13
- selected?: SelectedPaymentMethodFragment
13
+ selectedPm?: SelectedPaymentMethodFragment
14
14
  }
15
15
 
16
16
  export type PaymentMethodOptionsProps = Pick<UseFormComposeOptions, 'step'> & {
@@ -19,7 +19,6 @@ export type PaymentMethodOptionsProps = Pick<UseFormComposeOptions, 'step'> & {
19
19
  export type PaymentButtonProps = PaymentMethod & { buttonProps: LinkOrButtonProps }
20
20
  export type PaymentOptionsProps = PaymentMethod & PaymentMethodOptionsProps
21
21
 
22
- export type PaymentToggleProps = PaymentMethod
23
22
  export type PaymentHandlerProps = { code: string }
24
23
 
25
24
  export type ExpandPaymentMethodsContext = Partial<PaymentMethodContextFragment> & {
@@ -33,13 +32,15 @@ export type ExpandPaymentMethods = (
33
32
 
34
33
  export type PaymentPlaceOrderProps = PaymentMethod & Pick<UseFormComposeOptions, 'step'>
35
34
 
35
+ export type PaymentMethodActionCardProps = ActionCardProps & PaymentMethod
36
+
36
37
  export interface PaymentModule {
37
- PaymentOptions: React.VFC<PaymentOptionsProps>
38
- PaymentPlaceOrder: React.VFC<PaymentPlaceOrderProps>
39
- PaymentButton?: React.VFC<PaymentButtonProps>
40
- PaymentToggle?: React.VFC<PaymentToggleProps>
38
+ PaymentOptions: React.FC<PaymentOptionsProps>
39
+ PaymentPlaceOrder: React.FC<PaymentPlaceOrderProps>
40
+ PaymentButton?: React.FC<PaymentButtonProps>
41
41
  expandMethods?: ExpandPaymentMethods
42
- PaymentHandler?: React.VFC<PaymentHandlerProps>
42
+ PaymentHandler?: React.FC<PaymentHandlerProps>
43
+ PaymentActionCard?: React.FC<PaymentMethodActionCardProps>
43
44
  }
44
45
 
45
46
  export type PaymentMethodModules = { [code: string]: PaymentModule }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#1518](https://github.com/graphcommerce-org/graphcommerce/pull/1518) [`4143483f3`](https://github.com/graphcommerce-org/graphcommerce/commit/4143483f37c038d2bbf218be2685e27a31a35745) Thanks [@mikekeehnen](https://github.com/mikekeehnen)! - New ActionCardListForm implementation for Payment Methods
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`371e6cf52`](https://github.com/graphcommerce-org/graphcommerce/commit/371e6cf52916a3b6c44192bd40cc8271bd608832), [`4143483f3`](https://github.com/graphcommerce-org/graphcommerce/commit/4143483f37c038d2bbf218be2685e27a31a35745)]:
12
+ - @graphcommerce/next-ui@4.10.0
13
+ - @graphcommerce/magento-cart@4.4.4
14
+ - @graphcommerce/framer-scroller@2.1.17
15
+ - @graphcommerce/magento-store@4.2.11
16
+
3
17
  ## 3.2.0
4
18
 
5
19
  ### Minor Changes
@@ -0,0 +1,135 @@
1
+ import { Image } from '@graphcommerce/image'
2
+ import { ActionCard, Button, Form, FormDiv, useIconSvgSize } from '@graphcommerce/next-ui'
3
+ import {
4
+ ActionCardItemBase,
5
+ ActionCardItemRenderProps,
6
+ ActionCardListForm,
7
+ } from '@graphcommerce/next-ui/ActionCard/ActionCardListForm'
8
+ import {
9
+ useForm,
10
+ useFormCompose,
11
+ UseFormComposeOptions,
12
+ useFormPersist,
13
+ } from '@graphcommerce/react-hook-form'
14
+ import { Trans } from '@lingui/react'
15
+ import { SxProps, Theme } from '@mui/material'
16
+ import { useEffect } from 'react'
17
+ import { PaymentMethodActionCardProps, PaymentOptionsProps } from '../Api/PaymentMethod'
18
+ import { usePaymentMethodContext } from '../PaymentMethodContext/PaymentMethodContext'
19
+ import { useCartLock } from '../hooks'
20
+
21
+ function DefaultPaymentActionCard(props: PaymentMethodActionCardProps) {
22
+ return <ActionCard {...props} />
23
+ }
24
+
25
+ function PaymentMethodActionCard(
26
+ props: ActionCardItemRenderProps<PaymentOptionsProps> & {
27
+ sx?: SxProps<Theme>
28
+ },
29
+ ) {
30
+ const { onReset, code, step, Container, mollie_meta, sx = [] } = props
31
+ const { selectedMethod, selectedModule, modules } = usePaymentMethodContext()
32
+
33
+ const selectedAndOptions = selectedMethod?.code === code && selectedModule?.PaymentOptions
34
+ const Card = modules[code]?.PaymentActionCard ?? DefaultPaymentActionCard
35
+ const iconSize = useIconSvgSize('large')
36
+
37
+ return (
38
+ <Card
39
+ sx={[
40
+ {
41
+ '& .ActionCard-title': { typography: 'h5', lineHeight: 0 },
42
+ '& .ActionCard-details': { lineHeight: 1.5 },
43
+ },
44
+ ...(Array.isArray(sx) ? sx : [sx]),
45
+ ]}
46
+ image={
47
+ mollie_meta?.image && (
48
+ <Image
49
+ layout='fixed'
50
+ sx={{ width: iconSize, height: iconSize }}
51
+ width={0}
52
+ height={0}
53
+ unoptimized
54
+ src={mollie_meta?.image}
55
+ />
56
+ )
57
+ }
58
+ action={
59
+ <Button disableRipple variant='inline' color='secondary'>
60
+ <Trans id='Select' />
61
+ </Button>
62
+ }
63
+ reset={
64
+ <Button disableRipple variant='inline' color='secondary' onClick={onReset}>
65
+ <Trans id='Change' />
66
+ </Button>
67
+ }
68
+ after={
69
+ selectedAndOptions && (
70
+ <selectedModule.PaymentOptions {...selectedMethod} step={step} Container={Container} />
71
+ )
72
+ }
73
+ {...props}
74
+ />
75
+ )
76
+ }
77
+
78
+ export type PaymentMethodActionCardListFormProps = Pick<UseFormComposeOptions, 'step'>
79
+
80
+ export function PaymentMethodActionCardListForm(props: PaymentMethodActionCardListFormProps) {
81
+ const { step } = props
82
+ const { methods, selectedMethod, setSelectedMethod, setSelectedModule, modules } =
83
+ usePaymentMethodContext()
84
+
85
+ const [lockState] = useCartLock()
86
+ const form = useForm<{ code: string | null; paymentMethod?: string }>({
87
+ defaultValues: { code: lockState.method },
88
+ })
89
+
90
+ const { control, handleSubmit, watch, setValue } = form
91
+ const submit = handleSubmit(() => {})
92
+
93
+ const paymentMethod = watch('paymentMethod')
94
+
95
+ useFormPersist({ form, name: 'PaymentMethodActionCardList' })
96
+ useFormCompose({ form, step: 1, submit, key: 'PaymentMethodActionCardList' })
97
+
98
+ useEffect(() => {
99
+ const [code, child] = paymentMethod?.split('___') ?? ['']
100
+ if (code === selectedMethod?.code) return
101
+
102
+ const foundMethod = methods.find(
103
+ (method) => method.code === code && (!child || method.child === child),
104
+ )
105
+ if (foundMethod && !modules?.[foundMethod?.code ?? '']) {
106
+ console.error(`No PaymentModule found for method ${foundMethod.code}`)
107
+ }
108
+ setSelectedMethod(foundMethod)
109
+ setSelectedModule(modules?.[foundMethod?.code ?? ''])
110
+ }, [methods, modules, paymentMethod, selectedMethod?.code, setSelectedMethod, setSelectedModule])
111
+
112
+ useEffect(() => {
113
+ if (selectedMethod?.code) setValue('code', selectedMethod.code)
114
+ }, [selectedMethod?.code, setValue])
115
+
116
+ if (!methods || methods.length < 1) return null
117
+
118
+ return (
119
+ <Form onSubmit={submit} noValidate>
120
+ <ActionCardListForm<PaymentOptionsProps & ActionCardItemBase>
121
+ control={control}
122
+ name='paymentMethod'
123
+ errorMessage='Please select a shipping address'
124
+ items={methods.map((method) => ({
125
+ ...method,
126
+ value: method.code,
127
+ step,
128
+ Container: FormDiv,
129
+ }))}
130
+ render={PaymentMethodActionCard}
131
+ />
132
+ {/* <ApolloCartErrorAlert error={error} /> */}
133
+ </Form>
134
+ )
135
+ }
@@ -26,13 +26,6 @@ export type PaymentMethodTogglesProps = Pick<UseFormComposeOptions, 'step'> & {
26
26
  sx?: SxProps<Theme>
27
27
  }
28
28
 
29
- // function Content(props: PaymentMethod) {
30
- // const { code } = props
31
- // const { modules } = usePaymentMethodContext()
32
- // const Component = modules[code]?.PaymentToggle ?? ((p: PaymentToggleProps) => <>{p.title}</>)
33
- // return <Component {...props} />
34
- // }
35
-
36
29
  const cmpName = 'PaymentMethodToggles' as const
37
30
  const parts = [
38
31
  'formRoot',
package/README.md CHANGED
@@ -13,7 +13,6 @@ A Magento Payment Method module must implement
13
13
  export interface PaymentModule {
14
14
  PaymentOptions: React.VFC<PaymentOptionsProps>
15
15
  PaymentButton?: React.VFC<PaymentButtonProps>
16
- PaymentToggle?: React.VFC<PaymentToggleProps>
17
16
  expandMethods?: ExpandPaymentMethods
18
17
  }
19
18
  ```
package/index.ts CHANGED
@@ -8,3 +8,4 @@ export * from './PaymentMethodOptionsNoop/PaymentMethodOptionsNoop.gql'
8
8
  export * from './PaymentMethodPlaceOrder/PaymentMethodPlaceOrder'
9
9
  export * from './PaymentMethodPlaceOrderNoop/PaymentMethodPlaceOrderNoop'
10
10
  export * from './PaymentMethodToggles/PaymentMethodToggles'
11
+ export * from './PaymentMethodActionCardList/PaymentMethodActionCardListForm'
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-cart-payment-method",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "3.2.0",
5
+ "version": "3.3.0",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -15,17 +15,17 @@
15
15
  "@graphcommerce/eslint-config-pwa": "^4.1.8",
16
16
  "@graphcommerce/prettier-config-pwa": "^4.0.6",
17
17
  "@graphcommerce/typescript-config-pwa": "^4.0.3",
18
- "@graphcommerce/magento-cart-shipping-address": "3.1.0",
18
+ "@graphcommerce/magento-cart-shipping-address": "3.2.0",
19
19
  "@playwright/test": "^1.21.1",
20
20
  "type-fest": "^2.12.2"
21
21
  },
22
22
  "dependencies": {
23
- "@graphcommerce/framer-scroller": "2.1.16",
23
+ "@graphcommerce/framer-scroller": "2.1.17",
24
24
  "@graphcommerce/graphql": "3.2.1",
25
25
  "@graphcommerce/image": "3.1.7",
26
- "@graphcommerce/magento-cart": "4.4.3",
27
- "@graphcommerce/magento-store": "4.2.10",
28
- "@graphcommerce/next-ui": "4.9.0",
26
+ "@graphcommerce/magento-cart": "4.4.4",
27
+ "@graphcommerce/magento-store": "4.2.11",
28
+ "@graphcommerce/next-ui": "4.10.0",
29
29
  "@graphcommerce/react-hook-form": "3.2.2"
30
30
  },
31
31
  "peerDependencies": {