This commit is contained in:
2026-08-31 20:16:08 +02:00
parent fa79880771
commit eb8ff84739
10 changed files with 389 additions and 582 deletions
+28
View File
@@ -0,0 +1,28 @@
const form = document.querySelector('#review-form');
const statusBox = document.querySelector('#status');
const resultBox = document.querySelector('#result');
const button = form.querySelector('button');
form.addEventListener('submit', async (event) => {
event.preventDefault(); button.disabled = true; resultBox.hidden = true;
statusBox.className = 'status'; statusBox.textContent = 'Odczytuję dokładne liczniki…';
try {
const response = await fetch('/api/reviews', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({url: document.querySelector('#maps-url').value})});
const data = await response.json();
if (!response.ok) throw new Error(data.error || 'Nie udało się pobrać danych.');
document.querySelector('#total').textContent = data.total.toLocaleString('pl-PL');
document.querySelector('#average').textContent = data.average.toLocaleString('pl-PL', {minimumFractionDigits: 2});
const max = Math.max(...Object.values(data.ratings), 1);
const rows = Object.entries(data.ratings).map(([stars, count]) => {
const row = document.createElement('div'); row.className = 'rating-row';
const label = document.createElement('span'); label.textContent = `${stars}`;
const bar = document.createElement('div'); bar.className = 'bar';
const fill = document.createElement('i'); fill.style.width = `${count / max * 100}%`; bar.append(fill);
const value = document.createElement('strong'); value.textContent = count.toLocaleString('pl-PL');
row.append(label, bar, value); return row;
});
document.querySelector('#ratings').replaceChildren(...rows);
statusBox.textContent = ''; resultBox.hidden = false;
} catch (error) { statusBox.className = 'status error'; statusBox.textContent = error.message; }
finally { button.disabled = false; }
});