Skip to main content

DHTMLX Booking. Backend integration example

Live example​

A booking screen holds two conversations with a server: it reads what is free and it writes what was taken. setConfig({ data }) fills the widget once the request returns, and setConfirmHandler posts the reservation and reports the answer back with confirm.done() or confirm.error(). This demo runs both against a live REST backend.

//Check Go backend repository here - https://github.com/DHTMLX/booking-go
const serverURL = "https://docs.dhtmlx.com/booking-backend";

function g2l(v) {
    const utcDate = new Date(v);
    return new Date(
        utcDate.getUTCFullYear(),
        utcDate.getUTCMonth(),
        utcDate.getUTCDate(),
        utcDate.getUTCHours(),
        utcDate.getUTCMinutes(),
        utcDate.getUTCSeconds(),
        utcDate.getUTCMilliseconds(),
    ).valueOf();
}

function l2g(v) {
    const date = new Date(v);
    return Date.UTC(
        date.getFullYear(),
        date.getMonth(),
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds(),
    );
}

const handleSlotReservation = event => {
    const { confirm, slot, data } = event;

    const info = {
        doctor: slot.id,
        date: l2g(slot.time[0]),
        form: {
            name: data.name,
            email: data.email,
            details: data.description,
        },
    };

    fetch( serverURL + "/doctors/reservations", {
        method: "POST",
        body: JSON.stringify(info),
    }).then(response => {
        if (response.ok) confirm.done();
        else confirm.error();
    });
};

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

// data loading
fetch( serverURL + "/units")
    .then(res => res.json())
    .then(units => {
        units.forEach(unit => {
            if (unit.usedSlots) unit.usedSlots = unit.usedSlots.map(g2l);
            if (unit.slots) {
                unit.slots = unit.slots.map(slot => {
                    if (slot.dates) {
                        return {
                            ...slot,
                            dates: slot.dates.map(g2l)
                        };
                    }
                    return slot;
                });
            };
        });
         
        widget.setConfig({ data: units });
        widget.setConfirmHandler(handleSlotReservation);
    });
<!-- component container -->
<div id="root" style="height:100%"></div>

Use case​

Availability is never a static array in a working system. Someone books through the site, someone else calls the front desk, a third record arrives from a partner integration. So the widget starts empty, asks the server what is free, and sends every confirmation back for the server to judge. That round trip is the only honest place to catch a slot taken while the visitor filled in the form. The demo talks to the Go service booking-go, a working reference for the two endpoints your backend needs.

How to do this in your app​

  • Start empty: new booking.Booking("#root", { data: [] }) renders the page before the request comes back.
  • Load and apply: fetch your records, then hand them over with widget.setConfig({ data: units }).
  • Attach the handler after setConfig: that call mounts a new widget, so call widget.setConfirmHandler(handleSlotReservation) in the same then block. A handler attached earlier is gone.
  • Post the reservation: the handler receives { confirm, slot, data }. slot.id is the card. slot.time is [start, duration]: time[0] is the local start timestamp in milliseconds, and time[1] is the length in minutes. data holds the form under its formShape keys, name, email, and description by default.
  • Answer the widget: call confirm.done() when the response is ok, and confirm.error() when it is not.
  • Keep the same clock time: g2l copies the UTC year, month, day, and clock time into a local timestamp for usedSlots and slot dates, and l2g copies slot.time[0] back before the POST. A slot stored as 09:00 UTC shows as 09:00 for every visitor. That matches a server that stores the clinic's wall time in UTC.

Good to know​

  • setConfig rebuilds the widget: the properties you pass are merged into the current config, then the widget is destroyed and initialized again.
  • The widget thinks in local clock time: g2l and l2g already keep the clinic's hour on the clock. Shifting those timestamps again by the browser's timezone offset puts the slot on a different hour.
  • The dialog has two endings: confirm.done() shows the confirmed message and marks slot.time[0] used. confirm.error() shows the failure message and leaves the grid unchanged. Both texts come from the locale, and a value passed to confirm.error() is not shown.

FAQ​

How do you load booking data from a server?​

Create the widget with data: [], fetch your records, and pass them in with setConfig({ data }). The guide on working with a server uses plain fetch, and any client library works the same way.

How do you save a confirmed booking?​

Handle it in setConfirmHandler. The callback receives the slot and the form, so build your request body from them, POST it to your endpoint, and resolve the dialog with confirm.done() or confirm.error().

What happens when the server rejects a reservation?​

Call confirm.error(). The dialog shows its failure state and asks the visitor to go back. The widget leaves that slot on the grid.

API reference​

Additional resources​