Gets an object containing RSVP functionality.
Use the RSVP
object to create an RSVP for an event.
Note: To work with the Wix Events API, you need to publish your site.
import wixEventsFrontend from "wix-events-frontend";
import wixData from "wix-data";
$w.onReady(function () {
let eventId;
// Run a query that returns only one event. Add
// additional filtering to the query if necessary.
wixData
.query("Events/Events")
.eq("title", "My Event")
.find()
.then((results) => {
if (results.items.length > 0) {
eventId = results.items[0]._id;
} else {
console.log("Could not find event");
}
});
$w("#submit").onClick(() => {
const formValues = getFormValues();
wixEventsFrontend.rsvp
.createRsvp(eventId, formValues)
.then((result) => {
console.log("RSVP created.");
})
.catch((error) => {
console.log(`Error message: ${error.message}`);
if (error.fields) {
console.log(`Incorrect fields: ${error.fields}`);
}
});
});
});
function getFormValues() {
return [
{ name: "rsvpStatus", value: "YES" },
{ name: "firstName", value: $w("#firstName").value },
{ name: "lastName", value: $w("#lastName").value },
{ name: "email", value: $w("#email").value },
{ name: "custom", value: $w("#foodAllergies").value },
// When a form contains an address, the way you format the
// address information for submission depends on what type
// of input elements you use to gather that information.
// Wix address input element.
{ name: "address", value: $w("#address").value },
// Single element which is not an address
// input element, such as a text input.
{ name: "address", value: [$w("#address").value] },
// Multiple elements for the
// various parts of an address.
{
name: "address",
value: [
$w("#street").value,
$w("#city").value,
$w("#state").value,
$w("#country").value,
$w("#postalCode").value,
],
},
// When a form contains an input for adding more guests to an
// RSVP, format the guest names for submission in an array
// where each element is the full name of a guest.
{ name: "additionalGuests", value: $w("#additionalGuests").value },
{
name: "guestNames",
value: [
`${$w("#guest1FirstName").value} ${$w("#guest1LastName").value}`,
`${$w("#guest2FirstName").value} ${$w("#guest2LastName").value}`,
],
},
];
}