Skip to content

Commit

Permalink
update form payload
Browse files Browse the repository at this point in the history
  • Loading branch information
rrusher committed Feb 28, 2024
1 parent 1ebbc3a commit dfc38db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
29 changes: 16 additions & 13 deletions blocks/contact-form/contact-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const phoneRegex = /^[+]?[ (]?\d{3}[)]?[-.\s]?\d{3}[-.\s]?\d{4}$/;

/**
* Adds form and cookie values to a JSON object.
* Adds form and cookie values to payload.
*
* @param {FormData} form - The FormData object representing the form data.
*/
function addFranchiseData(form) {
// Common form elements
const firstName = form.elements.first_name.value;
const lastName = form.elements.last_name.value;
const email = form.elements.email.value;
const phone = form.elements.phone.value;
const comments = form.elements.comments.value;

// All forms except team inquiry
if (form.id !== 'team-inquiry') {
const hasAgentRadio = form.elements.hasAgent;
const hasAgentValue = Array.from(hasAgentRadio).find((radio) => radio.checked)?.value === 'yes';
Expand Down Expand Up @@ -68,17 +70,20 @@ function addFranchiseData(form) {
jsonObj.mlsName = 'MLSPIN - MLS Property Information Network';
jsonObj.pid = '234234';
}
// Data format to JSON
return JSON.stringify(jsonObj);
}

// Remaining Team Inquiry form elements
const title = form.elements.title.value;
const zip = form.elements.title.value;
const country = form.elements.title.value;
const state = form.elements.title.value;
const city = form.elements.title.value;
const address1 = form.elements.title.value;
const address2 = form.elements.title.value;
const numAgents = form.elements.title.value;
const gci = form.elements.title.value;
const zip = form.elements.postalCode.value;
const country = form.elements.country.value;
const state = form.elements.state.value;
const city = form.elements.city.value;
const address1 = form.elements.addressOne.value;
const address2 = form.elements.addressTwo.value;
const numAgents = form.elements.numOfAgents.value;
const gci = form.elements.gci.value;

const formData = new FormData();
formData.append('FirstName', firstName);
Expand All @@ -98,7 +103,8 @@ function addFranchiseData(form) {
formData.append('Subject', 'Join our Team Website Inquiry');
formData.append('SendEmail', true);
formData.append('To', '[email protected]');
return formData;
// Data format to URL Params
return new URLSearchParams(formData).toString();
}

function displayError(errors) {
Expand Down Expand Up @@ -181,10 +187,8 @@ const addForm = async (block) => {
const oldSubmit = validateFormInputs;
thankYou.classList.add('form-thank-you');
form.onsubmit = function handleSubmit(e) {
console.log('Handle submit'); // Check if this log appears
e.preventDefault();
if (oldSubmit(this)) {
console.log('OnSubmit called'); // Check if this log appears
const jsonData = addFranchiseData(this);
const headers = new Headers();
if (this.id === 'team-inquiry') {
Expand All @@ -193,7 +197,6 @@ const addForm = async (block) => {
headers.append('Content-Type', 'application/json; charset=UTF-8');
}
const { action, method } = this;
console.log('Call fetch'); // Check if this log appears
fetch(action, {
method,
headers,
Expand Down
15 changes: 2 additions & 13 deletions blocks/contact-form/forms/join-our-team.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<input name="gci" type="text" placeholder="CGI US Dollars*" aria-label="GCI US dollars" aria-required="true" autocomplete="off">
</div>
<div class="country">
<select class="country-dropdown" aria-labelledby="app">
<select name="country" class="country-dropdown" aria-labelledby="app">
<option>Country</option>
<option>
Afghanistan
Expand Down Expand Up @@ -776,18 +776,7 @@
placeholder="Leave a comment or question and we'll get back to you shortly."
autocomplete="off"></textarea>
</div>
<div id="captcha-20285" class="g-recaptcha contact-form-captcha" captcha-render="1">
<div style="width: 304px; height: 78px;">
<iframe title="reCAPTCHA" width="304" height="78" role="presentation" name="a-duze5gwmci0x"
frameborder="0" scrolling="no"
sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox allow-storage-access-by-user-activation"
src="https://www.google.com/recaptcha/api2/anchor?ar=2&amp;k=6LebYaYUAAAAAC9SqASljwaF57MpKSvEkwDOzk6l&amp;co=aHR0cHM6Ly93d3cuY29tbW9ubW92ZXMuY29tOjQ0Mw..&amp;hl=en&amp;v=Ya-Cd6PbRI5ktAHEhm9JuKEu&amp;size=normal&amp;cb=sa2502jzdt91"></iframe>
<textarea id="g-recaptcha-response-3" name="g-recaptcha-response"
class="g-recaptcha-response"
style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea>
</div>
<iframe style="display: none;"></iframe>
</div>
<div id="captcha-20285" class="g-recaptcha" captcha-render="1"></div>
<div class="cta">
<div class="button-container">
<button type="submit" class="primary submit" aria-label="Submit team form">Submit</button>
Expand Down

0 comments on commit dfc38db

Please sign in to comment.