Frontend-Logik an neue API-Antwortstruktur angepasst

This commit is contained in:
2025-03-17 21:29:37 +01:00
parent 2f4671cbc4
commit 84ba72ab72

View File

@@ -312,59 +312,52 @@
if (query) params.append('q', query); if (query) params.append('q', query);
fetch(`/search?${params.toString()}`) fetch(`/search?${params.toString()}`)
.then(response => { .then(response => response.json())
if (!response.ok) {
return response.json().then(data => {
throw new Error(data.error || 'Ein Fehler ist aufgetreten');
});
}
return response.json();
})
.then(data => { .then(data => {
const resultsDiv = document.getElementById('results'); if (data.error) {
resultsDiv.innerHTML = ''; showError(data.error);
if (data.length === 0) {
resultsDiv.innerHTML = '<div class="alert alert-info">Keine Ergebnisse gefunden.</div>';
lastResults = [];
updateResultCounts();
return; return;
} }
data.forEach(customer => { const resultsContainer = document.getElementById('results');
const card = document.createElement('div'); resultsContainer.innerHTML = '';
card.className = 'card result-card';
card.innerHTML = ` if (data.results && data.results.length > 0) {
<div class="card-body"> data.results.forEach(customer => {
<h5 class="card-title">${customer.Vorname} ${customer.Nachname}</h5> const card = document.createElement('div');
<p class="card-text"> card.className = 'card mb-3';
<strong>Kundennummer:</strong> ${customer.Nummer}<br> card.innerHTML = `
<strong>Fachrichtung:</strong> ${customer.Fachrichtung || 'N/A'}<br> <div class="card-body">
<strong>Adresse:</strong> ${createAddressLink(customer.Strasse, customer.PLZ, customer.Ort)} <h5 class="card-title">${customer.Vorname} ${customer.Nachname}</h5>
${customer.weather ? ` <p class="card-text">
<span class="weather-info"> <strong>Kundennummer:</strong> ${customer.Nummer}<br>
<img src="http://openweathermap.org/img/wn/${customer.weather.icon}@2x.png" <strong>Fachrichtung:</strong> ${customer.Fachrichtung || 'N/A'}<br>
alt="${customer.weather.description}" <strong>Adresse:</strong> ${createAddressLink(customer.Strasse, customer.PLZ, customer.Ort)}
title="${customer.weather.description}"> ${customer.weather ? `
${customer.weather.temperature}°C <span class="weather-info">
</span> <img src="http://openweathermap.org/img/wn/${customer.weather.icon}@2x.png"
` : ''} alt="${customer.weather.description}"
<br> title="${customer.weather.description}">
<strong>Telefon:</strong> ${createPhoneLink(customer.Tel)}<br> ${customer.weather.temperature}°C
<strong>E-Mail:</strong> ${createEmailLink(customer.mail)} </span>
</p> ` : ''}
<div class="card-actions"> <br>
<button class="btn btn-outline-primary share-button" onclick="copyCustomerLink('${customer.Nummer}')"> <strong>Telefon:</strong> ${createPhoneLink(customer.Tel)}<br>
🔗 Teilen <strong>E-Mail:</strong> ${createEmailLink(customer.mail)}
</button> </p>
</div> </div>
</div> `;
`; resultsContainer.appendChild(card);
resultsDiv.appendChild(card); });
});
// Zeige die Anzahl der Treffer an
lastResults = data; const totalResults = document.getElementById('total-results');
updateResultCounts(); if (totalResults) {
totalResults.textContent = `${data.total} Treffer gefunden`;
}
} else {
resultsContainer.innerHTML = '<div class="alert alert-info">Keine Ergebnisse gefunden.</div>';
}
}) })
.catch(error => { .catch(error => {
console.error('Fehler:', error); console.error('Fehler:', error);