const FORM_URL = '/form';
const PDF_BASE_URL = '/form';
const lVars = ['Aanleiding', 'Gewicht', 'Klachten', 'Naam', 'Soort'];
// No need to edit below :-) Hopefully...
const form = document.querySelector('#newsletter-form');
const pdfLink = document.querySelector('#download-pdf');
async function submitHandler(event) {
const formFields = document.querySelector('.form-row');
const thanks = document.querySelector('.thanks');
event.preventDefault();
try {
await fetch(FORM_URL, {
method: 'POST',
body: new FormData(form),
});
formFields.style.display = 'none';
thanks.style.display = 'block';
} catch (error) {
alert('Er ging iets mis! Probeer het opnieuw.');
console.error(error);
}
}
function createPDFLink() {
const queryString = lVars
.reduce((accumulator, value) => {
accumulator.push(`${value}=${getValue(value)}`);
return accumulator;
}, [])
.join('&');
return `${PDF_BASE_URL}?${queryString}`;
}
function getValue(varName) {
const ltVar = window[`Var${varName}`];
if (ltVar) {
return encodeURIComponent(ltVar.getValue());
}
return '';
}
form.addEventListener('submit', submitHandler);
pdfLink.setAttribute('href', createPDFLink());