I’m trying to make a lightbox form to register as a member of my website and, even thought it works, I’m experiencing some problems when it gets to saving contacts information.
It only saves the email and creates a contact that has only the email in it.
The form looks like this:
This is the lightbox’s code:
import wixUsers from 'wix-users';
import wixCrm from 'wix-crm';
import wixLocation from 'wix-location';
import {processSubmission} from 'backend/submitRequest';
$w.onReady(function () {
$w('#register').disable();
$w('#temptxt').collapse();
$w("#myCaptcha").onVerified(() => {
$w('#temptxt').collapse();
$w("#register").enable();
let myToken = $w("#myCaptcha").token;
});
$w("#password").onCustomValidation( (value, reject) => {
if(value.length < 4) {
reject("La contrasenya ha de tindre més de 4 caràcters");
}
});
$w("#password").onInput(() => {
if(!$w('#password').valid) {
$w('#temptxt').text = $w("#password").validationMessage;
$w('#temptxt').expand();
}
else{
$w('#temptxt').collapse();
}
});
$w('#register').onClick( () => {
let emails = [];
let labels = [];
let instr = "";
if ($w('#firstName').valid && $w('#lastNames').valid && $w('#instrument').valid && $w('#password').valid && $w('#priv').valid && $w('#newsletter').valid) {
if ($w('#email').valid) {
emails.push($w('#email').value);
if ($w('#instrument').value !== "no"){
instr = $w('#instrument').value;
}
if ($w('#simfonica').checked) {
labels.push($w('#simfonica').value);
}
if ($w('#juvenil').checked) {
labels.push($w('#juvenil').value);
}
let contactInfo = {
"firstName": $w('#firstName').value,
"lastName": $w('#lastNames').value,
"emails": emails,
"loginEmail": $w('#email').value,
"labels": labels,
"Instrument": instr,
};
let submitRequestData = {
"token": $w("#myCaptcha").token,
"data": {
"email": $w("#email").value,
"password": $w("#password").value,
"contactInfo": contactInfo,
"newsletter" : $w('#newsletter').checked
}
};
processSubmission(submitRequestData)
.then( () => {
$w("#myCaptcha").reset();
$w("#register").disable();
$w('#temptxt').html = '<p class = "p3"; style = "color:black"></p3>';
$w('#temptxt').text = "T'has registrat amb èxit!";
$w('#temptxt').expand();
wixUsers.login($w('#email').value, $w('#password').value)
wixLocation.to("/inici");
})
.catch( () => {
$w("#myCaptcha").reset();
$w("#register").disable();
$w('#temptxt').text = "Ha hagut un error. Per favor, torna a intentar la verificació CAPTCHA.";
$w('#temptxt').expand();
})
}
else{
$w('#temptxt').text = "Per favor, introdueix un email vàlid."
$w('#temptxt').expand();
}
}
else{
$w('#temptxt').text = "Per favor, ompli correctament tots els camps."
$w('#temptxt').expand();
}
});
$w("#myCaptcha").onError(() => {
$w('#temptxt').text = "Ha hagut un error. Per favor, torna a intentar la verificació CAPTCHA.";
$w('#temptxt').expand();
});
$w("#myCaptcha").onTimeout(() => {
$w("#register").disable();
$w("#temptxt").text = "La verificació reCAPTCHA ha perdut la connexió. Per favor, torna a intentar-ho més tard.";
$w("#temptxt").expand();
});
});
And this is the code of the backend file:
// Filename: backend/submitRequest.jsw (web modules need to have a .jsw extension)
import wixCaptcha from 'wix-captcha-backend';
import wixCrm from 'wix-crm-backend';
import wixUsersBackend from 'wix-users-backend';
// Authorize token and insert data
export function processSubmission(submitRequestData) {
return wixCaptcha.authorize(submitRequestData.token)
.then(() => {
return wixUsersBackend.register(submitRequestData.data.email, submitRequestData.data.password, submitRequestData.data.contactInfo)
.then(() => {
if (submitRequestData.data.newsletter) {
submitRequestData.data.contactInfo.labels.push("Subscriptors");
}
wixCrm.createContact(submitRequestData.data.contactInfo);
return { "type": "success" }
})
.catch((error) => ({ "type": "insertion error", "message": "Error: collection insertion failed: " + error }));
})
.catch((error) => ({ "type": "authorization error", "message": "Error: CAPTCHA authorization failed: " + error }));
}
Hope someone can help.
Thank you in advance!