diff options
| author | Adam Soltys <[email protected]> | 2024-12-24 00:41:51 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-12-24 00:41:51 -0800 |
| commit | 23f0335aeb26b56d835a7134227e43f8798fc3c5 (patch) | |
| tree | d477237d739237e2ca527750be2cd139393fb36a /src/routes/(app)/fund | |
| parent | de913914cc77500c5176cb0aaa2def0acfe98b37 (diff) | |
| download | coinos-ui-23f0335aeb26b56d835a7134227e43f8798fc3c5.tar.gz coinos-ui-23f0335aeb26b56d835a7134227e43f8798fc3c5.zip | |
create fund
Diffstat (limited to 'src/routes/(app)/fund')
| -rw-r--r-- | src/routes/(app)/fund/[id]/+page.server.ts | 7 | ||||
| -rw-r--r-- | src/routes/(app)/fund/[id]/+page.svelte | 137 | ||||
| -rw-r--r-- | src/routes/(app)/fund/[id]/withdraw/+page.server.ts | 15 | ||||
| -rw-r--r-- | src/routes/(app)/fund/[id]/withdraw/+page.svelte | 59 |
4 files changed, 88 insertions, 130 deletions
diff --git a/src/routes/(app)/fund/[id]/+page.server.ts b/src/routes/(app)/fund/[id]/+page.server.ts index 668ea3af..9ea33627 100644 --- a/src/routes/(app)/fund/[id]/+page.server.ts +++ b/src/routes/(app)/fund/[id]/+page.server.ts @@ -2,9 +2,12 @@ import getRates from "$lib/rates"; import { auth, get, post } from "$lib/utils"; import { redirect } from "@sveltejs/kit"; -export const load = async ({ params: { id } }) => { +export const load = async ({ depends, params: { id }, parent }) => { + depends("app:payments"); const data = await get(`/fund/${id}`); - data.rate = await getRates(); + const rates = await getRates(); + const { user } = await parent(); + data.rate = rates[user?.currency || "USD"]; return data; }; diff --git a/src/routes/(app)/fund/[id]/+page.svelte b/src/routes/(app)/fund/[id]/+page.svelte index 23dce871..f99b17d7 100644 --- a/src/routes/(app)/fund/[id]/+page.svelte +++ b/src/routes/(app)/fund/[id]/+page.svelte @@ -1,28 +1,33 @@ <script> + import Amount from "$comp/Amount.svelte"; + import Payments from "$comp/Payments.svelte"; import { format } from "date-fns"; import { enhance } from "$app/forms"; import { page } from "$app/stores"; import Avatar from "$comp/Avatar.svelte"; - import Icon from "$comp/Icon.svelte"; import { toast } from "@zerodevx/svelte-toast"; import { t } from "$lib/translations"; - import { types, copy, f, s, sat, sats } from "$lib/utils"; + import { types, copy, f, s, sat, sats, loc } from "$lib/utils"; import { loginRedirect } from "$lib/store"; + import { bech32 } from "@scure/base"; + const encoder = new TextEncoder(); let { data } = $props(); - let { amount, src, payments, rate, user } = data; + let { amount, payments, rate, user } = data; let { id } = $page.params; - - toast.pop(0); + let locale = loc(user); let currency = user ? user.currency : "CAD"; let amountFiat = $derived(parseFloat(((amount * rate) / sats).toFixed(2))); - $loginRedirect = $page.url.pathname; + $effect(() => ($loginRedirect = $page.url.pathname)); let show = $state(); let toggle = () => (show = !show); + let href = $derived(encodeURIComponent($page.url.href)); + let withdrawUrl = encoder.encode(`${$page.url.origin}/api/fund/${id}/withdraw`); + let lnurlw = bech32.encode("lnurl", bech32.toWords(withdrawUrl), 20000); </script> <div class="container px-4 max-w-4xl mx-auto mt-10 space-y-5"> @@ -30,117 +35,55 @@ <div class="md:shadow-xl rounded-3xl md:px-10 pt-5 pb-10 space-y-5 w-full md:mx-5" > - <div class="relative flex justify-center gap-2"> - <!-- <p class="absolute right-0 top-1 underline"><Icon icon="settings" /></p> --> - {#if id.split("-").length === 4} - <h1 class="text-2xl md:text-3xl font-semibold my-auto">Card {id}</h1> - {:else} - <img src="/images/moneypot.jpg" class="w-24" alt="Fund" /> - <div class="my-auto"> - <h1 class="text-2xl font-semibold my-auto"> - {$t("payments.fund")} - </h1> - <h1 class="text-secondary my-auto">{id}</h1> - </div> - {/if} - </div> {#if show} - <img {src} class="mx-auto" alt={id} /> + <div class="max-w-[360px] mx-auto"> + <img + src={`/qr/${href}/raw`} + class="z-10 border-4 border-white w-" + alt="URL" + /> + </div> {/if} <div class="flex justify-center gap-4"> - <div class="my-auto"> - <h2 class="text-2xl md:text-3xl font-semibold"> - {f(amountFiat, currency)} - </h2> - <h3 class="text-secondary md:text-lg mt-1"> - {sat(amount)} - </h3> - </div> + <Amount {amount} {currency} {rate} {locale} /> </div> <div class="flex gap-2" data-sveltekit-prefetch="off"> <div class="grow"> <a href={`/send/fund/${id}`}> - <button - class="rounded-full border py-3 px-2 font-bold hover:opacity-80 flex gap-1 w-full justify-center" - > - <Icon icon="plus" /> + <button class="btn"> + <iconify-icon icon="ph:plus-bold" width="32"></iconify-icon> {$t("payments.addFunds")} </button> </a> </div> <div class="grow"> <a href={`/fund/${id}/withdraw`}> - <button - class="rounded-full border py-3 px-2 font-bold hover:opacity-80 flex gap-1 w-full justify-center" - > - <Icon icon="minus" style="rotate-180" /> + <button class="btn"> + <iconify-icon + icon="ph:hand-coins-bold" + width="32" + flip="horizontal" + ></iconify-icon> {$t("payments.takeFunds")} </button> </a> </div> </div> <div class="flex gap-2" data-sveltekit-prefetch="off"> - <button - class="rounded-full border py-3 px-2 font-bold hover:opacity-80 flex gap-1 w-full justify-center" - onclick={toggle} - > - <Icon icon="qr" style="invert" /> - {show ? $t("payments.hide") : $t("payments.show")} QR - </button> - <button - class="rounded-full border py-3 px-2 font-bold hover:opacity-80 flex gap-1 w-full justify-center" - onclick={() => copy($page.url.host + $page.url.pathname)} - > - <Icon icon="copy" style="w-6" /> - {$t("payments.copyLink")} - </button> - </div> - <div class="text-base"> - {#each payments as p} - <a href={`/payment/${p.id}`}> - <div class="grid grid-cols-3 border-b h-24 hover:bg-gray-100 px-4"> - <div class="whitespace-nowrap my-auto"> - <div class="font-bold" class:text-red-800={p.amount > 0}> - {f(Math.abs(p.amount) * (p.rate / sats), p.currency)} - {#if p.tip} - <span class="text-sm text-secondary"> - +{Math.round((p.tip / Math.abs(p.amount)) * 100)}% - </span> - {/if} - </div> - - <div class="text-secondary"> - {sat(Math.abs(p.amount) + (p.tip || 0))} - </div> - </div> - - <div class="flex my-auto"> - <div class="flex"> - <div class="my-auto"> - <Avatar user={p.user} size={16} disabled={true} /> - </div> - <div class="my-auto ml-1 text-secondary"> - {p.user.username} - </div> - </div> - </div> - - <div class="text-secondary text-right text-sm my-auto"> - <div> - {format(new Date(p.created), "h:mm aaa")} - </div> - <div> - {format(new Date(p.created), "MMM d")} - </div> - </div> - </div> - </a> - {:else} - <p class="text-secondary text-lg text-center"> - {$t("payments.empty")} - </p> - {/each} + <a href={`/qr/${href}`} class="btn !w-auto grow"> + <iconify-icon icon="ph:link-bold" width="32"></iconify-icon> + <div class="my-auto">{$t("payments.shareLink")}</div> + </a> + <a href={`/qr/${lnurlw}`} class="btn !w-auto grow"> + <iconify-icon + icon="ph:lightning-fill" + width="24" + class="text-yellow-300" + ></iconify-icon> + {$t("payments.lnurlw")} + </a> </div> + <Payments {payments} fund={true} /> </div> </div> </div> diff --git a/src/routes/(app)/fund/[id]/withdraw/+page.server.ts b/src/routes/(app)/fund/[id]/withdraw/+page.server.ts index 1e477918..696c52ff 100644 --- a/src/routes/(app)/fund/[id]/withdraw/+page.server.ts +++ b/src/routes/(app)/fund/[id]/withdraw/+page.server.ts @@ -1,9 +1,15 @@ +import getRates from "$lib/rates"; +import { auth, fd, get, post } from "$lib/utils"; import { fail, redirect } from "@sveltejs/kit"; -import { fd, auth, post } from "$lib/utils"; -export async function load({ parent }) { +export async function load({ params, parent }) { const { user } = await parent(); if (!user) redirect(307, "/register"); + const rates = await getRates(); + const rate = rates[user.currency]; + const fund = await get(`/fund/${params.id}`); + const { amount: balance } = fund; + return { ...params, balance, rate }; } export const actions = { @@ -11,8 +17,9 @@ export const actions = { const body = await fd(request); try { await post("/take", body, auth(cookies)); - } catch (e: any) { - return fail(400, { message: e.message }); + } catch (e) { + const { message } = e as Error; + return fail(400, { message }); } redirect(307, `/fund/${body.id}`); diff --git a/src/routes/(app)/fund/[id]/withdraw/+page.svelte b/src/routes/(app)/fund/[id]/withdraw/+page.svelte index 89654d23..f6f12b1a 100644 --- a/src/routes/(app)/fund/[id]/withdraw/+page.svelte +++ b/src/routes/(app)/fund/[id]/withdraw/+page.svelte @@ -1,40 +1,35 @@ <script> - import { run } from 'svelte/legacy'; - + import handler from "$lib/handler"; import { t } from "$lib/translations"; import { enhance } from "$app/forms"; - import Icon from "$comp/Icon.svelte"; import Numpad from "$comp/Numpad.svelte"; import Spinner from "$comp/Spinner.svelte"; import { page } from "$app/stores"; - import { back } from "$lib/utils"; + import { s } from "$lib/utils"; import { pin } from "$lib/store"; let { data, form } = $props(); - let { rates } = data; - let { amount, id } = $state($page.params); - let { currency } = $state(data.user); + let { currency } = $derived(data.user); let loading = $state(); + let { id, balance, rate } = $derived(data); + let { amount } = $state(data); + let fiat = $state(); - let submit = () => (loading = true); - let update = () => { + let submit = $state(); + let submitting = $state(); + let toggle = () => (submitting = !submitting); + $effect(() => { if (form?.message.includes("pin")) $pin = undefined; loading = false; - }; - - run(() => { - update(form); }); - let rate; - run(() => { - rate = rates[currency]; - }); -</script> -<button class="ml-5 md:ml-20 mt-5 md:mt-10 hover:opacity-80" onclick={back}> - <Icon icon="arrow-left" style="w-10" /> -</button> + let setMax = (e) => { + e.preventDefault(); + fiat = false; + amount = balance; + }; +</script> {#if form?.message} <div class="text-red-600 text-center"> @@ -43,22 +38,32 @@ {/if} <div class="container px-4 mt-20 max-w-xl mx-auto"> - <Numpad bind:amount bind:currency bind:rate /> + <Numpad bind:amount {currency} {fiat} {rate} {submit} /> - <form method="POST" use:enhance onsubmit={submit}> + <form use:enhance={handler(toggle)} method="POST" class="space-y-5"> <input name="id" value={id} type="hidden" /> <input name="amount" value={amount} type="hidden" /> <input name="pin" value={$pin} type="hidden" /> - <div class="flex w-full"> + <div class="flex gap-2"> + <button + type="button" + class="btn !w-auto grow" + onclick={setMax} + disabled={submitting} + onkeydown={setMax}>Max ⚡️{s(balance)}</button + > + <button + use:focus + bind:this={submit} type="submit" - class="opacity-100 hover:opacity-80'} rounded-2xl border py-3 font-bold mx-auto mt-2 bg-black text-white px-4 w-24" + class="btn btn-accent !w-auto grow" > - {#if loading} + {#if submitting} <Spinner /> {:else} - {$t("payments.send")} + <div class="my-auto">{$t("payments.next")}</div> {/if} </button> </div> |
