Hey Stcroppe!
I sincerely appreciate your help! I took your suggestions and messed around with them all day yesterday since you posted. Tried different variations and read the suggested materials (GREAT info, btw!). I do find myself just seconds away from blowing this whole thing up out of frustration, though! haha
The code looks and reads like it should be operating the intended way, but it isn’t. And all because of one minor issue that I can’t seem to resolve. I will try to keep this brief and explain what and why it’s important:
The company that I’m attempting this site for REQUIRES all participants to agree to their waiver and release of liability. I have set this agreeance up with a switch that the user toggles, default to no, and switched to yes. If yes value present, it expands a box that contains an input for their email, then submit button. If switch toggled back to no value, it hides the box, making it so the switch HAS to have a yes value to submit/join.
I have been able to code that the switch disables on a successful submission, and would very much like to preform the same task for when the code reports back as a duplicate - and THAT is what is not working. In a nutshell, everything works: input drops on switch, input correctly validates email, input hides when switch equals ‘no’, and my hook even correctly finds and reports back any dups with my error text (Thank you!). But that’s where the fun stops as the switch is still shown in the ‘yes’ toggle position. This is problematic because, for some reason, when this happens and the user then switches to ‘no’, it reveals the email input and would technically allow them to signup (with an unused email) while NOT agreeing to the terms. I know that if I can just find a way to disable the switch when a dup is found, it would solve all issues.
I sincerely apologize for the long text, but would honestly rather provide too much information than not enough. And, with that said, I will provide all my code for the page and backend js:
Backend/data.js:
import wixData from ‘wix-data’;
export function FitnessSignup_beforeInsert(item, context) {
console.log(“in before insert hook”);
console.log("user is " + item.combine);
let p = new Promise( function (resolve, reject)
{
console.log(“in promise body”);
let user_q = wixData.query(“FitnessSignup”).eq(“combine”, item.combine);
user_q.count().then(count =>
{
console.log(“after big_q returns”);
if (count === 0) {
console.log(“count is zero, no dups found, resolving the promise to the original ‘item’ so the insert can proceed”);
resolve(item);
}
else {
console.log(“count is more than zero, rejecting the promise with an error message”);
reject(“count is more than 0, dups found!”);
}
})
});
return p;
}
Page code:
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;
$w.onReady( function () {
$w(“#fitnessSignups”).onBeforeSave( function () {
$w(“#fitnessSignups”).setFieldValue(‘site’, $w(‘#text10’).text);
$w(“#fitnessSignups”).setFieldValue(‘exercise’, $w(‘#text2’).text);
$w(“#fitnessSignups”).setFieldValue(‘day’, $w(‘#text3’).text);
$w(“#fitnessSignups”).setFieldValue(‘time’, $w(‘#text4’).text);
$w(“#fitnessSignups”).setFieldValue(‘location’, $w(‘#text8’).text);
$w(“#fitnessSignups”).setFieldValue(‘classId’, $w(‘#text15’).text);
$w(“#fitnessSignups”).setFieldValue(‘combine’, $w(‘#text15’).text + $w(“#input1”).value);
$w(“#fitnessSignups”).setFieldValue(‘remove’, ‘Leave class’);
});
$w('#fitnessSignups').onAfterSave(()=>{
setTimeout( **function** (){
if ($w(‘#success’).isVisible) {
$w(‘#box1’).hide(‘float’, floatOptions);
$w(‘#switch1’).disable();
}
});
});
$w("#dropdown1").onChange((event, $w) => {
console.log(event.target.value);
wixLocation.to(event.target.value);
});
});
let floatOptions = {
“duration”: 500,
“delay”: 0,
“direction”: “top”
};
export function switch1_click (event, $w) {
if ($w(‘#box1’).isVisible) {
$w(‘#box1’).hide(‘float’, floatOptions);
$w(‘#errorText’).hide(‘float’, floatOptions);
}
else {
$w(‘#box1’).show();
}
}
export function button2_click(event, $w) {
wixData.query(“FitnessSignup”)
.eq(‘combine’, $w(‘#text15’).text + $w(“#input1”).value)
.find()
.then((results) => {
if (results.length > 0) {
//id is not unique. show error and disable submit button
$w(‘#box1’).hide(‘float’, floatOptions);
$w(‘#errorText’).show();
$w(‘#switch1’).disable();
}
else {
//id is unique. do nothing
}
}). catch ((err) => {
let errorMsg = err;
});
}
I do appreciate your time with this!