From c98e4b8f8b4b646ec3f92618f9f74fd35cabbde1 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 18 Nov 2015 17:15:19 -0800 Subject: Currency toggle --- public/js/wallet.coffee | 30 ++++++++++++++++++++++++++---- views/users/wallet.html | 5 +++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/public/js/wallet.coffee b/public/js/wallet.coffee index c2cc5041..7399973d 100644 --- a/public/js/wallet.coffee +++ b/public/js/wallet.coffee @@ -30,8 +30,24 @@ $(-> $('.has-error').effect('shake', 500) return false ) + + $('#currency_toggle').click(-> + if $(this).html() is 'BTC' + g.amount = $('#amount').val() + $('#amount').val((g.amount * g.exchange).toFixed(2)) + $(this).html(g.user.currency) + else + amount = $('#amount').val() / g.exchange + if Math.abs(g.amount - amount) > 0.00001 + $('#amount').val(parseFloat(amount).toFixed(8)) + else + $('#amount').val(parseFloat(g.amount).toFixed(8)) + + $(this).html('BTC') + ) ) + getToken = -> $.get("/token", (token) -> g.token = token @@ -45,9 +61,14 @@ getUser = -> $('#address').val(user.address) $('form').fadeIn() - createWallet() + getExchangeRate() ) +getExchangeRate = -> + $.get("/ticker?currency=#{g.user.currency}&symbol=#{g.user.symbol}&type=bid", (exchange) -> + g.exchange = exchange + createWallet() + ) createWallet = -> if localStorage.getItem(g.user.username) is null @@ -102,9 +123,10 @@ getAddresses = -> calculateBalance = -> balances = JSON.parse(localStorage.getItem(g.user.username)).balances balance = balances.reduce (t,s) -> t + s - val = (balance / 100000000).toFixed(8) - $('#balance').html(val) - $('#amount').val(val) + g.balance = (balance / 100000000).toFixed(8) + $('#balance').html(g.balance) + $('#fiat').html("#{(g.balance * g.exchange).toFixed(2)} #{g.user.currency}") + $('#amount').val(g.balance) check_address = (address) -> try diff --git a/views/users/wallet.html b/views/users/wallet.html index 592aec3b..0ea2d187 100644 --- a/views/users/wallet.html +++ b/views/users/wallet.html @@ -10,7 +10,8 @@
- 0 BTC + 0 BTC / + 0
@@ -26,7 +27,7 @@
- + -- cgit v1.2.3