@graphcommerce/react-hook-form 9.0.0-canary.81 → 9.0.0-canary.83

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,9 @@
1
1
  # Change Log
2
2
 
3
+ ## 9.0.0-canary.83
4
+
5
+ ## 9.0.0-canary.82
6
+
3
7
  ## 9.0.0-canary.81
4
8
 
5
9
  ## 9.0.0-canary.80
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.0-canary.81",
5
+ "version": "9.0.0-canary.83",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -16,9 +16,9 @@
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@apollo/client": "^3",
19
- "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.81",
20
- "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.81",
21
- "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.81",
19
+ "@graphcommerce/eslint-config-pwa": "^9.0.0-canary.83",
20
+ "@graphcommerce/prettier-config-pwa": "^9.0.0-canary.83",
21
+ "@graphcommerce/typescript-config-pwa": "^9.0.0-canary.83",
22
22
  "@mui/utils": "^5",
23
23
  "graphql": "^16.6.0",
24
24
  "react": "^18.2.0",
@@ -120,19 +120,10 @@ export function ComposedSubmit(props: ComposedSubmitProps) {
120
120
  }
121
121
 
122
122
  const errors: ApolloError[] = []
123
- let rootThrown: GlobalError | undefined
124
123
 
125
124
  formEntries.forEach(([, { form }]) => {
126
125
  if (form && isFormGqlOperation(form) && form.error) errors.push(form.error)
127
- if (form && form.formState.errors.root?.thrown) rootThrown = form.formState.errors.root.thrown
128
126
  })
129
127
 
130
- return (
131
- <Render
132
- buttonState={buttonState}
133
- submit={submitAll}
134
- error={mergeErrors(errors)}
135
- rootThrown={rootThrown}
136
- />
137
- )
128
+ return <Render buttonState={buttonState} submit={submitAll} error={mergeErrors(errors)} />
138
129
  }
@@ -36,7 +36,6 @@ export type ComposedSubmitRenderComponentProps = {
36
36
  submit: () => Promise<void>
37
37
  buttonState: ButtonState
38
38
  error?: ApolloError
39
- rootThrown?: GlobalError
40
39
  }
41
40
 
42
41
  export type ComposedFormState = {
@@ -4,6 +4,7 @@ import {
4
4
  LazyQueryResultTuple,
5
5
  MutationTuple,
6
6
  TypedDocumentNode,
7
+ isApolloError,
7
8
  } from '@apollo/client'
8
9
  import { getOperationName } from '@apollo/client/utilities'
9
10
  import useEventCallback from '@mui/utils/useEventCallback'
@@ -21,13 +22,13 @@ type UseFormGraphQLCallbacks<Q, V> = {
21
22
  * Mutation.
22
23
  *
23
24
  * When returning false, it will silently stop the submission.
24
- * When an error is thrown, it will be set as a generic error with `setError('root.thrown', { message: error.message })`
25
+ * When an error is thrown, it will be set as an ApolloError
25
26
  */
26
27
  onBeforeSubmit?: (variables: V) => V | false | Promise<V | false>
27
28
  /**
28
29
  * Called after the mutation has been executed. Allows you to handle the result of the mutation.
29
30
  *
30
- * When an error is thrown, it will be set as a generic error with `setError('root.thrown', { message: error.message })`
31
+ * When an error is thrown, it will be set as an ApolloError
31
32
  */
32
33
  onComplete?: OnCompleteFn<Q, V>
33
34
 
@@ -115,6 +116,7 @@ export function useFormGql<Q, V extends FieldValues>(
115
116
  const [execute, { data, error, loading }] = tuple
116
117
 
117
118
  const submittedVariables = useRef<V>()
119
+ const returnedError = useRef<ApolloError>()
118
120
 
119
121
  // automatically updates the default values
120
122
  const initital = useRef(true)
@@ -153,14 +155,24 @@ export function useFormGql<Q, V extends FieldValues>(
153
155
  return
154
156
  }
155
157
 
156
- // Combine defaults with the formValues and encode
158
+ returnedError.current = undefined
157
159
  submittedVariables.current = undefined
160
+
161
+ // Combine defaults with the formValues and encode
158
162
  let variables = !deprecated_useV1 ? formValues : encode({ ...defaultValues, ...formValues })
159
163
 
160
164
  // Wait for the onBeforeSubmit to complete
161
165
  const [onBeforeSubmitResult, onBeforeSubmitError] = await beforeSubmit(variables)
162
166
  if (onBeforeSubmitError) {
163
- form.setError('root', { message: onBeforeSubmitError.message })
167
+ if (isApolloError(onBeforeSubmitError)) {
168
+ returnedError.current = onBeforeSubmitError
169
+ } else {
170
+ console.log(
171
+ 'A non ApolloError was thrown during the onBeforeSubmit handler.',
172
+ onBeforeSubmitError,
173
+ )
174
+ }
175
+
164
176
  return
165
177
  }
166
178
  if (onBeforeSubmitResult === false) return
@@ -176,7 +188,19 @@ export function useFormGql<Q, V extends FieldValues>(
176
188
 
177
189
  const [, onCompleteError] = await complete(result, variables)
178
190
  if (onCompleteError) {
179
- form.setError('root', { message: onCompleteError.message })
191
+ returnedError.current = onCompleteError as ApolloError
192
+ return
193
+ }
194
+ if (onCompleteError) {
195
+ if (isApolloError(onCompleteError)) {
196
+ returnedError.current = onCompleteError
197
+ } else {
198
+ console.log(
199
+ 'A non ApolloError was thrown during the onComplete handler.',
200
+ onCompleteError,
201
+ )
202
+ }
203
+
180
204
  return
181
205
  }
182
206
 
@@ -191,7 +215,7 @@ export function useFormGql<Q, V extends FieldValues>(
191
215
  ...gqlDocumentHandler,
192
216
  handleSubmit,
193
217
  data,
194
- error,
218
+ error: error ?? returnedError.current,
195
219
  submittedVariables: submittedVariables.current,
196
220
  }
197
221
  }