Fix currency converter in WebUI

This commit is contained in:
Djuri Baars 2024-09-18 01:49:18 +02:00
parent 761c7f2991
commit 7d82b1e1a9
4 changed files with 86 additions and 38 deletions

View File

@ -54,6 +54,7 @@
"@testing-library/jest-dom": "^6.5.0",
"bootstrap": "^5.3.2",
"bootstrap-icons": "^1.11.3",
"msgpack-es": "^0.0.5",
"nostr-tools": "^2.7.1",
"patch-package": "^8.0.0",
"svelte-i18n": "^4.0.0",

View File

@ -28,7 +28,7 @@ $input-font-size-sm: $font-size-base * 0.875;
@import '../node_modules/bootstrap/scss/forms';
@import '../node_modules/bootstrap/scss/buttons';
@import '../node_modules/bootstrap/scss/button-group';
@import '../node_modules/bootstrap/scss/pagination';
//@import '../node_modules/bootstrap/scss/pagination';
@import '../node_modules/bootstrap/scss/dropdown';

View File

@ -1,18 +1,18 @@
<script lang="ts">
import { Col, Container, Input, InputGroup, InputGroupText, Row } from '@sveltestrap/sveltestrap';
import { onDestroy, onMount } from 'svelte';
import { encode, decode } from 'msgpack-es';
let exchangeRates = {
USD: 57798,
GBP: 44236,
CHF: 49362,
AUD: 86552,
JPY: 8221088,
EUR: 52347,
CAD: 78508
};
let updateInterval: number;
let socket: WebSocket;
let currencies = { ...exchangeRates };
let btcValue = 1;
@ -65,25 +65,44 @@
}
}
async function fetchExchangeRates() {
try {
const response = await fetch('http://localhost:8080/api/lastprice');
const data = await response.json();
exchangeRates = data;
currencies = { ...data };
updateValues(lastEditedField, inputValues[lastEditedField]);
} catch (error) {
console.error('Error fetching exchange rates:', error);
}
}
// async function fetchExchangeRates() {
// try {
// const response = await fetch('https://ws.btclock.dev/api/lastprice');
// const data = await response.json();
// exchangeRates = data;
// currencies = { ...data };
// updateValues(lastEditedField, inputValues[lastEditedField]);
// } catch (error) {
// console.error('Error fetching exchange rates:', error);
// }
// }
onMount(() => {
fetchExchangeRates();
updateInterval = setInterval(fetchExchangeRates, 10000);
socket = new WebSocket('ws://ws.btclock.dev/api/v2/ws');
socket.binaryType = 'arraybuffer';
socket.addEventListener('open', () => {
socket.send(
encode({
type: 'subscribe',
eventType: 'price',
currencies: ['USD', 'EUR', 'GBP', 'CAD', 'AUD', 'JPY']
})
);
});
socket.addEventListener('message', (event) => {
let data = decode(event.data);
if ('price' in data) {
let currencyKey = Object.keys(data.price);
exchangeRates[currencyKey] = data.price[currencyKey];
updateValues(lastEditedField, inputValues[lastEditedField]);
}
});
});
onDestroy(() => {
clearInterval(updateInterval);
socket.close();
});
</script>

View File

@ -654,21 +654,21 @@
"@scure/base" "~1.1.0"
"@sveltejs/adapter-auto@^3.0.0":
version "3.2.4"
resolved "https://registry.yarnpkg.com/@sveltejs/adapter-auto/-/adapter-auto-3.2.4.tgz#fe0dc1aa986cfa8a37deca4050ccec3f53db1c2a"
integrity sha512-a64AKYbfTUrVwU0xslzv1Jf3M8bj0IwhptaXmhgIkjXspBXhD0od9JiItQHchijpLMGdEDcYBlvqySkEawv6mQ==
version "3.2.5"
resolved "https://registry.yarnpkg.com/@sveltejs/adapter-auto/-/adapter-auto-3.2.5.tgz#b0247c96ecb66769aac3fe2ffe787a68c8805f62"
integrity sha512-27LR+uKccZ62lgq4N/hvyU2G+hTP9fxWEAfnZcl70HnyfAjMSsGk1z/SjAPXNCD1mVJIE7IFu3TQ8cQ/UH3c0A==
dependencies:
import-meta-resolve "^4.1.0"
"@sveltejs/adapter-static@^3.0.0":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@sveltejs/adapter-static/-/adapter-static-3.0.4.tgz#5ab50410156c1c71dc78404e498fc38207aa59e4"
integrity sha512-Qm4GAHCnRXwfWG9/AtnQ7mqjyjTs7i0Opyb8H2KH9rMR7fLxqiPx/tXeoE6HHo66+72CjyOb4nFH3lrejY4vzA==
version "3.0.5"
resolved "https://registry.yarnpkg.com/@sveltejs/adapter-static/-/adapter-static-3.0.5.tgz#12ceaf396e27f5871ae59da44c15caf9f4a91005"
integrity sha512-kFJR7RxeB6FBvrKZWAEzIALatgy11ISaaZbcPup8JdWUdrmmfUHHTJ738YHJTEfnCiiXi6aX8Q6ePY7tnSMD6Q==
"@sveltejs/kit@^2.0.0":
version "2.5.27"
resolved "https://registry.yarnpkg.com/@sveltejs/kit/-/kit-2.5.27.tgz#d1ab6c11bf9eac21104321d745ca1660ddd23c5a"
integrity sha512-CcbRTzl+65oWljAASL6UlxM4x3NWwd0fjq5fQOfP243vs50myFQ8lil0fr3Im6HeeQqYUCtnv8HjO8REWVPjTw==
version "2.5.28"
resolved "https://registry.yarnpkg.com/@sveltejs/kit/-/kit-2.5.28.tgz#13de1ccab39cf84fb82d1e8e7c89eb233e9622e2"
integrity sha512-/O7pvFGBsQPcFa9UrW8eUC5uHTOXLsUp3SN0dY6YmRAL9nfPSrJsSJk//j5vMpinSshzUjteAFcfQTU+04Ka1w==
dependencies:
"@types/cookie" "^0.6.0"
cookie "^0.6.0"
@ -1610,7 +1610,7 @@ check-error@^2.1.1:
resolved "https://registry.yarnpkg.com/check-error/-/check-error-2.1.1.tgz#87eb876ae71ee388fa0471fe423f494be1d96ccc"
integrity sha512-OAlb+T7V4Op9OwdkjmguYRqncdlx5JiofwOAUkmTF+jNdHwzTaTs4sRAGpzLF3oOz5xAyDGrPgeIDFQmDOTiJw==
"chokidar@>=3.0.0 <4.0.0", chokidar@^3.4.1:
chokidar@^3.4.1:
version "3.6.0"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.6.0.tgz#197c6cc669ef2a8dc5e7b4d97ee4e092c3eb0d5b"
integrity sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==
@ -1625,6 +1625,13 @@ check-error@^2.1.1:
optionalDependencies:
fsevents "~2.3.2"
chokidar@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-4.0.0.tgz#4d603963e5dd762dc5c7bb1cb5664e53a3002225"
integrity sha512-mxIojEAQcuEvT/lyXq+jf/3cO/KoA6z4CeNDGGevTybECPOMFCnQy3OPahluUkbqgPNGw5Bi78UC7Po6Lhy+NA==
dependencies:
readdirp "^4.0.1"
chownr@^1.1.1:
version "1.1.4"
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.1.4.tgz#6fc9d7b42d32a583596337666e7d08084da2cc6b"
@ -2978,6 +2985,11 @@ ms@^2.1.3:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
msgpack-es@^0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/msgpack-es/-/msgpack-es-0.0.5.tgz#2cc46435e26a4c13520731c515d0a1d9a191dfc8"
integrity sha512-iK8YNWqs4csqE7udSmuY/1bz0MH8L80eX2ZGIlf8g7G2aq9wRUbVpTbyixqQNNJZ2kJ8v2NgVzj0npRqdJBMuA==
nan@^2.14.0, nan@^2.17.0, nan@^2.18.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.19.0.tgz#bb58122ad55a6c5bc973303908d5b16cfdd5a8c0"
@ -3516,6 +3528,11 @@ readable-stream@^3.1.1, readable-stream@^3.4.0:
string_decoder "^1.1.1"
util-deprecate "^1.0.1"
readdirp@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-4.0.1.tgz#b2fe35f8dca63183cd3b86883ecc8f720ea96ae6"
integrity sha512-GkMg9uOTpIWWKbSsgwb5fA4EavTR+SG/PMPoAY8hkhHfEEY0/vqljY+XHqtDf2cr2IJtoNRDbrrEpZUiZCkYRw==
readdirp@~3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.6.0.tgz#74a370bd857116e245b29cc97340cd431a02a6c7"
@ -3670,11 +3687,11 @@ sander@^0.5.0:
rimraf "^2.5.2"
sass@^1.69.5:
version "1.78.0"
resolved "https://registry.yarnpkg.com/sass/-/sass-1.78.0.tgz#cef369b2f9dc21ea1d2cf22c979f52365da60841"
integrity sha512-AaIqGSrjo5lA2Yg7RvFZrlXDBCp3nV4XP73GrLGvdRWWwk+8H3l0SDvq/5bA4eF+0RFPLuWUk3E+P1U/YqnpsQ==
version "1.79.0"
resolved "https://registry.yarnpkg.com/sass/-/sass-1.79.0.tgz#4a58442b6c094ac57a0805519bff6133d70590c9"
integrity sha512-9Q1xXsm9XT54yYkmQAoH3vCMEIavwWWQGJ3cZ0WJAgecR4edDDTdtiPyEeFDNWO/hLCy3qZKvwjK4ulPR5Yzow==
dependencies:
chokidar ">=3.0.0 <4.0.0"
chokidar "^4.0.0"
immutable "^4.0.0"
source-map-js ">=0.6.2 <2.0.0"
@ -3804,16 +3821,16 @@ sorcery@^0.11.0:
minimist "^1.2.0"
sander "^0.5.0"
"source-map-js@>=0.6.2 <2.0.0", source-map-js@^1.0.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.0.tgz#16b809c162517b5b8c3e7dcd315a2a5c2612b2af"
integrity sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==
source-map-js@^1.2.1:
"source-map-js@>=0.6.2 <2.0.0", source-map-js@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.1.tgz#1ce5650fddd87abc099eda37dcff024c2667ae46"
integrity sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==
source-map-js@^1.0.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.0.tgz#16b809c162517b5b8c3e7dcd315a2a5c2612b2af"
integrity sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==
space-separated-tokens@^1.0.0:
version "1.1.5"
resolved "https://registry.yarnpkg.com/space-separated-tokens/-/space-separated-tokens-1.1.5.tgz#85f32c3d10d9682007e917414ddc5c26d1aa6899"
@ -4298,7 +4315,7 @@ vite-node@2.1.1:
pathe "^1.1.2"
vite "^5.0.0"
vite@^5.0.0, vite@^5.4.2:
vite@^5.0.0:
version "5.4.5"
resolved "https://registry.yarnpkg.com/vite/-/vite-5.4.5.tgz#e4ab27709de46ff29bd8db52b0c51606acba893b"
integrity sha512-pXqR0qtb2bTwLkev4SE3r4abCNioP3GkjvIDLlzziPpXtHgiJIjuKl+1GN6ESOT3wMjG3JTeARopj2SwYaHTOA==
@ -4309,6 +4326,17 @@ vite@^5.0.0, vite@^5.4.2:
optionalDependencies:
fsevents "~2.3.3"
vite@^5.4.2:
version "5.4.6"
resolved "https://registry.yarnpkg.com/vite/-/vite-5.4.6.tgz#85a93a1228a7fb5a723ca1743e337a2588ed008f"
integrity sha512-IeL5f8OO5nylsgzd9tq4qD2QqI0k2CQLGrWD0rCN0EQJZpBK5vJAx0I+GDkMOXxQX/OfFHMuLIx6ddAxGX/k+Q==
dependencies:
esbuild "^0.21.3"
postcss "^8.4.43"
rollup "^4.20.0"
optionalDependencies:
fsevents "~2.3.3"
vitefu@^0.2.5:
version "0.2.5"
resolved "https://registry.yarnpkg.com/vitefu/-/vitefu-0.2.5.tgz#c1b93c377fbdd3e5ddd69840ea3aa70b40d90969"