From 1f63848ca9fb01336f04b41434de1cc06a5b3d93 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 10 Sep 2024 20:39:47 -0700 Subject: move routes --- src/routes/(app)/invoice/+page.server.ts | 29 ++ src/routes/(app)/invoice/+page.svelte | 0 src/routes/(app)/invoice/+server.ts | 7 + src/routes/(app)/invoice/[id]/+layout.server.ts | 36 +++ src/routes/(app)/invoice/[id]/+layout.svelte | 1 + src/routes/(app)/invoice/[id]/+page.server.ts | 5 + src/routes/(app)/invoice/[id]/+page.svelte | 351 +++++++++++++++++++++ src/routes/(app)/invoice/[id]/memo/+page.server.ts | 13 + src/routes/(app)/invoice/[id]/memo/+page.svelte | 95 ++++++ src/routes/(app)/invoice/[id]/paid/+page.svelte | 85 +++++ src/routes/(app)/invoice/[id]/tip/+page.server.ts | 12 + src/routes/(app)/invoice/[id]/tip/+page.svelte | 209 ++++++++++++ src/routes/(app)/invoice/sent/+page.svelte | 11 + 13 files changed, 854 insertions(+) create mode 100644 src/routes/(app)/invoice/+page.server.ts create mode 100644 src/routes/(app)/invoice/+page.svelte create mode 100644 src/routes/(app)/invoice/+server.ts create mode 100644 src/routes/(app)/invoice/[id]/+layout.server.ts create mode 100644 src/routes/(app)/invoice/[id]/+layout.svelte create mode 100644 src/routes/(app)/invoice/[id]/+page.server.ts create mode 100644 src/routes/(app)/invoice/[id]/+page.svelte create mode 100644 src/routes/(app)/invoice/[id]/memo/+page.server.ts create mode 100644 src/routes/(app)/invoice/[id]/memo/+page.svelte create mode 100644 src/routes/(app)/invoice/[id]/paid/+page.svelte create mode 100644 src/routes/(app)/invoice/[id]/tip/+page.server.ts create mode 100644 src/routes/(app)/invoice/[id]/tip/+page.svelte create mode 100644 src/routes/(app)/invoice/sent/+page.svelte (limited to 'src/routes/(app)/invoice') diff --git a/src/routes/(app)/invoice/+page.server.ts b/src/routes/(app)/invoice/+page.server.ts new file mode 100644 index 00000000..83127457 --- /dev/null +++ b/src/routes/(app)/invoice/+page.server.ts @@ -0,0 +1,29 @@ +import { auth, post } from "$lib/utils"; +import { error, redirect } from "@sveltejs/kit"; +import type { Invoice } from "$lib/types"; + +export let load = async ({ cookies, params, parent }) => { + let { account } = params; + let { subject, user, rates } = await parent(); + + let invoice: Invoice = { + account, + type: account ? "bitcoin" : "lightning", + rate: rates[user?.currency || subject?.currency], + }; + + if (!user) user = subject; + + try { + invoice = await post("/invoice", { invoice, account, user }, auth(cookies)); + } catch (e) { + console.log(e); + error(500, "Failed to generate invoice"); + } + + let { id } = invoice; + + if (invoice.memoPrompt && !invoice.memo) { + redirect(307, `/invoice/${id}/memo`); + } else redirect(307, `/invoice/${id}`); +}; diff --git a/src/routes/(app)/invoice/+page.svelte b/src/routes/(app)/invoice/+page.svelte new file mode 100644 index 00000000..e69de29b diff --git a/src/routes/(app)/invoice/+server.ts b/src/routes/(app)/invoice/+server.ts new file mode 100644 index 00000000..5e3d0300 --- /dev/null +++ b/src/routes/(app)/invoice/+server.ts @@ -0,0 +1,7 @@ +import { auth, post } from "$lib/utils"; +import { json } from "@sveltejs/kit"; + +export let POST = async ({ cookies: c, request: r }) => { + let j = await r.json(); + return json(await post("/invoice", j, auth(c))); +}; diff --git a/src/routes/(app)/invoice/[id]/+layout.server.ts b/src/routes/(app)/invoice/[id]/+layout.server.ts new file mode 100644 index 00000000..e7144b15 --- /dev/null +++ b/src/routes/(app)/invoice/[id]/+layout.server.ts @@ -0,0 +1,36 @@ +import Qr from "qrcode-base64"; +import { get } from "$lib/utils"; +import { redirect } from "@sveltejs/kit"; + +export async function load({ depends, params, url, parent }) { + depends("app:invoice"); + + let { subject, user } = await parent(); + let { id } = params; + let invoice = await get(`/invoice/${id}`); + let options = !!url.searchParams.get("options"); + + if ( + user && + invoice.uid !== user.id && + !(url.pathname.includes("tip") || url.pathname.includes("memo")) && + !options + ) { + redirect(307, `/send/invoice/${id}`); + } + + let { amount, pending, received } = invoice; + amount = parseInt(amount); + + let paid = + (!amount && (pending || received)) || + (amount > 0 && (pending >= amount || received >= amount)); + if (paid && !url.pathname.endsWith("paid")) { + redirect( 307, `/invoice/${id}/paid` + (options ? "?options=true" : "") + ); + } + + let src = Qr.drawImg(invoice.text || "", { size: 340 }); + + return { id, invoice, subject, src }; +} diff --git a/src/routes/(app)/invoice/[id]/+layout.svelte b/src/routes/(app)/invoice/[id]/+layout.svelte new file mode 100644 index 00000000..4fa864ce --- /dev/null +++ b/src/routes/(app)/invoice/[id]/+layout.svelte @@ -0,0 +1 @@ + diff --git a/src/routes/(app)/invoice/[id]/+page.server.ts b/src/routes/(app)/invoice/[id]/+page.server.ts new file mode 100644 index 00000000..a57efa64 --- /dev/null +++ b/src/routes/(app)/invoice/[id]/+page.server.ts @@ -0,0 +1,5 @@ +import invoice from "$lib/invoice"; + +export const actions = { + default: invoice, +}; diff --git a/src/routes/(app)/invoice/[id]/+page.svelte b/src/routes/(app)/invoice/[id]/+page.svelte new file mode 100644 index 00000000..c4810889 --- /dev/null +++ b/src/routes/(app)/invoice/[id]/+page.svelte @@ -0,0 +1,351 @@ + + +
+ {#if $showQr} +
+ + {txt} + +
+ {:else} +
+ {txt} +
+ {/if} + + {#each invoice.items as i} +
+
{i.quantity}
+
+ {i.name} +
+
+ {f(i.price * i.quantity, currency)} +
+
+ {sat(btc(i.price * i.quantity, rate))} +
+
+ {/each} + + {#if amount > 0} +
+
+ {f(amountFiat, currency)} + + {#if tip} + + +{f(tip * (rate / sats), currency)} + + {/if} +
+
+ ⚡️{`${s(amount)}`} + + {#if tip} + + +⚡️{s(tip)} + + {/if} +
+
+ {/if} + +
+ + + {#if user?.id === invoice?.user?.id} + + {:else} + + + + {/if} + +
+ +
+
+ + {#if !account} +
+ + + + + +
+ {/if} + + {#if type === types.liquid} +
+
+ {$t("payments.onlyLbtc")} + L-BTC, + {$t("payments.dontDeposit")} +
+
+ {/if} + + {#if user?.id === invoice?.user?.id} + + + + {/if} +
+ +{#if settingAmount} +
+
+
+ +
+ + +
+ + +
+ {$t("payments.amountPrompt")} + +
+
+
+{/if} + + diff --git a/src/routes/(app)/invoice/[id]/memo/+page.server.ts b/src/routes/(app)/invoice/[id]/memo/+page.server.ts new file mode 100644 index 00000000..24dec1af --- /dev/null +++ b/src/routes/(app)/invoice/[id]/memo/+page.server.ts @@ -0,0 +1,13 @@ +import invoice from "$lib/invoice"; +import { redirect } from "@sveltejs/kit"; + +export async function load({ parent, params }) { + let { invoice } = await parent(); + if (!invoice.amount) { + redirect(307, `/invoice/${invoice.id}`); + } +} + +export const actions = { + default: invoice, +}; diff --git a/src/routes/(app)/invoice/[id]/memo/+page.svelte b/src/routes/(app)/invoice/[id]/memo/+page.svelte new file mode 100644 index 00000000..4d23ff19 --- /dev/null +++ b/src/routes/(app)/invoice/[id]/memo/+page.svelte @@ -0,0 +1,95 @@ + + +
+ + + + + + + + + + +

{$t("invoice.addMemo")}

+
+ +
{invoice.memoPrompt}
+
+ + +
diff --git a/src/routes/(app)/invoice/[id]/paid/+page.svelte b/src/routes/(app)/invoice/[id]/paid/+page.svelte new file mode 100644 index 00000000..df05f12b --- /dev/null +++ b/src/routes/(app)/invoice/[id]/paid/+page.svelte @@ -0,0 +1,85 @@ + + +
+ {#if received} +
+ +
+ +

+ {$t("invoice.paymentSuccessful")} +

+ +

+ {f(fiat(received - tip, rate), currency)} + {#if tip} + + + {f(fiat(tip, rate), currency)} + + {/if} +

+

+ ⚡️{s(received - tip)} + + {#if tip} + + + ⚡️{s(tip)} + + {/if} +

+ {/if} + + {#if pending} +
+ +
+ +

Payment detected

+

+ {f(fiat(pending - tip, rate), currency)} + {#if tip} + + {f(fiat(tip, rate), currency)} + {/if} +

+ +

+ ⚡️{s(pending - tip)} + {#if tip} + + + ⚡️{s(tip)} + + {/if} +

+ {/if} +
+ + +
+ + +
+ {$t("payments.tapAnywhere")} +
diff --git a/src/routes/(app)/invoice/[id]/tip/+page.server.ts b/src/routes/(app)/invoice/[id]/tip/+page.server.ts new file mode 100644 index 00000000..8417de91 --- /dev/null +++ b/src/routes/(app)/invoice/[id]/tip/+page.server.ts @@ -0,0 +1,12 @@ +import invoice from "$lib/invoice"; +import { redirect } from "@sveltejs/kit"; + +export async function load({ parent, params }) { + let { invoice } = await parent(); + if (!invoice.amount) + redirect(307, `/invoice/${invoice.id}`); +} + +export const actions = { + default: invoice, +}; diff --git a/src/routes/(app)/invoice/[id]/tip/+page.svelte b/src/routes/(app)/invoice/[id]/tip/+page.svelte new file mode 100644 index 00000000..35b24bf1 --- /dev/null +++ b/src/routes/(app)/invoice/[id]/tip/+page.svelte @@ -0,0 +1,209 @@ + + +
+
+ + + + + + + + + + {#if invoice.memoPrompt} + + {/if} + +

{$t("invoice.addTipq")}

+ {#if !showCustomAmount} +
{Math.round(tipPercent)}%
+ + + +
+
+ + {f(amountFiat, currency)} + +{f(tipAmount, currency)} + +
+ +
+ {sat(amount)} + +{sat(tip)} +
+
+ + {#if tip} +
+ + +
+ {:else} + {#each tipAmounts as amount} + + {/each} + {/if} + +
+ +
+ {/if} + + +
+ + {/if} +
+ + diff --git a/src/routes/(app)/invoice/sent/+page.svelte b/src/routes/(app)/invoice/sent/+page.svelte new file mode 100644 index 00000000..b95bc85e --- /dev/null +++ b/src/routes/(app)/invoice/sent/+page.svelte @@ -0,0 +1,11 @@ + + +Invoice sent to +
+ +

{$request.requester.username} is ready to pay

+
-- cgit v1.2.3