Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit ae275d7

Browse files
committed
Very close
1 parent 50e7c97 commit ae275d7

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

components/molecules/GravityForm/GravityForm.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,37 @@ export default function GravityForm({
3131
formDefaults={fieldDefaults}
3232
id={formId && `gform-${formId}`}
3333
validationSchema={formValidationSchema}
34-
// onSubmit={(values, actions) => console.log({values, actions})}
34+
onSubmit={(values) => {
35+
const formApiUrl = `https://nextjswp.test/wp-json/gf/v2/forms/${formId}/submissions`
36+
const formData = new FormData()
37+
const formKeys = Object.keys(values)
38+
39+
formKeys.forEach((key) => {
40+
let fieldName = key.replaceAll('-', '_')
41+
if (fieldName.endsWith('_filedata')) {
42+
fieldName = fieldName.slice(0, -9)
43+
}
44+
if (values[key]) {
45+
formData.append(fieldName, values[key])
46+
}
47+
})
48+
49+
// console.log({values, formApiUrl})
50+
// for (var pair of formData.entries()) {
51+
// console.log(pair[0]+ ', ' + pair[1]);
52+
// }
53+
54+
fetch(formApiUrl, {
55+
method: 'POST',
56+
headers: {
57+
'Content-Type': 'application/x-www-form-urlencoded'
58+
},
59+
mimeType: 'multipart/form-data',
60+
body: formData
61+
}).then((response) => response.json())
62+
// .then(data => console.log(data))
63+
// .catch(error => console.log({error}))
64+
}}
3565
>
3666
{(formikProps) => (
3767
<>

0 commit comments

Comments
 (0)