1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
<script>
import { Left, Icon } from '$comp';
import { selectedRate } from '$lib/store';
import { f, s, post, warning, sat, sats } from '$lib/utils';
import { t } from '$lib/translations';
export let amount,
currency,
fiat = !amount;
export let amountFiat = amount ? amount * ($selectedRate / sats) : 0;
let amountSats = amount;
let loading = false;
let symbol =
{
USD: '$',
CAD: '$',
AUD: '$',
NZD: '$',
MXN: '$',
BRL: '$',
HKD: '$',
TWD: '$',
JPY: '¥',
CNY: '¥',
GBP: '£',
EUR: '€',
KRW: '₩'
}[currency] || '';
$: amount = parseInt(fiat ? Math.round(amountFiat / ($selectedRate / sats)) : amountSats);
$: amountSatsFormatted = s(amountSats);
$: amountFiatConverted = f(amountSats * ($selectedRate / sats), currency);
$: amountSatsConverted = sat(amountFiat / ($selectedRate / sats));
const numPad = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '0', '<'];
const handleInput = (value) => {
if (fiat) {
if (amountFiat === 0 && value !== '.' && value !== '<' && value !== '0') {
amountFiat = value;
} else if ((amountFiat === 0 || amountFiat === '0') && value === '0') {
return;
} else if (amountFiat === 0 && value === '.') {
amountFiat = '0.';
} else if (amountFiat !== 0 && amountFiat.includes('.') && value === '.') {
return;
} else if (value === '<') {
if (amountFiat !== 0) {
amountFiat = amountFiat.slice(0, amountFiat.length - 1);
if (amountFiat.length === 0) {
amountFiat = 0;
amountSats = 0;
}
}
} else if (value !== '.' && value !== '<' && parseInt(amountFiat + value) > $selectedRate) {
warning($t('user.receive.lessThan1BTCWarning'));
} else if (amountFiat !== 0 && amountFiat.match(/\.../)) {
return;
} else {
amountFiat = amountFiat + value;
}
} else {
if (value === '.') {
return;
} else if (!amountSats && value !== '<' && value !== '0') {
amountSats = parseInt(value);
} else if (value === '<') {
if (amountSats !== 0) {
amountSats = Math.floor(amountSats / 10);
if (amountSats.length === 0) {
amountSats = 0;
amountFiat = 0;
}
}
} else if (value !== '<' && parseInt(amountSats + value) > sats) {
warning($t('user.receive.lessThan1BTCWarning'));
} else {
amountSats = parseInt(amountSats + value);
}
}
};
</script>
<div class="flex justify-center items-center mb-3 px-3">
<div class="space-y-5">
<div class="text-center">
<div class="text-5xl md:text-6xl font-semibold tracking-widest mb-1">
{fiat ? `${symbol}${amountFiat}` : '⚡️' + amountSatsFormatted}
</div>
<span class="text-secondary mr-1">{fiat ? amountSatsConverted : amountFiatConverted}</span>
<button
type="button"
on:click={() => {
if (fiat) {
amountSats = parseInt((amountFiat / ($selectedRate / sats)).toFixed(0));
} else {
amountFiat =
(amountSats * ($selectedRate / sats)).toFixed(2) > 0.0
? (amountSats * ($selectedRate / sats)).toFixed(2)
: 0;
}
fiat = !fiat;
}}><Icon icon="swap" style="inline hover:opacity-80" /></button
>
</div>
<div class="grid grid-cols-3 gap-2 w-[300px] mx-auto">
{#each numPad as value}
{#if value === '<'}
<button
type="button"
class="bg-primary rounded-xl py-4 px-8 font-semibold active:bg-black active:text-white flex justify-center items-center hover:opacity-80"
on:click={() => handleInput(value)}
>
<Left />
</button>
{:else}
<button
type="button"
class="bg-primary rounded-xl py-4 px-8 font-semibold active:bg-black active:text-white hover:opacity-80"
on:click={() => handleInput(value)}>{value}</button
>
{/if}
{/each}
</div>
</div>
</div>
|