From 2aa7156e12200798c45a53878dbcc37c77625752 Mon Sep 17 00:00:00 2001 From: secondl1ght <85003930+secondl1ght@users.noreply.github.com> Date: Mon, 22 Aug 2022 18:00:00 -0400 Subject: finish hooking up currency selection --- src/lib/socket.js | 3 +- src/lib/store.js | 2 - src/lib/utils.js | 4 +- src/routes/+layout.svelte | 6 +-- src/routes/[username]/+page.server.js | 2 +- src/routes/[username]/dashboard/+page.svelte | 22 ++++----- src/routes/[username]/receive/+page.svelte | 61 ++++++++++++++++++------- src/routes/[username]/transactions/+page.svelte | 2 +- src/routes/invoice/+server.js | 3 +- src/routes/invoice/[id]/+page.svelte | 9 ++-- 10 files changed, 66 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/lib/socket.js b/src/lib/socket.js index a30d4376..e85e8f15 100644 --- a/src/lib/socket.js +++ b/src/lib/socket.js @@ -1,5 +1,5 @@ import { get } from 'svelte/store'; -import { rate, user, token, invoices, preferredCurrency, newPayment } from '$lib/store'; +import { rate, user, token, invoices, newPayment } from '$lib/store'; import { success } from '$lib/utils'; let interval, socket; @@ -32,7 +32,6 @@ export const messages = (data) => ({ login() { user.set(data); - preferredCurrency.set('USD'); } }); diff --git a/src/lib/store.js b/src/lib/store.js index 8469dab7..18a8824f 100644 --- a/src/lib/store.js +++ b/src/lib/store.js @@ -30,12 +30,10 @@ const persistLocal = (key, defaultValue) => { export const rate = writable(); export const rates = writable(); export const selectedRate = writable(); -export const conversion = writable(); export const user = writable(); export const invoiceAmount = writable(); export const invoiceAmountFiat = writable(); export const token = persistSession('token'); export const invoices = writable({}); -export const preferredCurrency = writable(); export const newPayment = persistLocal('newPayment'); export const colorTheme = writable('from-primary to-gradient'); diff --git a/src/lib/utils.js b/src/lib/utils.js index 3f2f566e..a1bdf8d7 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -22,7 +22,7 @@ export const get = (url, headers = { accept: 'application/json' }) => export const post = (url, body, headers) => { headers = { ...headers, accept: 'application/json', 'content-type': 'application/json' }; return fetch(base + url, { - method: "POST", + method: 'POST', body: JSON.stringify(body), headers }) @@ -44,7 +44,7 @@ export const post = (url, body, headers) => { export const put = (url, body, headers) => { headers = { ...headers, 'content-type': 'application/json' }; return fetch(base + url, { - method: "PUT", + method: 'PUT', body: JSON.stringify(body), headers }); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 4cf819fc..9ae32138 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -5,7 +5,7 @@ import '../app.css'; import { onMount } from 'svelte'; import { connect } from '$lib/socket'; - import { user, token, rate, rates, selectedRate, conversion } from '$lib/store'; + import { user, token, rate, rates, selectedRate } from '$lib/store'; import { page } from '$app/stores'; import { browser } from '$app/env'; import { goto } from '$app/navigation'; @@ -18,8 +18,7 @@ export let data; $rates = data.rates; - $: $conversion = data.rates[data.user.currency] / $rate; - $: $selectedRate = $rate * $conversion; + $: $selectedRate = $rate * (data.rates[data.user.currency] / data.rates.USD); let ready = false; @@ -32,7 +31,6 @@ }); if (protectedRoutes.find((p) => $page.url.pathname.match(p))) { - console.log('TOKEN', $token); if (!$token) { goto('/login'); warning($_('error.signIn')); diff --git a/src/routes/[username]/+page.server.js b/src/routes/[username]/+page.server.js index 2520b0b1..9ae4af2d 100644 --- a/src/routes/[username]/+page.server.js +++ b/src/routes/[username]/+page.server.js @@ -18,5 +18,5 @@ export async function PUT({ request }) { let { token } = cookie.parse(request.headers.get('cookie') || ''); let { user } = await request.json(); await post(`/user`, user, { authorization: `Bearer ${token}` }); - console.log("put done") + console.log('put done'); } diff --git a/src/routes/[username]/dashboard/+page.svelte b/src/routes/[username]/dashboard/+page.svelte index 9e09aa16..56aa0d6f 100644 --- a/src/routes/[username]/dashboard/+page.svelte +++ b/src/routes/[username]/dashboard/+page.svelte @@ -2,7 +2,7 @@ import { post, failure } from '$lib/utils'; import { tick } from 'svelte'; import { AppHeader, Icon } from '$comp'; - import { rate, user, preferredCurrency } from '$lib/store'; + import { selectedRate, user } from '$lib/store'; import { _ } from 'svelte-i18n'; let payreq = '', @@ -26,24 +26,19 @@ $: btcPrice = new Intl.NumberFormat('en-US', { style: 'currency', - currency: 'USD' - }).format($rate); + currency: $user.currency + }).format($selectedRate); $: accountBalanceFiat = new Intl.NumberFormat('en-US', { style: 'currency', - currency: 'USD' - }).format($user.account.balance * ($rate / 100000000)); + currency: $user.currency + }).format($user.account.balance * ($selectedRate / 100000000)); $: accountBalanceBtc = $user.account.balance / 100000000; $: accountBalanceSats = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format( $user.account.balance ); - - $: feepayreq = new Intl.NumberFormat('en-US', { - style: 'currency', - currency: 'USD' - }).format(($user.account.balance * ($rate / 100000000)) / 100); {#if $user} @@ -64,8 +59,9 @@ {#if !withdrawing} {$_('user.dashboard.bitcoinPrice')} - {$rate ? btcPrice : $_('user.dashboard.fetchingRate')} - {$preferredCurrency} + {$selectedRate ? btcPrice : $_('user.dashboard.fetchingRate')} {@html $_('user.dashboard.dataFromBinance')} @@ -75,7 +71,7 @@ {$rate ? accountBalanceFiat : $_('user.dashboard.fetchingRate')}{$selectedRate ? accountBalanceFiat : $_('user.dashboard.fetchingRate')} {accountBalanceSats} SAT diff --git a/src/routes/[username]/receive/+page.svelte b/src/routes/[username]/receive/+page.svelte index 6d750d1b..2706ec86 100644 --- a/src/routes/[username]/receive/+page.svelte +++ b/src/routes/[username]/receive/+page.svelte @@ -2,7 +2,7 @@ // in the future we will want to allow users to set their fiat currency in the settings import { AppHeader, Icon } from '$comp'; import { goto } from '$app/navigation'; - import { invoiceAmount, invoiceAmountFiat, rate, user } from '$lib/store'; + import { invoiceAmount, invoiceAmountFiat, selectedRate, user } from '$lib/store'; import { post, warning } from '$lib/utils'; import { page } from '$app/stores'; import { _, number } from 'svelte-i18n'; @@ -11,11 +11,13 @@ let amountFiat = 0; let amountSats = 0; - $: amount = useFiat ? Math.round(amountFiat / ($rate / 100000000)) : amountSats; + $: amount = useFiat ? Math.round(amountFiat / ($selectedRate / 100000000)) : amountSats; - $: $invoiceAmount = parseInt(amountSats === 0 ? amountFiat / ($rate / 100000000) : amountSats); + $: $invoiceAmount = parseInt( + amountSats === 0 ? amountFiat / ($selectedRate / 100000000) : amountSats + ); $: $invoiceAmountFiat = parseInt( - amountFiat === 0 ? amountSats * ($rate / 100000000) : amountFiat + amountFiat === 0 ? amountSats * ($selectedRate / 100000000) : amountFiat ); $: amountSatsFormatted = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format( amountSats @@ -23,11 +25,11 @@ $: amountFiatConverted = new Intl.NumberFormat('en-US', { style: 'currency', - currency: 'USD' - }).format(amountSats * ($rate / 100000000)); + currency: $user.currency + }).format(amountSats * ($selectedRate / 100000000)); $: amountSatsConverted = new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format( - amountFiat / ($rate / 100000000) + amountFiat / ($selectedRate / 100000000) ); const numPad = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '0', '<']; @@ -50,7 +52,7 @@ amountSats = 0; } } - } else if (value !== '.' && value !== '<' && parseInt(amountFiat + value) > $rate) { + } else if (value !== '.' && value !== '<' && parseInt(amountFiat + value) > $selectedRate) { warning($_('user.receive.lessThan1BTCWarning')); } else if (amountFiat !== 0 && amountFiat.match(/\.../)) { return; @@ -60,7 +62,7 @@ } else { if (value === '.') { return; - } else if (amountSats === 0 && value !== '<' && value !== '0') { + } else if (amountSats === '0' && value !== '<' && value !== '0') { amountSats = value; } else if (value === '<') { if (amountSats !== 0) { @@ -79,7 +81,11 @@ }; let submit = async () => { - let { id } = await post('/invoice', { amount, rate: $rate, username: $page.params.username }); + let { id } = await post('/invoice', { + amount, + rate: $selectedRate, + username: $page.params.username + }); goto(`/invoice/${id}`); }; @@ -88,26 +94,47 @@
- {#if $rate} + {#if $selectedRate}
- {useFiat ? `$${amountFiat}` : amountSatsFormatted}{useFiat ? 'USD' : 'SAT'}{useFiat ? $user.currency : 'SAT'}
{useFiat ? `${amountSatsConverted} SAT` : `${amountFiatConverted} USD`}{useFiat ? `${amountSatsConverted} SAT` : amountFiatConverted}