41 lines
1.7 KiB
HTML
41 lines
1.7 KiB
HTML
<div x-data="{
|
|
inputValue: '1',
|
|
rate: 0,
|
|
formula: '',
|
|
ratesError: '',
|
|
ratesUpdated: '',
|
|
convert(v) { return v.times(this.rate); },
|
|
async init() {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const valueParam = params.get('v');
|
|
if (valueParam) this.inputValue = valueParam.replace(/,/g, '.');
|
|
|
|
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();
|
|
const rates = {};
|
|
let updated = '';
|
|
for (const item of data.items || []) {
|
|
rates[item.id] = item.rate;
|
|
updated = item.updated;
|
|
}
|
|
const fromRate = '{{ .Params.from }}' === 'eur' ? 1 : rates['{{ .Params.from }}'];
|
|
const toRate = '{{ .Params.to }}' === 'eur' ? 1 : rates['{{ .Params.to }}'];
|
|
if (fromRate && toRate) {
|
|
this.rate = parseFloat(toRate) / parseFloat(fromRate);
|
|
this.ratesUpdated = new Date(updated).toLocaleString('de-DE', { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit' });
|
|
this.formula = '1 {{ .Params.from | upper }} = ' + prettyNumber(this.rate) + ' {{ .Params.to | upper }}';
|
|
} else {
|
|
this.ratesError = 'Wechselkurse nicht gefunden.';
|
|
}
|
|
} catch (e) {
|
|
this.ratesError = 'Wechselkurse konnten nicht geladen werden.';
|
|
}
|
|
}
|
|
}">
|