<!doctype html><html lang="de"><head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>Sofortkauf Demo</title> <style> body { font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial; background:#f4f6f8; margin:0; padding:24px; } .grid { display:grid; grid-template-columns: repeat(auto-fit,minmax(220px,1fr)); gap:16px; max-width:1000px; margin:0 auto; } .card { background:white; border-radius:12px; box-shadow:0 6px 18px rgba(20,20,40,0.06); padding:12px; cursor:pointer; transition:transform .12s ease, box-shadow .12s ease; user-select:none; } .card:hover { transform:translateY(-4px); box-shadow:0 10px 30px rgba(20,20,40,0.08); } .img { width:100%; height:140px; border-radius:8px; background-size:cover; background-position:center; margin-bottom:10px; } .title { font-weight:600; margin-bottom:6px; } .price { color:#0b8457; font-weight:700; } .meta { font-size:13px; color:#666; margin-top:6px; } .badge { display:inline-block; background:#ffeecf; color:#7a4b00; padding:4px 8px; border-radius:999px; font-size:12px; margin-top:8px; } /* Toast */ .toast-container { position:fixed; right:16px; bottom:16px; z-index:9999; display:flex; flex-direction:column; gap:8px; } .toast { background:#111827; color:white; padding:12px 16px; border-radius:10px; min-width:220px; box-shadow:0 6px 20px rgba(0,0,0,0.16); display:flex; gap:8px; align-items:center; } .toast.success { background:#065f46; } .toast.error { background:#7f1d1d; } .small { font-size:13px; color:#e6e6e6; } /* Small control area */ .controls { max-width:1000px; margin:18px auto 24px; display:flex; gap:10px; align-items:center; } .btn { background:#111827; color:white; padding:8px 12px; border-radius:8px; border:none; cursor:pointer; } .btn.secondary { background:#e6e6e6; color:#111827; } .note { color:#444; font-size:14px; } </style></head><body><h1>Sofortkauf Demo — Klick auf ein Produkt</h1><div class="controls"> <button id="enableNotifyBtn" class="btn">System-Benachrichtigungen aktivieren</button> <button id="simulateServerBtn" class="btn secondary">Server-Simulation: AN</button> <div style="flex:1"></div> <div class="note">Klicke ein Produkt, um "Sofortkauf" auszulösen.</div></div><div class="grid" id="productGrid"> <!-- Produkte werden per JS eingefügt --></div><div class="toast-container" id="toastContainer" aria-live="polite" aria-atomic="true"></div><script>/* ============================ Konfiguration / Beispiel-Produkte ============================ */const PRODUCTS = [ { id: 'p1', title: 'Cody Rhodes', price: 'Cody Rhodes', img: 'https://picsum.photos/seed/headphones/800/600' }, { id: 'p2', title: 'Smartwatch Pro', price: '199,00 €', img: 'https://picsum.photos/seed/watch/800/600' }, { id: 'p3', title: 'Gaming-Tastatur', price: '129,50 €', img: 'https://picsum.photos/seed/keyboard/800/600' }, { id: 'p4', title: 'Kaffeevollautomat', price: '549,00 €', img: 'https://picsum.photos/seed/coffee/800/600' },];let simulateServer = true; // Falls kein Backend vorhanden: simulation=true/* ============================ DOM Aufbau ============================ */const grid = document.getElementById('productGrid');PRODUCTS.forEach(p => { const card = document.createElement('article'); card.className = 'card'; card.tabIndex = 0; card.dataset.productId = p.id; card.innerHTML = ` <div class="img" style="background-image:url('${p.img}');"></div> <div class="title">${p.title}</div> <div class="price">${p.price}</div> <div class="meta">Sofortkauf: Klick auf die Karte</div> <div class="badge">Sofortkauf</div> `; // Klick / Enter löst Sofortkauf aus card.addEventListener('click', () => buyProduct(p.id)); card.addEventListener('keydown', (e) => { if (e.key === 'Enter') buyProduct(p.id); }); grid.appendChild(card);});/* ============================ Toasts (sichtbare Seite-Benachrichtigungen) ============================ */const toastContainer = document.getElementById('toastContainer');function showToast(message, { type = 'default', timeout = 3500 } = {}) { const t = document.createElement('div'); t.className = 'toast ' + (type === 'success' ? 'success' : type === 'error' ? 'error' : ''); t.innerHTML = `<div>${message}</div>`; toastContainer.appendChild(t); setTimeout(() => { t.style.opacity = '0'; t.style.transform = 'translateY(6px)'; setTimeout(() => t.remove(), 300); }, timeout);}/* ============================ System-Benachrichtigungen (Notification API) ============================ */async function requestNotificationPermission() { if (!('Notification' in window)) { showToast('Browser unterstützt keine System-Benachrichtigungen.', { type: 'error' }); return false; } if (Notification.permission === 'granted') return true; const perm = await Notification.requestPermission(); return perm === 'granted';}async function sendSystemNotification(title, body) { if (!('Notification' in window)) return; if (Notification.permission !== 'granted') return; try { const n = new Notification(title, { body, icon: '' }); // Klick auf System-Benachrichtigung -> Fokus auf Tab n.onclick = () => { window.focus(); n.close(); }; } catch (err) { console.warn('Notification failed', err); }}/* ============================ Sofortkauf Funktion (Frontend) ============================ */async function buyProduct(productId) { // UI: direkte Rückmeldung an User (optimistic) const product = PRODUCTS.find(p => p.id === productId); showToast(`Sofortkauf: ${product.title} — Anfrage wird gesendet...`); try { let responseData; if (simulateServer) { // Simulation: kurze Wartezeit, dann Erfolg responseData = await simulateServerBuy(productId); } else { // Echte Implementierung: POST an dein Backend // Ersetze '/api/buy' mit deiner realen API-URL. Erwartet JSON { productId }. const res = await fetch('/api/buy', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ productId }) }); if (!res.ok) throw new Error('Server antwortete mit ' + res.status); responseData = await res.json(); // z. B. { success:true, orderId: '...' } } // Erfolg showToast(`✅ Superstar erfolgreich beantragt: ${product.title}`, { type: 'success' }); // Optional: Systembenachrichtigung await sendSystemNotification('Kauf erfolgreich', `${product.title} wurde gekauft.`); // Hier könntest du z.B. zur Bestellseite weiterleiten: // window.location.href = '/order-confirmation/' + responseData.orderId; } catch (err) { console.error(err); showToast('Fehler beim Kauf: ' + (err.message || 'Unbekannt'), { type: 'error', timeout: 5000 }); await sendSystemNotification('Kauf fehlgeschlagen', `${product.title}: ${err.message || 'Fehler'}`); }}/* ============================ Server-Simulation (falls kein Backend) ============================ */function simulateServerBuy(productId) { return new Promise((resolve, reject) => { // Simuliere 600–1400ms Wartezeit const t = 600 + Math.random() * 800; setTimeout(() => { // Simuliere 90% Erfolg, 10% Fehler if (Math.random() < 0.9) { resolve({ success: true, orderId: 'SIM-' + Date.now(), productId }); } else { reject(new Error('Zahlung abgelehnt (simuliert)')); } }, t); });}/* ============================ UI: Buttons / Controls ============================ */const enableNotifyBtn = document.getElementById('enableNotifyBtn');enableNotifyBtn.addEventListener('click', async () => { const ok = await requestNotificationPermission(); if (ok) { showToast('System-Benachrichtigungen aktiviert.', { type: 'success' }); } else { showToast('System-Benachrichtigungen nicht erlaubt.', { type: 'error' }); }});const simulateServerBtn = document.getElementById('simulateServerBtn');simulateServerBtn.addEventListener('click', () => { simulateServer = !simulateServer; simulateServerBtn.textContent = 'Server-Simulation: ' + (simulateServer ? 'AN' : 'AUS'); simulateServerBtn.classList.toggle('secondary', simulateServer); showToast('Simulation ' + (simulateServer ? 'AN' : 'AUS'));});/* ============================ Accessibility / Hinweis deines Backends ============================ Integrationstipps: - Backend: POST /api/buy { productId } -> Antwort { success:true, orderId } - Backend sollte idempotent sein (mehrere Klicks nicht mehrfach berechnen). - Für echte Push-Benachrichtigungen: Web Push (VAPID) + Service Worker. - Für Zahlungsabwicklung: leite Nutzer zu Zahlungs-Provider (Stripe, PayPal) oder rufe serverseitige Zahlung aus. - Wichtig: Sicherheit (Authentifizierung, CSRF, Rate-Limiting).*/</script></body></html>