@graphcommerce/react-hook-form 9.0.1-canary.1 → 9.0.1-canary.3

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 9.0.1-canary.3
4
+
5
+ ## 9.0.1-canary.2
6
+
7
+ ### Patch Changes
8
+
9
+ - [#2465](https://github.com/graphcommerce-org/graphcommerce/pull/2465) [`881e146`](https://github.com/graphcommerce-org/graphcommerce/commit/881e146acf0d129d6d44ec207619a45ad04d673b) - Solve an issue where the payment submission would remain in a spinning state when placing an order failed: `useFormGql` will now set `root` error on the form when there is an error response on the GraphQL operation, an error is thrown in onBeforeSubmit and in onSuccess. ([@paales](https://github.com/paales))
10
+
3
11
  ## 9.0.1-canary.1
4
12
 
5
13
  ## 9.0.0
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/react-hook-form",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "9.0.1-canary.1",
5
+ "version": "9.0.1-canary.3",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -13,9 +13,9 @@
13
13
  },
14
14
  "peerDependencies": {
15
15
  "@apollo/client": "*",
16
- "@graphcommerce/eslint-config-pwa": "^9.0.1-canary.1",
17
- "@graphcommerce/prettier-config-pwa": "^9.0.1-canary.1",
18
- "@graphcommerce/typescript-config-pwa": "^9.0.1-canary.1",
16
+ "@graphcommerce/eslint-config-pwa": "^9.0.1-canary.3",
17
+ "@graphcommerce/prettier-config-pwa": "^9.0.1-canary.3",
18
+ "@graphcommerce/typescript-config-pwa": "^9.0.1-canary.3",
19
19
  "@mui/utils": "^5",
20
20
  "graphql": "^16.6.0",
21
21
  "react": "^18.2.0",
@@ -1,5 +1,4 @@
1
1
  import type {
2
- ApolloError,
3
2
  FetchResult,
4
3
  LazyQueryHookOptions,
5
4
  LazyQueryResultTuple,
@@ -8,7 +7,7 @@ import type {
8
7
  MutationTuple,
9
8
  TypedDocumentNode,
10
9
  } from '@apollo/client'
11
- import { isApolloError } from '@apollo/client'
10
+ import { ApolloError, isApolloError } from '@apollo/client'
12
11
  import { getOperationName } from '@apollo/client/utilities'
13
12
  import useEventCallback from '@mui/utils/useEventCallback'
14
13
  import { useEffect, useRef } from 'react'
@@ -179,16 +178,23 @@ export function useFormGql<Q, V extends FieldValues>(
179
178
  if (onBeforeSubmitError) {
180
179
  if (isApolloError(onBeforeSubmitError)) {
181
180
  returnedError.current = onBeforeSubmitError
181
+ form.setError('root', { message: onBeforeSubmitError.message })
182
182
  } else {
183
- console.info(
184
- 'A non ApolloError was thrown during the onBeforeSubmit handler.',
185
- onBeforeSubmitError,
186
- )
183
+ const message =
184
+ process.env.NODE_ENV === 'development'
185
+ ? `A non ApolloError was thrown during the onBeforeSubmit handler: ${onBeforeSubmitError.message}`
186
+ : 'An unexpected error occurred, please contact the store owner'
187
+ form.setError('root', { message })
188
+ returnedError.current = new ApolloError({ graphQLErrors: [{ message }] })
187
189
  }
190
+ return
191
+ }
188
192
 
193
+ if (onBeforeSubmitResult === false) {
194
+ form.setError('root', { message: 'Form submission cancelled' })
189
195
  return
190
196
  }
191
- if (onBeforeSubmitResult === false) return
197
+
192
198
  variables = onBeforeSubmitResult
193
199
 
194
200
  submittedVariables.current = variables
@@ -207,21 +213,25 @@ export function useFormGql<Q, V extends FieldValues>(
207
213
  },
208
214
  })
209
215
 
210
- const [, onCompleteError] = await complete(result, variables)
211
- if (onCompleteError) {
212
- returnedError.current = onCompleteError as ApolloError
216
+ // If there are submission errors, set the error and return
217
+ if (result.errors && result.errors.length > 0) {
218
+ form.setError('root', { message: result.errors.map((e) => e.message).join(', ') })
213
219
  return
214
220
  }
221
+
222
+ const [, onCompleteError] = await complete(result, variables)
215
223
  if (onCompleteError) {
216
224
  if (isApolloError(onCompleteError)) {
217
225
  returnedError.current = onCompleteError
226
+ form.setError('root', { message: onCompleteError.message })
218
227
  } else {
219
- console.info(
220
- 'A non ApolloError was thrown during the onComplete handler.',
221
- onCompleteError,
222
- )
228
+ const message =
229
+ process.env.NODE_ENV === 'development'
230
+ ? `A non ApolloError was thrown during the onComplete handler: ${onCompleteError.message}`
231
+ : 'An unexpected error occurred, please contact the store owner'
232
+ form.setError('root', { message })
233
+ returnedError.current = new ApolloError({ graphQLErrors: [{ message }] })
223
234
  }
224
-
225
235
  return
226
236
  }
227
237
 
@@ -236,7 +246,7 @@ export function useFormGql<Q, V extends FieldValues>(
236
246
  ...gqlDocumentHandler,
237
247
  handleSubmit,
238
248
  data,
239
- error: error ?? returnedError.current,
249
+ error: returnedError.current ?? error,
240
250
  submittedVariables: submittedVariables.current,
241
251
  }
242
252
  }