Skip to content

Commit 774c2ab

Browse files
committed
[update] convert_timestamps added
1 parent 6f0a2b1 commit 774c2ab

4 files changed

Lines changed: 100 additions & 9 deletions

File tree

docs/api/config/booking-data.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ For each card object you can specify the following parameters:
6969
- `size` - (optional) the duration of one slot in minutes
7070
- `gap` - (optional) the gap between slots in minutes; 0 is set by default
7171
- `days` - (optional) days of the week when a slot is available for booking; possible values: from 0 to 6 where 0 is Sunday and 6 is Saturday; if no days are specified, all days are applied by default; if days are specified, the slot parameters (**to**, **from**, **size**, **gap**) defined for these days will be applied
72-
- `dates` - (optional) an array of timestamps in milliseconds which are exact dates when a slot is available; the slot parameters (**to**, **from**, **size**, **gap**) for these specified dates will be applied
72+
- `dates` - (optional) an array of timestamps in milliseconds which are exact dates when a slot is available; the slot parameters (**to**, **from**, **size**, **gap**) for these specified dates will be applied (timestamps are in a local timezone)
7373

7474
:::note
7575
Slot parameters specified for days will override common parameters defined for all days.
@@ -79,8 +79,8 @@ If several slots objects are created for the same day, make sure that slots time
7979

8080
- `availableSlots` - (optional) an array of timestamps of available slots in milliseconds; if available slots are specified here, all slots from the `slots` array are ignored (i.e., become unavailable); each object in the array has the next parameters:
8181
- `id` - (required) the id of a slot
82-
- `time` - (required) an array that includes timestamp and slot duration in minutes
83-
- `usedSlots` - (optional) an array of timestamps of booked slots in milliseconds
82+
- `time` - (required) an array that includes timestamp and slot duration in minutes (timestamps are in a local timezone)
83+
- `usedSlots` - (optional) an array of timestamps of booked slots in milliseconds (timestamps are in a local timezone)
8484
- `slotSize` - (optional) the duration of a slot in minutes; the value will be applied to all slots of this card if other value is not set inside the `slots` object; *60* minutes is set by default
8585
- `slotGap` - (optional) the gap between slots in minutes that is set for all slots in the current card; this value is applied if any other value is not specified inside the `slots` object; 0 is set by default
8686

docs/api/events/booking-confirmslot-event.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The callback of the **confirm-slot** event can take an object with the following
3535

3636
- `slot` - (required) an object with the next slot parameters:
3737
- `id` - (required) the ID of a card for which the booking of a slot is confirmed
38-
- `time` - (required) an array with the slot start time in milliseconds and the slot duration in minutes
38+
- `time` - (required) an array with the slot start time in milliseconds and the slot duration in minutes (timestamps are in a local timezone)
3939
- `data` - (required) an abject with the booking screen fields with the following parameters for each field:
4040
- `key` - (required) the form field ID (from the [`formShape`](/api/config/booking-formshape)). By default, three fields are added: *name*, *email*, *description*
4141
- `confirm` - (required) an object with the next parameters:

docs/api/events/booking-selectslot-event.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ description: You can learn about the select-slot event in the documentation of t
2424
The callback of the **select-slot** event can take an object with the following parameters:
2525

2626
- `id` - (required) the ID of a card the selected slot belongs to
27-
- `time` - (required) an array with the slot start time in milliseconds and the slot duration in minutes
27+
- `time` - (required) an array with the slot start time in milliseconds and the slot duration in minutes (timestamps are in a local timezone)
2828

2929
### Example
3030

docs/guides/saving-reservations.md

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
---
2-
sidebar_label: Saving slots reservations to server
3-
title: Saving slots reservations to the server
4-
description: You can learn about saving slots reservations to server in the documentation of the DHTMLX JavaScript Booking library. Browse developer guides and API reference, try out code examples and live demos, and download a free 30-day evaluation version of DHTMLX Booking.
2+
sidebar_label: Working with server
3+
title: Working with server
4+
description: You can learn about working with server in the documentation of the DHTMLX JavaScript Booking library. Browse developer guides and API reference, try out code examples and live demos, and download a free 30-day evaluation version of DHTMLX Booking.
55
---
66

7-
# Saving slots reservations to the server
7+
# Working with server
88

99
About loading data from the server see here: [Loading data](/guides/loading-data#loading-data-1).
1010

11+
## Saving slots reservations to the server
12+
1113
To handle slots reservation, you should apply the [`setConfirmHandler`](/api/methods/booking-setconfirmhandler-method) method.
1214

1315
~~~jsx {}
@@ -51,6 +53,95 @@ fetch("/server/url")
5153
});
5254
~~~
5355

56+
## Converting timestamps
57+
58+
In case the widget is applied in different timezones, you can convert timestamps when loading data and sending data to the server.
59+
60+
The next example demonstrates how to convert a UTC timestamp into the local timezone during the loading process and how to convert the local time back to UTC when sending data to the server.
61+
62+
In the example below, the **g2l** function converts a UTC timestamp into the local timezone. During the data loading process, this function is used to convert the times in *usedSlots* and *slots* from UTC to the local time. The **l2g** function converts a local time back to UTC. It's applied during slots reservation, namely, the **l2g** function is used to convert the local time (from slot.time[0]) to UTC before sending it to the server.
63+
64+
~~~jsx
65+
const serverURL = "https://some-backend-url";
66+
67+
function g2l(v) {
68+
const utcDate = new Date(v);
69+
return new Date(
70+
utcDate.getUTCFullYear(),
71+
utcDate.getUTCMonth(),
72+
utcDate.getUTCDate(),
73+
utcDate.getUTCHours(),
74+
utcDate.getUTCMinutes(),
75+
utcDate.getUTCSeconds(),
76+
utcDate.getUTCMilliseconds(),
77+
).valueOf();
78+
}
79+
80+
function l2g(v) {
81+
const date = new Date(v);
82+
return Date.UTC(
83+
date.getFullYear(),
84+
date.getMonth(),
85+
date.getDate(),
86+
date.getHours(),
87+
date.getMinutes(),
88+
date.getSeconds(),
89+
date.getMilliseconds(),
90+
);
91+
}
92+
93+
const handleSlotReservation = event => {
94+
const { confirm, slot, data } = event;
95+
96+
const info = {
97+
doctor: slot.id,
98+
date: l2g(slot.time[0]),
99+
form: {
100+
name: data.name,
101+
email: data.email,
102+
details: data.description,
103+
},
104+
};
105+
106+
fetch( serverURL + "/doctors/reservations", {
107+
method: "POST",
108+
body: JSON.stringify(info),
109+
}).then(response => {
110+
if (response.ok) confirm.done();
111+
else confirm.error();
112+
});
113+
};
114+
115+
// widget initialization
116+
const widget = new booking.Booking("#root", {
117+
data: [],
118+
});
119+
120+
// data loading
121+
fetch( serverURL + "/units")
122+
.then(res => res.json())
123+
.then(units => {
124+
units.forEach(unit => {
125+
if (unit.usedSlots) unit.usedSlots = unit.usedSlots.map(g2l);
126+
if (unit.slots) {
127+
unit.slots = unit.slots.map(slot => {
128+
if (slot.dates) {
129+
return {
130+
...slot,
131+
dates: slot.dates.map(g2l)
132+
};
133+
}
134+
return slot;
135+
});
136+
};
137+
});
138+
139+
widget.setConfig({ data: units });
140+
widget.setConfirmHandler(handleSlotReservation);
141+
});
142+
~~~
143+
144+
54145
## Example
55146

56147
<iframe src="https://snippet.dhtmlx.com/dpbmyr8j?mode=result" frameborder="0" class="snippet_iframe" width="100%" height="600"></iframe>

0 commit comments

Comments
 (0)