Athenaeum/static/js/profile.js

81 lines
2.8 KiB
JavaScript
Raw Normal View History

2025-06-01 11:09:37 +02:00
document.addEventListener('DOMContentLoaded', function() {
loadOrderHistory();
});
2025-05-30 18:00:52 +02:00
2025-06-01 11:09:37 +02:00
async function loadOrderHistory() {
const orderHistoryContainer = document.getElementById('order-history');
if (!orderHistoryContainer) return;
orderHistoryContainer.innerHTML = `
<div class="text-center py-5">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Ładowanie...</span>
</div>
</div>
`;
2025-05-25 16:25:22 +02:00
try {
2025-05-25 16:54:16 +02:00
const response = await fetch('/api/order-history', {
2025-06-01 11:09:37 +02:00
credentials: 'include'
2025-05-25 16:54:16 +02:00
});
2025-05-25 16:25:22 +02:00
2025-06-01 11:09:37 +02:00
if (!response.ok) {
throw new Error('Błąd ładowania historii zamówień');
}
2025-05-25 16:54:16 +02:00
const orders = await response.json();
2025-06-01 11:09:37 +02:00
displayOrderHistory(orders);
2025-05-25 16:25:22 +02:00
} catch (error) {
2025-06-01 11:09:37 +02:00
console.error('Błąd:', error);
orderHistoryContainer.innerHTML = `
<div class="alert alert-danger">Wystąpił błąd podczas ładowania historii zamówień</div>
`;
2025-05-25 16:25:22 +02:00
}
2025-06-01 11:09:37 +02:00
}
2025-05-30 18:00:52 +02:00
2025-06-01 11:09:37 +02:00
function displayOrderHistory(orders) {
const orderHistoryContainer = document.getElementById('order-history');
if (!orderHistoryContainer) return;
if (orders.length === 0) {
orderHistoryContainer.innerHTML = `
<div class="alert alert-info">Brak historii zamówień</div>
`;
return;
}
orderHistoryContainer.innerHTML = '';
orders.forEach(order => {
2025-05-30 18:00:52 +02:00
const orderDate = new Date(order.data_zamowienia).toLocaleDateString();
2025-06-01 11:09:37 +02:00
const orderElement = `
<div class="card mb-3 order-card">
<div class="card-header">
<h5>Zamówienie #${order.id} - ${orderDate}</h5>
<p class="mb-0">Status: ${order.status || 'W realizacji'}</p>
<p class="mb-0">Suma: ${order.suma_totalna} PLN</p>
<p class="mb-0">Typ dostawy: ${order.typ_dostawy === 'local' ? 'Odbiór lokalny' : 'Dostawa'}</p>
2025-05-30 18:00:52 +02:00
</div>
2025-06-01 11:09:37 +02:00
<div class="card-body">
<h6>Produkty:</h6>
<ul class="list-group">
${order.items.map(item => `
<li class="list-group-item d-flex justify-content-between align-items-center">
<div>
<strong>${item.tytul}</strong> - ${item.autor}
</div>
<div>
${item.ilosc} x ${item.cena} PLN
</div>
</li>
`).join('')}
</ul>
2025-05-30 18:00:52 +02:00
</div>
</div>
`;
2025-06-01 11:09:37 +02:00
orderHistoryContainer.innerHTML += orderElement;
2025-05-30 18:00:52 +02:00
});
}