Question:
What is a more efficient way to nest if statements? I custom coded a sports registration system on a multi-state box. Upon successful payment there are 9 if statements the code shuffles through (by URL) to put the correct user input in the proper CMS. The user input is entering the proper CMS 75% of the time.
Product:
Wix Editor
What are you trying to achieve:
Parent X picks a Pricing Plan from a dynamic page → Enters child information in multi-state box → Pays the proper amount → If payment is “successful” → if wixLocationFrontend.url == “www.sportsclub.com/baseball” → insert into Baseball CMS
What have you already tried:
import wixLocationFrontend from 'wix-location-frontend';
import wixWindowFrontend from "wix-window-frontend";
import { myCreateOnlineOrderFunction } from "backend/checkout.web.js";
import wixPayFrontend from "wix-pay-frontend";
$w("#payNowButton").onClick(async () => {
try {
const order = await myCreateOnlineOrderFunction(planId);
const result = await wixPayFrontend.startPayment(order.wixPayOrderId);
if (result.status === "Successful") {
if(wixLocationFrontend.url == "https://www.sportsclub.com/plans/baseball/registration"){
// Collect data from the form fields
let name = $w("#athlete").value;
let birthdate = $w("#athleteBirthdate").value;
// Create an object to store the collected data
let formData = {
"name": name,
"birthdate": birthdate
};
wixData.insert("BaseballRegistration", formData)
.then(() => {
console.log("Data stored successfully");
// Additional actions upon successful storage, like
//showing a thank you message
console.log("Successfully Ordered");
})
.catch((error) => {
console.error("Error storing data", error);
// Error handling actions
})
}//end of baseball if
if(wixLocationFrontend.url == "https://www.sportsclub.com/plans/track/registration"){
// Collect data from the form fields
let name = $w("#athlete").value;
let birthdate = $w("#athleteBirthdate").value;
// Create an object to store the collected data
let formData = {
"name": name,
"birthdate": birthdate
};
wixData.insert("TrackRegistration", formData)
.then(() => {
console.log("Data stored successfully");
// Additional actions upon successful storage, like
//showing a thank you message
console.log("Successfully Ordered");
})
.catch((error) => {
console.error("Error storing data", error);
// Error handling actions
})
}//end of track if statement
Any help would be appreciated! I am sure there is a better way. Thank you.