diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/socket.js | 8 | ||||
| -rw-r--r-- | src/lib/utils.js | 8 | ||||
| -rw-r--r-- | src/routes/[username]/dashboard.svelte | 49 | ||||
| -rw-r--r-- | src/routes/withdraw.js | 10 |
4 files changed, 63 insertions, 12 deletions
diff --git a/src/lib/socket.js b/src/lib/socket.js index c1b68a12..a30d4376 100644 --- a/src/lib/socket.js +++ b/src/lib/socket.js @@ -1,5 +1,6 @@ import { get } from 'svelte/store'; import { rate, user, token, invoices, preferredCurrency, newPayment } from '$lib/store'; +import { success } from '$lib/utils'; let interval, socket; @@ -18,8 +19,13 @@ export const messages = (data) => ({ }, payment() { - invoices.set({ ...get(invoices), [data.invoice.uuid]: data.invoice }); + if (data.invoice) invoices.set({ ...get(invoices), [data.invoice.uuid]: data.invoice }); newPayment.set(true); + if (data.amount > 0) { + success(`Received ${data.amount} sats!`); + } else { + success(`Sent ${-data.amount} sats!`); + } }, connected: auth, diff --git a/src/lib/utils.js b/src/lib/utils.js index a812c136..24f94d40 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -18,11 +18,12 @@ export const get = (url, headers = { accept: 'application/json' }) => } }); -export const post = (url, body) => - fetch(base + url, { +export const post = (url, body, headers) => { + headers = { ...headers, accept: 'application/json', 'content-type': 'application/json' }; + return fetch(base + url, { method: 'POST', body: JSON.stringify(body), - headers: { accept: 'application/json', 'content-type': 'application/json' } + headers }) .then((r) => r.text()) .then((body) => { @@ -37,6 +38,7 @@ export const post = (url, body) => return body; }); +} export const copy = (text) => { navigator.clipboard.writeText(text); diff --git a/src/routes/[username]/dashboard.svelte b/src/routes/[username]/dashboard.svelte index 11295728..7fab2125 100644 --- a/src/routes/[username]/dashboard.svelte +++ b/src/routes/[username]/dashboard.svelte @@ -1,7 +1,29 @@ <script> + import { post, failure } from '$lib/utils'; + import { tick } from 'svelte'; import { AppHeader } from '$comp'; import { rate, user, preferredCurrency } from '$lib/store'; + let payreq = '', + payreqField, + withdrawing; + + let toggle = async () => { + withdrawing = !withdrawing; + await tick(); + if (withdrawing) payreqField.focus(); + }; + + let withdraw = async () => { + try { + await post('/withdraw', { payreq }); + withdrawing = false; + } catch (e) { + console.log(e); + failure("Withdrawal failed, please contact support."); + } + }; + $: btcPrice = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' @@ -18,7 +40,7 @@ $user.account.balance ); - $: feeAmount = new Intl.NumberFormat('en-US', { + $: feepayreq = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(($user.account.balance * ($rate / 100000000)) / 100); @@ -55,14 +77,25 @@ >{$rate ? accountBalanceFiat : 'fetching rate...'}</span > - <span class="text-secondary text-xl block" - >{accountBalanceSats} SATS - </span> + <span class="text-secondary text-xl block">{accountBalanceSats} SATS </span> + + <button class="rounded-full border py-2 font-bold w-28 mt-4" on:click={toggle} + >Withdraw</button + > + <p class="text-secondary mt-2">You will be charged a 1% withdrawal fee on the payreq.</p> - <button class="rounded-full border py-2 font-bold w-28 mt-4">Withdraw</button> - <p class="text-secondary mt-2"> - You will be charged a 1% withdrawal fee on the amount. - </p> + {#if withdrawing} + <input + bind:this={payreqField} + type="text" + name="payreq" + bind:value={payreq} + class="block border rounded-xl p-3 w-full" + /> + <button class="rounded-full border py-2 font-bold w-28 mt-4" on:click={withdraw} + >Submit</button + > + {/if} </div> </div> {/if} diff --git a/src/routes/withdraw.js b/src/routes/withdraw.js new file mode 100644 index 00000000..aaed2250 --- /dev/null +++ b/src/routes/withdraw.js @@ -0,0 +1,10 @@ +import { post } from '$lib/utils'; +export async function POST({ locals, request }) { + let { token } = locals; + let body = await request.json(); + console.log(body) + body = await post('/lightning/send', body, { + authorization: `Bearer ${token}` + }); + return { body }; +} |
