Created
February 24, 2016 21:57
-
-
Save mauriciosoares/c0e2271c95d0770304cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @flow */ | |
import getDocument from 'shared/utils/get-document'; | |
export default function submitGoogleForm(fields, mapper, googleFormId='google-form') { | |
const document = getDocument(); | |
const formData = Object.keys(mapper).reduce((data, key) => { | |
data[mapper[key]] = fields[key]; | |
return data; | |
}, {}); | |
const googleForm = document.getElementById(googleFormId); | |
if (googleForm === null) { | |
throw new Error('You must have an embedded iframe with your Google Form'); | |
} | |
const form = document.createElement('form'); | |
form.action = googleForm.src.replace('viewform', 'formResponse'); | |
form.method = 'POST'; | |
form.target = googleFormId; | |
form.style = 'display: none'; | |
Object.keys(formData).forEach(key => { | |
let field = document.createElement('input'); | |
field.name = key; | |
field.value = formData[key]; | |
form.appendChild(field); | |
}); | |
document.body.appendChild(form); | |
form.submit(); | |
document.body.removeChild(form); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment