From 90662d8d0b70264a04ab2c6a0a9aa7b8c60477e1 Mon Sep 17 00:00:00 2001 From: rxf Date: Sun, 24 Aug 2025 14:55:42 +0200 Subject: [PATCH] Liste sortieren, bearbeiten --- public/global.js | 28 ++++++++++++++++++- routes/address.js | 68 ++++++++++++++++++++++++++++------------------- views/index.pug | 13 +++------ 3 files changed, 71 insertions(+), 38 deletions(-) diff --git a/public/global.js b/public/global.js index e9e518b..501853c 100644 --- a/public/global.js +++ b/public/global.js @@ -15,6 +15,15 @@ function updateSortArrows() { el.style.opacity = currentSort.key === key ? '1' : '0.3'; }); } + + // Tab-Wechsel Funktion aus index.pug +function showTab(tab) { + document.getElementById('tabInputContent').style.display = tab === 'input' ? '' : 'none'; + document.getElementById('tabListContent').style.display = tab === 'list' ? '' : 'none'; + document.getElementById('tabInput').classList.toggle('active', tab === 'input'); + document.getElementById('tabList').classList.toggle('active', tab === 'list'); +} + document.addEventListener('DOMContentLoaded', () => { const saveBtn = document.getElementById('saveBtn'); const refreshBtn = document.getElementById('refreshBtn'); @@ -27,6 +36,8 @@ document.addEventListener('DOMContentLoaded', () => { const limitInput = document.getElementById('limit'); const resultDiv = document.getElementById('result'); const tableBody = document.querySelector('#entriesTable tbody'); + const tabInput = document.getElementById('tabInput'); + const tabList = document.getElementById('tabList'); let editId = null; @@ -153,6 +164,7 @@ document.addEventListener('DOMContentLoaded', () => { } } + function clearForm() { espIdInput.value = ''; sensorNumberInput.value = ''; @@ -267,9 +279,20 @@ document.addEventListener('DOMContentLoaded', () => { sensorNumberInput.value = item._id; nameInput.value = item.chip.name || ''; descriptionInput.value = item.chip.description || ''; -// addressInput.value = item.address || ''; + addressInput.value = ''; editId = id; + try { + const rt = await fetch(`api/holAdresse/${item._id}`) + const data = await rt.json(); + console.dir(data) + if (!data.error && data.address) { + addressInput.value = data.address; + } + } catch (e) { + console.log("Fehler beim Adresse holen", e) + } saveBtn.textContent = 'Aktualisieren'; + showTab('input') } }); }); @@ -293,8 +316,11 @@ document.addEventListener('DOMContentLoaded', () => { } } + saveBtn.addEventListener('click', saveEntry); refreshBtn.addEventListener('click', loadEntries); +tabInput.addEventListener('click', () => showTab('input')) +tabList.addEventListener('click', () => showTab('list')) loadEntries(); }); \ No newline at end of file diff --git a/routes/address.js b/routes/address.js index 172b9bb..4b7b985 100644 --- a/routes/address.js +++ b/routes/address.js @@ -5,6 +5,43 @@ import { getCollections, update_pflux, clientClose } from '../db/mongo.js'; export function registerAddressRoute(app, requireLogin) { const APIHOST = process.env.APIHOST || 'https://noise.fuerst-stuttgart.de/srv/'; + + const holAdresse = async (id) => { + // Adresse wie bisher holen (über die Sensornummer via nominative) + let addressString = ''; + let addrParts = {}; + try { + const url = APIHOST + 'getaddress/' + `?sensorid=${id}`; + console.log(url) + const r = await fetch(url, { headers: { 'Accept': 'application/json' } }); + if (r.ok) { + const data = await r.json(); + const addrObj = data?.erg?.address || data?.address || {}; + const street = addrObj.street ?? ''; + const plz = addrObj.plz ?? ''; + const city = addrObj.city ?? ''; + const rightPart = [plz, city].filter(Boolean).join(' ').trim(); + addressString = [street, rightPart].filter(Boolean).join(', '); + addrParts = { street, plz, city }; + } + } catch (err) { + console.error('Address lookup failed:', err); + } + return { + address: addressString, + parts: addrParts, + } + } + + app.get('/api/holAdresse/:sensorNumber', requireLogin, async (req, res) => { + const sensorNumber = parseInt(req.params.sensorNumber, 10); + if (isNaN(sensorNumber)) { + return res.status(400).json({ error: 'Ungültige Sensornummer' }); + } + const addr = await holAdresse(sensorNumber) + res.json(addr) + }) + app.get('/api/address/:sensorNumber', requireLogin, async (req, res) => { const sensorNumber = parseInt(req.params.sensorNumber, 10); if (isNaN(sensorNumber)) { @@ -37,32 +74,9 @@ export function registerAddressRoute(app, requireLogin) { await clientClose() } } + const adr = await holAdresse(encodeURIComponent(propEntry._id)) + adr.props = propsF - // Adresse wie bisher holen (über die Sensornummer via nominative) - let addressString = ''; - let addrParts = {}; - try { - const url = APIHOST + 'getaddress/' + `?sensorid=${encodeURIComponent(propEntry._id)}`; - console.log(url) - const r = await fetch(url, { headers: { 'Accept': 'application/json' } }); - if (r.ok) { - const data = await r.json(); - const addrObj = data?.erg?.address || data?.address || {}; - const street = addrObj.street ?? ''; - const plz = addrObj.plz ?? ''; - const city = addrObj.city ?? ''; - const rightPart = [plz, city].filter(Boolean).join(' ').trim(); - addressString = [street, rightPart].filter(Boolean).join(', '); - addrParts = { street, plz, city }; - } - } catch (err) { - console.error('Address lookup failed:', err); - } - - return res.json({ - address: addressString, - parts: addrParts, - props: propsF - }); - }); + return res.json(adr) + }) } diff --git a/views/index.pug b/views/index.pug index e5570f6..3a78db2 100644 --- a/views/index.pug +++ b/views/index.pug @@ -9,8 +9,8 @@ html(lang="de") h1 ESP-ID → Sensornummer // Tab Navigation div.tabs - button.tab-btn#tabInput.active(type="button" onclick="showTab('input')") Eingabe - button.tab-btn#tabList(type="button" onclick="showTab('list')") Liste + button.tab-btn#tabInput.active(type="button") Eingabe + button.tab-btn#tabList(type="button") Liste // Eingabe-Tab div#tabInputContent.tab-content @@ -54,11 +54,4 @@ html(lang="de") th(id="thDate" data-sort="date" style="cursor:pointer") Datum th Aktionen tbody - script(type="module" src="/global.js") - script. - function showTab(tab) { - document.getElementById('tabInputContent').style.display = tab === 'input' ? '' : 'none'; - document.getElementById('tabListContent').style.display = tab === 'list' ? '' : 'none'; - document.getElementById('tabInput').classList.toggle('active', tab === 'input'); - document.getElementById('tabList').classList.toggle('active', tab === 'list'); - } \ No newline at end of file + script(type="module" src="/global.js") \ No newline at end of file