Fixes und Währungsumstellung

This commit is contained in:
2026-06-04 23:32:29 +02:00
parent aff5b2cf12
commit b5b7866395
10 changed files with 214 additions and 181 deletions

View File

@@ -1,36 +1,21 @@
{{ define "main" }}
{{ $catData := index hugo.Data .Params.category }}
{{ $config := "" }}
{{ if eq .Params.engine "linear" }}
{{ $config = dict "fromFactor" .Params.from_factor
"toFactor" .Params.to_factor | jsonify }}
{{ else if eq .Params.engine "intermediate" }}
{{ $toExpr := index $catData.to_intermediate
.Params.from | default "v" }}
{{ $fromExpr := index $catData.from_intermediate
.Params.to | default "v" }}
{{ $config = printf
"{ toIntermediate: v => %s, fromIntermediate: v => %s }"
$toExpr $fromExpr }}
{{ else if eq .Params.engine "runtime" }}
{{ $fromCode := .Params.from }}
{{ $toCode := .Params.to }}
{{ $config = printf
"{ init: async function () { const pbUrl = document.querySelector('meta[name=\\'pocketbase-url\\']')?.content || 'https://www.alphabreed.com'; try { const response = await fetch(`${pbUrl}/api/collections/currencies/records`); if (!response.ok) { this.ratesError = 'Wechselkurse konnten nicht geladen werden.'; return; } const data = await response.json(); let latestUpdate = ''; for (const item of data.items || []) { this.rates[item.id] = item.rate; if (!latestUpdate || item.updated > latestUpdate) { latestUpdate = item.updated; } } const fromRate = '%s' === 'eur' ? 1 : this.rates['%s']; const toRate = '%s' === 'eur' ? 1 : this.rates['%s']; if (fromRate && toRate) { this.currentRate = parseFloat(toRate) / parseFloat(fromRate); } if (latestUpdate) { this.ratesUpdated = new Date(latestUpdate).toLocaleString('de-DE', { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit' }); } } catch (e) { this.ratesError = 'Wechselkurse konnten nicht geladen werden.'; } }, convert: function (value) { const fromRate = '%s' === 'eur' ? 1 : this.rates['%s']; const toRate = '%s' === 'eur' ? 1 : this.rates['%s']; if (!fromRate || !toRate) { return null; } return value.times(new Decimal(toRate)).dividedBy(new Decimal(fromRate)); } }"
$fromCode $fromCode $toCode $toCode
$fromCode $fromCode $toCode $toCode }}
{{ end }}
{{ $availableUnits := partial "available-units.html"
(dict "category" .Params.category "units" $catData.units) }}
<div x-data="createConverter('{{ .Params.engine }}',
{{ $config | safeJS }})"
x-init="init()">
{{ $engine := .Params.engine | default "linear" }}
{{ if eq $engine "currencies" }}
{{ partial "conversion/currencies-data" . }}
{{ else if eq $engine "temperatures" }}
{{ partial "conversion/temperatures-data" . }}
{{ else }}
{{ partial "conversion/linear-data" . }}
{{ end }}
<div id="conversionform"
class="bg-white p-8 rounded-lg shadow-md w-full
dark:bg-gray-700 dark:text-grey-200"
x-data="navActions()">
dark:bg-gray-700 dark:text-grey-200">
<h1 id="headline"
class="text-2xl font-bold mb-6 text-center">
@@ -40,18 +25,13 @@
<div>
<a x-ref="ajaxLink"
x-target.push="maincontent"
href="/"
class="hidden"></a>
<div>
<label for="type"
class="block text-sm font-medium opacity-75">
Einheitentyp:
</label>
<select id="type" name="type"
@change="navigate($event.target.value)"
@change="navigate($event.target.value, '1')"
class="select mt-1 block w-full">
{{ range $slug, $data := hugo.Data }}
{{ if and $data.slug $data.name }}
@@ -79,8 +59,8 @@
Umrechnen von:
</label>
<select id="from" name="from"
@change="navigateWithValue(
$event.target.value)"
@change="navigate(
$event.target.value, inputValue)"
class="select mt-1 block w-full">
{{ range $catData.unitOrder }}
{{ $unitData := index $availableUnits . }}
@@ -98,8 +78,8 @@
<div class="text-center sm:pt-6">
<button type="button"
@click="navigateWithResult(
$el.dataset.swapUrl)"
@click="navigate(
$el.dataset.swapUrl, prettyNumber(convert(prepareValue(inputValue))))"
data-swap-url="/{{ .Params.to }}-in-{{ .Params.from }}/"
class="inline-flex items-center text-sm
font-medium text-primary
@@ -110,8 +90,7 @@
stroke="currentColor"
class="w-6 h-6">
<path stroke-linecap="round"
stroke-linejoin="round"
d="M7.5 21 3 16.5m0 0L7.5 12M3
stroke-linejoin="round" d="M7.5 21 3 16.5m0 0L7.5 12M3
16.5h13.5m0-13.5L21 7.5m0
0L16.5 12M21 7.5H7.5" />
</svg>
@@ -125,8 +104,8 @@
Umrechnen in:
</label>
<select id="to" name="to"
@change="navigateWithValue(
$event.target.value)"
@change="navigate(
$event.target.value, inputValue)"
class="select mt-1 block w-full">
{{ range $catData.unitOrder }}
{{ $unitData := index $availableUnits . }}
@@ -155,7 +134,7 @@
<input type="text" id="value"
inputmode="decimal"
x-model="inputValue"
@input="calculate()"
@input.debounce.500ms="updateUrl(inputValue)"
class="textinput mt-1 block w-full"
placeholder="Wert eingeben">
</div>
@@ -166,13 +145,8 @@
</label>
<input type="text" id="result"
disabled
:value="result"
:value="prettyNumber(convert(prepareValue(inputValue)))"
class="textinput mt-1 block w-full">
<p x-show="ratesError"
x-cloak
class="text-sm text-red-600 mt-1
dark:text-red-400"
x-text="ratesError"></p>
</div>
</div>
@@ -188,5 +162,3 @@
</div>
{{ end }}