Files
Reservair/src/components/admin.js
T

150 lines
4.2 KiB
JavaScript
Raw Normal View History

2026-06-11 19:03:29 +02:00
async function fetch_reservations_to_confirm(object_id) {
const url = `/wordpress/wp-json/reservations/v1/object/${object_id}/timetable/reservation/unconfirmed`;
return await fetch(url, {
method: 'GET'
}).then(x => x.json());
}
async function confirm_reservation(confirmation_code) {
const url = `/wordpress/wp-json/reservations/v1/accept/${confirmation_code}`;
const x = await fetch(url, {
method: 'GET'
});
}
async function refuse_reservation(confirmation_code) {
const url = `/wordpress/wp-json/reservations/v1/refuse/${confirmation_code}`;
const x = await fetch(url, {
method: 'GET'
});
}
async function render_unconfirmed_reservations_table(self) {
const reservations = await fetch_reservations_to_confirm(self.object_id);
const rows = reservations.map(reservation => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${reservation.date}</td>
<td>${get_format_time(new Date(`${reservation.date}T${reservation.start}`))}</td>
<td>${get_format_time(add_minutes(new Date(`${reservation.date}T${reservation.start}`), parseInt(reservation.num_minutes)))}</td>
<td>${reservation.num_minutes} min</td>
<td>${reservation.email}</td>
`;
let td = document.createElement('td');
let confirm_button = document.createElement('button');
confirm_button.classList.add('button');
confirm_button.classList.add('button-primary');
confirm_button.onclick = function() {
confirm_reservation(reservation.confirmation_code)
.then(x => self.refresh());
};
confirm_button.innerText = "Confirm";
td.appendChild(confirm_button);
let refuse_button = document.createElement('button');
refuse_button.classList.add('button');
refuse_button.classList.add('button-secondary');
refuse_button.onclick = function() {
confirm_reservation(reservation.confirmation_code)
.then(x => self.refresh());
};
refuse_button.innerText = "Refuse";
td.appendChild(refuse_button);
row.appendChild(td);
return row;
});
self.body.replaceChildren(...rows);
}
function create_unconfirmed_reservations(object_id, container) {
let table = document.createElement('table');
table.classList.add('widefat');
let header = document.createElement('thead');
header.innerHTML = `
<tr>
<th>Date</th>
<th>From</th>
<th>To</th>
<th>Length</th>
<th>Email</th>
<th>Actions</th>
</tr>
`;
table.appendChild(header);
let body = document.createElement('tbody');
table.appendChild(body);
container.appendChild(table);
return {
object_id: object_id,
container: container,
body: body,
refresh() {
render_unconfirmed_reservations_table(this);
}
};
}
function create_notice(id, type, mesg) {
let container = document.createElement('div');
container.id = id;
container.classList.add('notice', `notice-${type}`, 'settings-error', 'is-dismissible');
container.innerHTML = `<p><strong>${mesg}</strong></p>`;
return container;
}
function show_notice(target, type, mesg) {
target.querySelectorAll('.notice').forEach(x => x.remove());
const notice = create_notice('test', type, mesg);
target.prepend(notice);
}
async function error_handler(error) {
if(error.body != null && error.body.message != null) {
show_notice(error.target, 'error', error.body.message);
}
console.error(error);
}
async function delete_action(id) {
return await fetch(get_rest_url(`action/${id}`), {
method: 'DELETE'
});
}
function collect_selected_actions(target) {
return Array.from(target.querySelectorAll('input[type="checkbox"].action-selector:checked')).map(x =>
x.value
);
}
async function delete_object(id) {
return await fetch(get_rest_url(`object/${id}`), {
method: 'DELETE'
});
}
async function set_object_actions(id, actions) {
return await fetch(get_rest_url(`object/${id}/action`), {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(actions)
});
}
function inline_edit_row() {
let row = document.createElement('tr');
row.classList.add('iedit author-self level-0 post-1 type-post status-publish format-standard hentry category-uncategorized');
return row;
}