Skip to content

Commit 47ee457

Browse files
authored
Merge pull request #5 from DHTMLX/next
merge next to master
2 parents 6f0a2b1 + 9b0cccc commit 47ee457

5 files changed

Lines changed: 112 additions & 29 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/loading-data.md

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ description: You can learn how to load data into Booking in the documentation of
66

77
# Loading data
88

9-
## Preparing data
10-
119
The following information can be loaded into Booking:
1210

1311
- [`data`](/api/config/booking-data) - an array of objects containing cards data
@@ -93,8 +91,6 @@ const data = [
9391
];
9492
~~~
9593

96-
## Loading data
97-
9894
You can load JSON data into Booking from an external file or the server-side script after the component has been initialized.
9995

10096
To load local data from a separate file, first prepare the source file with data.
@@ -163,22 +159,9 @@ const { data } = getData();
163159
const booking = new booking.Booking("#root", { data });
164160
~~~
165161

166-
To get server data, you can send the request for data using the native **fetch** method (or any other way):
162+
About loading data from the server, refer to [Working with server](/guides/saving-reservations)
167163

168-
~~~jsx {}
169-
const booking = new booking.Booking("#booking", {data: []});
170-
const server = "https://some-backend-url";
171-
172-
fetch(server + "/data").then((res) => res.json()).then((data) => {
173-
booking.setConfig({data});
174-
});
175-
~~~
176-
177-
## Example
178-
179-
The snippet below shows how to save slots reservations to the server:
180-
181-
<iframe src="https://snippet.dhtmlx.com/dpbmyr8j?mode=result" frameborder="0" class="snippet_iframe" width="100%" height="600"></iframe>
164+
---
182165

183166
**Related articles**:
184167
- [confirm-slot](/api/events/booking-confirmslot-event) event

docs/guides/saving-reservations.md

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
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

9-
About loading data from the server see here: [Loading data](/guides/loading-data#loading-data-1).
9+
## Loading data
10+
11+
To get server data, you can send the request for data using the native **fetch** method (or any other way):
12+
13+
~~~jsx {}
14+
const booking = new booking.Booking("#booking", {data: []});
15+
const server = "https://some-backend-url";
16+
17+
fetch(server + "/data").then((res) => res.json()).then((data) => {
18+
booking.setConfig({data});
19+
});
20+
~~~
21+
22+
## Saving slots reservations to the server
1023

1124
To handle slots reservation, you should apply the [`setConfirmHandler`](/api/methods/booking-setconfirmhandler-method) method.
1225

@@ -51,6 +64,93 @@ fetch("/server/url")
5164
});
5265
~~~
5366

67+
## Working with UTC data
68+
69+
The widget applies a local timezone but if you have UTC data it's necessary to convert data to a local timezone.
70+
71+
For example, if you have UTC timestamps, you can apply the functions provided in the example below to convert them. 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.
72+
73+
~~~jsx
74+
const serverURL = "https://some-backend-url";
75+
76+
function g2l(v) {
77+
const utcDate = new Date(v);
78+
return new Date(
79+
utcDate.getUTCFullYear(),
80+
utcDate.getUTCMonth(),
81+
utcDate.getUTCDate(),
82+
utcDate.getUTCHours(),
83+
utcDate.getUTCMinutes(),
84+
utcDate.getUTCSeconds(),
85+
utcDate.getUTCMilliseconds(),
86+
).valueOf();
87+
}
88+
89+
function l2g(v) {
90+
const date = new Date(v);
91+
return Date.UTC(
92+
date.getFullYear(),
93+
date.getMonth(),
94+
date.getDate(),
95+
date.getHours(),
96+
date.getMinutes(),
97+
date.getSeconds(),
98+
date.getMilliseconds(),
99+
);
100+
}
101+
102+
const handleSlotReservation = event => {
103+
const { confirm, slot, data } = event;
104+
105+
const info = {
106+
doctor: slot.id,
107+
date: l2g(slot.time[0]),
108+
form: {
109+
name: data.name,
110+
email: data.email,
111+
details: data.description,
112+
},
113+
};
114+
115+
fetch( serverURL + "/doctors/reservations", {
116+
method: "POST",
117+
body: JSON.stringify(info),
118+
}).then(response => {
119+
if (response.ok) confirm.done();
120+
else confirm.error();
121+
});
122+
};
123+
124+
// widget initialization
125+
const widget = new booking.Booking("#root", {
126+
data: [],
127+
});
128+
129+
// data loading
130+
fetch( serverURL + "/units")
131+
.then(res => res.json())
132+
.then(units => {
133+
units.forEach(unit => {
134+
if (unit.usedSlots) unit.usedSlots = unit.usedSlots.map(g2l);
135+
if (unit.slots) {
136+
unit.slots = unit.slots.map(slot => {
137+
if (slot.dates) {
138+
return {
139+
...slot,
140+
dates: slot.dates.map(g2l)
141+
};
142+
}
143+
return slot;
144+
});
145+
};
146+
});
147+
148+
widget.setConfig({ data: units });
149+
widget.setConfirmHandler(handleSlotReservation);
150+
});
151+
~~~
152+
153+
54154
## Example
55155

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

0 commit comments

Comments
 (0)