Files
shopApiNg/index.html

323 lines
10 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Category Tree Viewer</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 2rem;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: rgba(255, 255, 255, 0.95);
border-radius: 16px;
padding: 2rem;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
h1 {
color: #667eea;
margin-bottom: 2rem;
font-size: 2.5rem;
text-align: center;
}
.tree {
list-style: none;
}
.tree ul {
list-style: none;
padding-left: 2rem;
}
.category {
margin: 0.5rem 0;
padding: 0.75rem;
background: white;
border-radius: 8px;
border-left: 4px solid #667eea;
transition: all 0.3s ease;
cursor: pointer;
}
.category:hover {
transform: translateX(4px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}
.category-header {
display: flex;
align-items: center;
gap: 1rem;
}
.category-image {
width: 48px;
height: 48px;
object-fit: cover;
border-radius: 6px;
background: #f0f0f0;
}
.category-info {
flex: 1;
}
.category-name {
font-weight: 600;
color: #333;
font-size: 1.1rem;
}
.category-count {
color: #666;
font-size: 0.9rem;
margin-top: 0.25rem;
}
.toggle {
color: #667eea;
font-weight: bold;
margin-right: 0.5rem;
user-select: none;
}
.children {
margin-top: 0.5rem;
}
.children.hidden {
display: none;
}
.loading {
text-align: center;
padding: 3rem;
color: #667eea;
font-size: 1.2rem;
}
.error {
background: #fee;
color: #c33;
padding: 1rem;
border-radius: 8px;
text-align: center;
}
.category-products {
margin-top: 0.5rem;
padding-left: 4rem;
font-size: 0.9rem;
color: #555;
}
.product-item {
padding: 0.25rem 0;
border-bottom: 1px solid #eee;
}
.product-item:last-child {
border-bottom: none;
}
</style>
</head>
<body>
<div class="container">
<h1>📦 Category Tree</h1>
<div id="tree-container">
<div class="loading">Loading categories...</div>
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io({
transports: ['websocket']
});
socket.on('connect', () => {
console.log('🔌 Connected to server via WebSocket');
});
socket.on('categoriesUpdated', () => {
console.log('🔄 Categories updated, reloading tree...');
loadCategories();
});
socket.on('categoryProductsUpdated', ({ id }) => {
console.log(`🔄 Products for category ${id} updated, reloading...`);
// Find the specific category element and reload its products
// Since we don't have easy access to instances, we could reload the whole tree
// or dispatch a custom event. For simplicity, we'll reload the tree for now
// but ideally we'd target the specific DOM element.
// Better approach: emit event that specific components can listen to
document.dispatchEvent(new CustomEvent('productsUpdated', { detail: { id } }));
});
async function loadCategories() {
try {
const response = await fetch('/api/categories');
if (!response.ok) throw new Error('Failed to load categories');
const categories = await response.json();
renderTree(categories);
} catch (err) {
document.getElementById('tree-container').innerHTML =
`<div class="error">❌ ${err.message}</div>`;
}
}
function renderTree(categories) {
const container = document.getElementById('tree-container');
container.innerHTML = '';
const ul = document.createElement('ul');
ul.className = 'tree';
categories.forEach(cat => {
ul.appendChild(renderCategory(cat));
});
container.appendChild(ul);
}
function renderCategory(category) {
const li = document.createElement('li');
const div = document.createElement('div');
div.className = 'category';
const header = document.createElement('div');
header.className = 'category-header';
// Toggle for children
if (category.children && category.children.length > 0) {
const toggle = document.createElement('span');
toggle.className = 'toggle';
toggle.textContent = '▼';
header.appendChild(toggle);
}
// Image
if (category.kBild) {
const img = document.createElement('img');
img.className = 'category-image';
img.src = `/img/cat/${category.kBild}.avif`;
img.alt = category.cName;
img.onerror = () => img.style.display = 'none';
header.appendChild(img);
}
// Info
const info = document.createElement('div');
info.className = 'category-info';
const name = document.createElement('div');
name.className = 'category-name';
name.textContent = category.cName;
info.appendChild(name);
const count = document.createElement('div');
count.className = 'category-count';
count.textContent = `${category.articleCount} articles`;
info.appendChild(count);
header.appendChild(info);
div.appendChild(header);
// Products
const productsDiv = document.createElement('div');
productsDiv.className = 'category-products';
productsDiv.innerHTML = '<small>Loading products...</small>';
div.appendChild(productsDiv);
// Load products
const loadProducts = () => {
productsDiv.innerHTML = '<small>Loading products...</small>';
productsDiv.style.display = 'block';
fetch(`/api/categories/${category.kKategorie}/products`)
.then(res => res.ok ? res.json() : [])
.then(products => {
productsDiv.innerHTML = '';
if (products.length === 0) {
productsDiv.style.display = 'none';
return;
}
const ul = document.createElement('ul');
ul.style.listStyle = 'none';
products.slice(0, 3).forEach(p => {
const li = document.createElement('li');
li.className = 'product-item';
li.textContent = `📦 ${p.cName}`;
ul.appendChild(li);
});
if (products.length > 3) {
const more = document.createElement('li');
more.className = 'product-item';
more.style.fontStyle = 'italic';
more.textContent = `...and ${products.length - 3} more`;
ul.appendChild(more);
}
productsDiv.appendChild(ul);
})
.catch(() => {
productsDiv.style.display = 'none';
});
};
loadProducts();
// Listen for updates
const updateHandler = (e) => {
if (e.detail.id === category.kKategorie) {
console.log(`✨ refreshing products for category ${category.kKategorie}`);
loadProducts();
}
};
document.addEventListener('productsUpdated', updateHandler);
// Children
if (category.children && category.children.length > 0) {
const childrenUl = document.createElement('ul');
childrenUl.className = 'children';
category.children.forEach(child => {
childrenUl.appendChild(renderCategory(child));
});
div.appendChild(childrenUl);
// Toggle functionality
div.addEventListener('click', (e) => {
if (e.target.closest('.children')) return;
e.stopPropagation();
childrenUl.classList.toggle('hidden');
const toggle = div.querySelector('.toggle');
if (toggle) {
toggle.textContent = childrenUl.classList.contains('hidden') ? '▶' : '▼';
}
});
}
li.appendChild(div);
return li;
}
loadCategories();
</script>
</body>
</html>