Skip to main content

Saving slots reservations to the server

About loading data from the server see here: Loading data.

To handle slots reservation, you should apply the setConfirmHandler method.

// create a function to handle the logic of reservation
const handleSlotReservation = (event) => {
const { confirm, slot, data } = event;

// create the info object
const info = {
item: slot.id,
start: slot.time[0],
data
};

// send the POST request to the server with the info object in the request body
fetch("/server/url", {
method: "POST",
body: JSON.stringify(info),
// handle the response
}).then((response) => {
if (response.ok) confirm.done();
else confirm.error();
});
};

// create Booking
const booking = new booking.Booking("#root", {
data: [],
// configuration parameters
});

// fetch available data from the server and convert it to JSON
fetch("/server/url")
.then((res) => res.json())
.then((items) => {
// update Booking configuration with the fetched items, allowing the widget to display them
booking.setConfig({ data: items });
// assign the handleSlotReservation function to be called when a user confirms booking,
// link user actions to the reservation logic
booking.setConfirmHandler(handleSlotReservation);
});

Example

Related articles: