@graphcommerce/react-hook-form 7.1.0-canary.8 → 8.0.0-canary.40

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,81 @@
1
1
  # Change Log
2
2
 
3
+ ## 8.0.0-canary.40
4
+
5
+ ## 7.1.0-canary.39
6
+
7
+ ## 7.1.0-canary.38
8
+
9
+ ## 7.1.0-canary.37
10
+
11
+ ## 7.1.0-canary.36
12
+
13
+ ## 7.1.0-canary.35
14
+
15
+ ## 7.1.0-canary.34
16
+
17
+ ## 7.1.0-canary.33
18
+
19
+ ## 7.1.0-canary.32
20
+
21
+ ## 7.1.0-canary.31
22
+
23
+ ## 7.1.0-canary.30
24
+
25
+ ## 7.1.0-canary.29
26
+
27
+ ## 7.1.0-canary.28
28
+
29
+ ## 7.1.0-canary.27
30
+
31
+ ## 7.1.0-canary.26
32
+
33
+ ## 7.1.0-canary.25
34
+
35
+ ## 7.1.0-canary.24
36
+
37
+ ## 7.1.0-canary.23
38
+
39
+ ## 7.1.0-canary.22
40
+
41
+ ### Patch Changes
42
+
43
+ - [#2097](https://github.com/graphcommerce-org/graphcommerce/pull/2097) [`cdc89cedb`](https://github.com/graphcommerce-org/graphcommerce/commit/cdc89cedb9eb4d37a2c37497689e74ff6df21bf6) - Set default value of `experimental_v2` to false in `useFormGql`, since we don't want an experimental feature as default ([@mikekeehnen](https://github.com/mikekeehnen))
44
+
45
+ ## 7.1.0-canary.21
46
+
47
+ ### Patch Changes
48
+
49
+ - [#2093](https://github.com/graphcommerce-org/graphcommerce/pull/2093) [`112b041f0`](https://github.com/graphcommerce-org/graphcommerce/commit/112b041f01a33fbd521ce3eb3955844f96b29917) - Created a new experimental mutation abort feature inside `useFormGql`. This will allow redundant mutations to be canceled. This feature can be enabled via the `experimental_useV2` prop on the `useFormGql` component ([@mikekeehnen](https://github.com/mikekeehnen))
50
+
51
+ ## 7.1.0-canary.20
52
+
53
+ ## 7.1.0-canary.19
54
+
55
+ ## 7.1.0-canary.18
56
+
57
+ ## 7.1.0-canary.17
58
+
59
+ ## 7.1.0-canary.16
60
+
61
+ ## 7.1.0-canary.15
62
+
63
+ ## 7.1.0-canary.14
64
+
65
+ ## 7.1.0-canary.13
66
+
67
+ ## 7.1.0-canary.12
68
+
69
+ ## 7.1.0-canary.11
70
+
71
+ ## 7.1.0-canary.10
72
+
73
+ ## 7.1.0-canary.9
74
+
75
+ ### Patch Changes
76
+
77
+ - [#2007](https://github.com/graphcommerce-org/graphcommerce/pull/2007) [`f59c27660`](https://github.com/graphcommerce-org/graphcommerce/commit/f59c276605f9ed649d1197a9ba0e3f12d7c6d026) - Reconstruct crosssells behaviour. Add submitted variables & showSuccess to RHF ([@JoshuaS98](https://github.com/JoshuaS98))
78
+
3
79
  ## 7.1.0-canary.8
4
80
 
5
81
  ## 7.0.2-canary.7
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": "7.1.0-canary.8",
5
+ "version": "8.0.0-canary.40",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -11,19 +11,17 @@
11
11
  "project": "./tsconfig.json"
12
12
  }
13
13
  },
14
- "dependencies": {
15
- "@apollo/client": "~3.8.2",
16
- "react-hook-form": "7.46.1"
17
- },
18
14
  "devDependencies": {
19
- "@graphcommerce/eslint-config-pwa": "7.1.0-canary.8",
20
- "@graphcommerce/prettier-config-pwa": "7.1.0-canary.8",
21
- "@graphcommerce/typescript-config-pwa": "7.1.0-canary.8",
22
- "@testing-library/react": "^14.0.0"
15
+ "@testing-library/react": "^14.1.0"
23
16
  },
24
17
  "peerDependencies": {
18
+ "@apollo/client": "^3",
19
+ "@graphcommerce/eslint-config-pwa": "8.0.0-canary.40",
20
+ "@graphcommerce/prettier-config-pwa": "8.0.0-canary.40",
21
+ "@graphcommerce/typescript-config-pwa": "8.0.0-canary.40",
25
22
  "graphql": "^16.6.0",
26
23
  "react": "^18.2.0",
27
- "react-dom": "^18.2.0"
24
+ "react-dom": "^18.2.0",
25
+ "react-hook-form": "^7"
28
26
  }
29
27
  }
@@ -19,6 +19,30 @@ type UseFormGraphQLCallbacks<Q, V> = {
19
19
  */
20
20
  onBeforeSubmit?: (variables: V) => V | false | Promise<V | false>
21
21
  onComplete?: OnCompleteFn<Q, V>
22
+
23
+ /**
24
+ * Changes:
25
+ * - Restores `defaultValues` functionality to original functionality, use `values` instead.
26
+ * - Does not reset the form after submission, use `values` instead.
27
+ * - Does not 'encode' the variables, use onBeforeSubmit instead.
28
+ *
29
+ * Future plans:
30
+ * - Remove the useMutation/useLazyQuery tuple and use a reguler client.mutation() call.
31
+ * - Write graphql errors to setError('root')
32
+ * - Remove onBeforeSubmit, onComplete and the handleSubmit rewrite with a single mutate() callback.
33
+ *
34
+ *
35
+ * ```ts
36
+ * const { handleSubmit } = useFormGqlMutation();
37
+ *
38
+ * const submit = handleSubmit((formValues, mutate) => {
39
+ * // onBeforeSubmit now simply is code before mutate() where you can return early for example or set errors.
40
+ * const result = mutate() // executes the mutation and automatically sets generic errors with setError('root')
41
+ * // onComplete: now simply use the result after the form, to for example reset the form, or do other things.
42
+ * })
43
+ * ```
44
+ */
45
+ experimental_useV2?: boolean
22
46
  }
23
47
 
24
48
  export type UseFormGraphQlOptions<Q, V extends FieldValues> = UseFormProps<V> &
@@ -28,7 +52,11 @@ export type UseFormGqlMethods<Q, V extends FieldValues> = Omit<
28
52
  UseGqlDocumentHandler<V>,
29
53
  'encode' | 'type'
30
54
  > &
31
- Pick<UseFormReturn<V>, 'handleSubmit'> & { data?: Q | null; error?: ApolloError }
55
+ Pick<UseFormReturn<V>, 'handleSubmit'> & {
56
+ data?: Q | null
57
+ error?: ApolloError
58
+ submittedVariables?: V
59
+ }
32
60
 
33
61
  /**
34
62
  * Combines useMutation/useLazyQuery with react-hook-form's useForm:
@@ -46,14 +74,27 @@ export function useFormGql<Q, V extends FieldValues>(
46
74
  defaultValues?: UseFormProps<V>['defaultValues']
47
75
  } & UseFormGraphQLCallbacks<Q, V>,
48
76
  ): UseFormGqlMethods<Q, V> {
49
- const { onComplete, onBeforeSubmit, document, form, tuple, defaultValues } = options
77
+ const {
78
+ onComplete,
79
+ onBeforeSubmit,
80
+ document,
81
+ form,
82
+ tuple,
83
+ defaultValues,
84
+ experimental_useV2 = false,
85
+ } = options
50
86
  const { encode, type, ...gqlDocumentHandler } = useGqlDocumentHandler<Q, V>(document)
51
- const [execute, { data, error }] = tuple
87
+ const [execute, { data, error, loading }] = tuple
88
+
89
+ const submittedVariables = useRef<V>()
52
90
 
53
91
  // automatically updates the default values
54
92
  const initital = useRef(true)
93
+ const controllerRef = useRef<AbortController | undefined>()
55
94
  const valuesString = JSON.stringify(defaultValues)
56
95
  useEffect(() => {
96
+ if (experimental_useV2) return
97
+
57
98
  if (initital.current) {
58
99
  initital.current = false
59
100
  return
@@ -66,7 +107,8 @@ export function useFormGql<Q, V extends FieldValues>(
66
107
  const handleSubmit: UseFormReturn<V>['handleSubmit'] = (onValid, onInvalid) =>
67
108
  form.handleSubmit(async (formValues, event) => {
68
109
  // Combine defaults with the formValues and encode
69
- let variables = encode({ ...defaultValues, ...formValues })
110
+ submittedVariables.current = undefined
111
+ let variables = experimental_useV2 ? formValues : encode({ ...defaultValues, ...formValues })
70
112
 
71
113
  // Wait for the onBeforeSubmit to complete
72
114
  if (onBeforeSubmit) {
@@ -76,14 +118,28 @@ export function useFormGql<Q, V extends FieldValues>(
76
118
  }
77
119
  // if (variables === false) onInvalid?.(formValues, event)
78
120
 
79
- const result = await execute({ variables })
121
+ submittedVariables.current = variables
122
+ if (loading && experimental_useV2) controllerRef.current?.abort()
123
+ controllerRef.current = new window.AbortController()
124
+ const result = await execute({
125
+ variables,
126
+ context: { fetchOptions: { signal: controllerRef.current.signal } },
127
+ })
128
+
80
129
  if (onComplete && result.data) await onComplete(result, variables)
81
130
 
82
131
  // Reset the state of the form if it is unmodified afterwards
83
- if (typeof diff(form.getValues(), formValues) === 'undefined') form.reset(formValues)
132
+ if (typeof diff(form.getValues(), formValues) === 'undefined' && !experimental_useV2)
133
+ form.reset(formValues)
84
134
 
85
135
  await onValid(formValues, event)
86
136
  }, onInvalid)
87
137
 
88
- return { ...gqlDocumentHandler, handleSubmit, data, error }
138
+ return {
139
+ ...gqlDocumentHandler,
140
+ handleSubmit,
141
+ data,
142
+ error,
143
+ submittedVariables: submittedVariables.current,
144
+ }
89
145
  }