This commit is contained in:
Martin Slachta
2026-06-11 19:03:29 +02:00
commit 0d829845c4
150 changed files with 38582 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
# JS Data Sources
The JavaScript *RSV* Client layer. The frontend uses `RsvDataSource` to work with the Reservair's REST API.
The `RsvDataSource` is an object for a specific resource & contains methods for working with it.
+44
View File
@@ -0,0 +1,44 @@
const RsvDataSource = {
create_rsv_resource(base_url, { nonce } = {}) {
function request(url, method, body) {
const headers = { 'Content-Type': 'application/json' };
if (nonce) headers['X-WP-Nonce'] = nonce;
else headers['X-WP-Nonce'] = ReservairServiceAPI.nonce;
return fetch(url, {
method,
credentials: 'same-origin',
headers,
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
}).then(r => {
if (!r.ok) throw new Error(`${method} ${url} failed: ${r.status}`);
return r.status === 204 ? null : r.json();
});
}
return {
base_url: base_url,
get_page(skip = 0, limit = 20, params = {}) {
const url = new URL(base_url);
url.searchParams.set('skip', skip);
url.searchParams.set('limit', limit);
for (const [k, v] of Object.entries(params)) {
url.searchParams.set(k, v);
}
return request(url, 'GET');
},
get(id) {
return request(`${base_url}/${id}`, 'GET');
},
post(data) {
return request(base_url, 'POST', data);
},
put(id, data) {
return request(`${base_url}/${id}`, 'PUT', data);
},
delete(id) {
return request(`${base_url}/${id}`, 'DELETE');
},
};
}
};
@@ -0,0 +1,2 @@
const RsvFormDefinitionResource = () =>
RsvDataSource.create_rsv_resource(ReservairServiceAPI.restUrl + '/form-definition');
@@ -0,0 +1,19 @@
const RsvReservationClient = {
accept(reservation_id) {
return this._post(reservation_id, 'accept');
},
refuse(reservation_id) {
return this._post(reservation_id, 'refuse');
},
_post(reservation_id, action) {
return fetch(`${ReservairServiceAPI.restUrl}/reservation/${reservation_id}/${action}`, {
method: 'POST',
credentials: 'same-origin',
headers: { 'X-WP-Nonce': ReservairServiceAPI.nonce },
}).then(r => {
if (!r.ok) return r.json().then(e => { throw new Error(e.error || 'Request failed'); });
});
},
};
@@ -0,0 +1,2 @@
const RsvReservationResource = () =>
RsvDataSource.create_rsv_resource(ReservairServiceAPI.restUrl + '/reservation');
@@ -0,0 +1,2 @@
const RsvTimetableCapacityResource = (id) =>
RsvDataSource.create_rsv_resource(ReservairServiceAPI.restUrl + `/timetable/${id}/capacity`);
@@ -0,0 +1,2 @@
const RsvTimetableReservationResource = (id) =>
RsvDataSource.create_rsv_resource(ReservairServiceAPI.restUrl + `/timetable/${id}/reservation`);
@@ -0,0 +1,2 @@
const RsvTimetableResource = () =>
RsvDataSource.create_rsv_resource(ReservairServiceAPI.restUrl + '/timetable');