2026-02-14 22:17:32 -08:00
|
|
|
import { init, logout, api } from '/app.js';
|
|
|
|
|
|
|
|
|
|
const profile = await init();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const check = await api('GET', '/api/admin/check');
|
|
|
|
|
if (!check.admin) {
|
|
|
|
|
document.body.style.opacity = 1;
|
|
|
|
|
document.body.textContent = 'Access denied.';
|
|
|
|
|
throw new Error('not admin');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
document.body.style.opacity = 1;
|
|
|
|
|
if (!document.body.textContent) document.body.textContent = 'Access denied.';
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.getElementById('main').style.display = 'block';
|
|
|
|
|
document.getElementById('logout-btn').addEventListener('click', logout);
|
|
|
|
|
|
|
|
|
|
async function loadTrips() {
|
|
|
|
|
const trips = await api('GET', '/api/trips');
|
|
|
|
|
const container = document.getElementById('trips');
|
|
|
|
|
container.innerHTML = '';
|
|
|
|
|
for (const trip of trips) {
|
|
|
|
|
const card = document.createElement('wa-card');
|
2026-02-16 09:50:19 -08:00
|
|
|
card.dataset.tripId = trip.id;
|
2026-02-14 22:27:49 -08:00
|
|
|
|
2026-02-15 17:40:39 -08:00
|
|
|
const nameRow = document.createElement('div');
|
|
|
|
|
nameRow.style.display = 'flex';
|
|
|
|
|
nameRow.style.alignItems = 'center';
|
2026-02-14 22:21:36 -08:00
|
|
|
const tripLink = document.createElement('a');
|
|
|
|
|
tripLink.href = '/trip/' + trip.id;
|
|
|
|
|
tripLink.textContent = trip.name;
|
2026-02-15 17:40:39 -08:00
|
|
|
tripLink.className = 'trip-name';
|
|
|
|
|
tripLink.style.flex = '1';
|
|
|
|
|
const deleteBtn = document.createElement('button');
|
|
|
|
|
deleteBtn.className = 'close-btn';
|
2026-02-14 22:27:49 -08:00
|
|
|
deleteBtn.textContent = '\u00d7';
|
2026-02-14 22:17:32 -08:00
|
|
|
deleteBtn.addEventListener('click', async () => {
|
|
|
|
|
if (!confirm('Delete trip "' + trip.name + '"?')) return;
|
|
|
|
|
await api('DELETE', '/api/trips/' + trip.id);
|
|
|
|
|
loadTrips();
|
|
|
|
|
});
|
2026-02-15 17:40:39 -08:00
|
|
|
nameRow.appendChild(tripLink);
|
|
|
|
|
nameRow.appendChild(deleteBtn);
|
|
|
|
|
card.appendChild(nameRow);
|
2026-02-14 22:17:32 -08:00
|
|
|
|
2026-02-15 17:40:39 -08:00
|
|
|
const details = document.createElement('wa-details');
|
|
|
|
|
details.summary = 'Admins';
|
|
|
|
|
|
|
|
|
|
const tags = document.createElement('div');
|
|
|
|
|
tags.className = 'tags';
|
2026-02-14 22:17:32 -08:00
|
|
|
for (const admin of trip.admins) {
|
2026-02-15 17:40:39 -08:00
|
|
|
const tag = document.createElement('wa-tag');
|
|
|
|
|
tag.size = 'small';
|
|
|
|
|
tag.variant = 'brand';
|
|
|
|
|
tag.setAttribute('with-remove', '');
|
|
|
|
|
tag.textContent = admin.email;
|
|
|
|
|
tag.addEventListener('wa-remove', async () => {
|
2026-02-14 22:17:32 -08:00
|
|
|
await api('DELETE', '/api/trips/' + trip.id + '/admins/' + admin.id);
|
|
|
|
|
loadTrips();
|
|
|
|
|
});
|
2026-02-15 17:40:39 -08:00
|
|
|
tags.appendChild(tag);
|
2026-02-14 22:17:32 -08:00
|
|
|
}
|
2026-02-15 17:40:39 -08:00
|
|
|
details.appendChild(tags);
|
2026-02-14 22:17:32 -08:00
|
|
|
|
|
|
|
|
const input = document.createElement('wa-input');
|
2026-02-15 17:40:39 -08:00
|
|
|
input.placeholder = 'Add admin email';
|
2026-02-14 22:27:49 -08:00
|
|
|
input.size = 'small';
|
2026-02-15 17:40:39 -08:00
|
|
|
input.className = 'email';
|
|
|
|
|
input.style.marginTop = '0.3rem';
|
|
|
|
|
const addBtn = document.createElement('button');
|
|
|
|
|
addBtn.slot = 'end';
|
|
|
|
|
addBtn.className = 'input-action';
|
2026-02-14 22:27:49 -08:00
|
|
|
addBtn.textContent = '+';
|
2026-02-15 17:40:39 -08:00
|
|
|
const doAdd = async () => {
|
2026-02-14 22:17:32 -08:00
|
|
|
const email = input.value.trim();
|
|
|
|
|
if (!email) return;
|
|
|
|
|
await api('POST', '/api/trips/' + trip.id + '/admins', { email });
|
2026-02-16 09:50:19 -08:00
|
|
|
await loadTrips();
|
|
|
|
|
const card = container.querySelector('[data-trip-id="' + trip.id + '"]');
|
|
|
|
|
if (card) {
|
|
|
|
|
const det = card.querySelector('wa-details');
|
|
|
|
|
if (det) det.open = true;
|
|
|
|
|
const inp = card.querySelector('wa-input');
|
|
|
|
|
if (inp) inp.focus();
|
|
|
|
|
}
|
2026-02-15 17:40:39 -08:00
|
|
|
};
|
|
|
|
|
addBtn.addEventListener('click', doAdd);
|
|
|
|
|
input.addEventListener('keydown', (e) => { if (e.key === 'Enter') doAdd(); });
|
|
|
|
|
input.appendChild(addBtn);
|
|
|
|
|
details.appendChild(input);
|
2026-02-14 22:17:32 -08:00
|
|
|
|
2026-02-15 17:40:39 -08:00
|
|
|
card.appendChild(details);
|
2026-02-14 22:17:32 -08:00
|
|
|
container.appendChild(card);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-15 17:40:39 -08:00
|
|
|
async function createTrip() {
|
2026-02-14 22:17:32 -08:00
|
|
|
const input = document.getElementById('new-trip-name');
|
|
|
|
|
const name = input.value.trim();
|
|
|
|
|
if (!name) return;
|
|
|
|
|
await api('POST', '/api/trips', { name });
|
|
|
|
|
input.value = '';
|
|
|
|
|
loadTrips();
|
2026-02-15 17:40:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.getElementById('create-trip-btn').addEventListener('click', createTrip);
|
|
|
|
|
document.getElementById('new-trip-name').addEventListener('keydown', (e) => { if (e.key === 'Enter') createTrip(); });
|
2026-02-14 22:17:32 -08:00
|
|
|
|
|
|
|
|
await loadTrips();
|
2026-02-15 18:09:58 -08:00
|
|
|
await customElements.whenDefined('wa-button');
|
2026-02-14 22:17:32 -08:00
|
|
|
document.body.style.opacity = 1;
|