diff options
| author | Adam Soltys <[email protected]> | 2024-10-29 14:40:25 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-29 14:40:25 -0700 |
| commit | a92dd4d39a86af41dd9cd6c9f58263a13f852906 (patch) | |
| tree | d48137096599fcd15560724690124e0175d84c40 /src | |
| parent | 0cf898e843f23a915f0bf3787330336d9bb8c5d1 (diff) | |
| download | coinos-ui-a92dd4d39a86af41dd9cd6c9f58263a13f852906.tar.gz coinos-ui-a92dd4d39a86af41dd9cd6c9f58263a13f852906.zip | |
check for user before preloading
Diffstat (limited to 'src')
| -rw-r--r-- | src/routes/(app)/+layout.svelte | 10 | ||||
| -rw-r--r-- | src/routes/(app)/invoice/+page.server.ts | 40 | ||||
| -rw-r--r-- | src/routes/(app)/invoice/+server.ts | 6 | ||||
| -rw-r--r-- | src/routes/(app)/pay/[username]/[...amount]/+page.server.ts | 80 | ||||
| -rw-r--r-- | src/routes/(app)/pay/[username]/[...amount]/+page.svelte | 24 |
5 files changed, 84 insertions, 76 deletions
diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index 6efb3c6c..ca0b5f90 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -31,10 +31,12 @@ }; afterNavigate(() => { - preloadData(`/${user.username}`); - preloadData(`/${user.username}/receive`); - preloadData("/payments"); - preloadData("/send"); + if (user) { + preloadData(`/${user.username}`); + preloadData(`/${user.username}/receive`); + preloadData("/payments"); + preloadData("/send"); + } }); onMount(async () => { diff --git a/src/routes/(app)/invoice/+page.server.ts b/src/routes/(app)/invoice/+page.server.ts index 5f3c38d3..8d2f3b7f 100644 --- a/src/routes/(app)/invoice/+page.server.ts +++ b/src/routes/(app)/invoice/+page.server.ts @@ -1,29 +1,29 @@ +import type { Invoice } from "$lib/types"; import { auth, post } from "$lib/utils"; import { error, redirect } from "@sveltejs/kit"; -import type { Invoice } from "$lib/types"; -export let load = async ({ cookies, parent }) => { - let aid = cookies.get("aid"); - let { subject, user, rates } = await parent(); - if (aid === user.id) redirect(307, `/${user.username}/receive`); +export const load = async ({ cookies, parent }) => { + const aid = cookies.get("aid"); + let { subject, user, rates } = await parent(); + if (aid === user.id) redirect(307, `/${user.username}/receive`); - let invoice: Invoice = { - aid, - rate: rates[user?.currency || subject?.currency], - }; + let invoice: Invoice = { + aid, + rate: rates[user?.currency || subject?.currency], + }; - if (!user) user = subject; + if (!user) user = subject; - try { - invoice = await post("/invoice", { invoice, aid, user }, auth(cookies)); - } catch (e) { - console.log(e); - error(500, "Failed to generate invoice"); - } + try { + invoice = await post("/invoice", { invoice, aid, user }, auth(cookies)); + } catch (e) { + console.log(e); + error(500, "Failed to generate invoice"); + } - let { id } = invoice; + const { id } = invoice; - if (invoice.memoPrompt && !invoice.memo) { - redirect(307, `/invoice/${id}/memo`); - } else redirect(307, `/invoice/${id}`); + if (invoice.memoPrompt && !invoice.memo) { + redirect(307, `/invoice/${id}/memo`); + } else redirect(307, `/invoice/${id}`); }; diff --git a/src/routes/(app)/invoice/+server.ts b/src/routes/(app)/invoice/+server.ts index 0b5eb04e..f20d70f3 100644 --- a/src/routes/(app)/invoice/+server.ts +++ b/src/routes/(app)/invoice/+server.ts @@ -1,7 +1,7 @@ import { auth, post } from "$lib/utils"; import { json } from "@sveltejs/kit"; -export const POST = async ({ cookies: c, request: r }) => { - const j = await r.json(); - return json(await post("/invoice", j, auth(c))); +export const POST = async ({ cookies, request }) => { + const body = await request.json(); + return json(await post("/invoice", body, auth(cookies))); }; diff --git a/src/routes/(app)/pay/[username]/[...amount]/+page.server.ts b/src/routes/(app)/pay/[username]/[...amount]/+page.server.ts index 61809cf7..60288813 100644 --- a/src/routes/(app)/pay/[username]/[...amount]/+page.server.ts +++ b/src/routes/(app)/pay/[username]/[...amount]/+page.server.ts @@ -1,53 +1,55 @@ +import { auth, fd, get, post, sats, types } from "$lib/utils"; import { error, fail, redirect } from "@sveltejs/kit"; -import { types, sats, fd, auth, get, post } from "$lib/utils"; export async function load({ cookies, params, parent }) { - let { username } = params; - let { rates, user } = await parent(); + const { username } = params; + const { rates, user } = await parent(); - if (user?.username === username) error(500, "Cannot send to self"); - let subject = await get(`/users/${username}`); + if (user?.username === username) error(500, "Cannot send to self"); + const subject = await get(`/users/${username}`); - let [amount, currency] = params.amount.split("/"); + let [amount, currency] = params.amount.split("/"); - let rate; - if (amount) { - rate = rates[currency ? currency.toUpperCase() : subject.currency]; - if (!rate) error(500, "Invalid currency symbol"); - if (currency) amount = (amount * sats) / rate; + let rate; - let { id } = await post( - "/invoice", - { - invoice: { - amount, - currency: currency === subject.currency ? currency : undefined, - prompt: false, - rate: currency === subject.currency ? rate : undefined, - type: types.lightning, - }, - user: { username }, - }, - auth(cookies), - ); + console.log("HI", amount); + if (amount) { + rate = rates[currency ? currency.toUpperCase() : subject.currency]; + if (!rate) error(500, "Invalid currency symbol"); + if (currency) amount = (amount * sats) / rate; - redirect(307, `/invoice/${id}`); - } + const { id } = await post( + "/invoice", + { + invoice: { + amount, + currency: currency === subject.currency ? currency : undefined, + prompt: false, + rate: currency === subject.currency ? rate : undefined, + type: types.lightning, + }, + user: { username }, + }, + auth(cookies), + ); - return { amount, rate, subject }; + redirect(307, `/invoice/${id}`); + } + + return { amount, rate, subject }; } export const actions = { - default: async ({ cookies, request }) => { - let p; - try { - let body = await fd(request); - p = await post("/payments", body, auth(cookies)); - } catch (e: any) { - console.log(e); - return fail(400, { message: e.message }); - } + default: async ({ cookies, request }) => { + let p; + try { + const body = await fd(request); + p = await post("/payments", body, auth(cookies)); + } catch (e: any) { + console.log(e); + return fail(400, { message: e.message }); + } - redirect(307, `/sent/${p.id}`); - }, + redirect(307, `/sent/${p.id}`); + }, }; diff --git a/src/routes/(app)/pay/[username]/[...amount]/+page.svelte b/src/routes/(app)/pay/[username]/[...amount]/+page.svelte index 8e3abab4..5089e78c 100644 --- a/src/routes/(app)/pay/[username]/[...amount]/+page.svelte +++ b/src/routes/(app)/pay/[username]/[...amount]/+page.svelte @@ -8,7 +8,7 @@ import Numpad from "$comp/Numpad.svelte"; import Spinner from "$comp/Spinner.svelte"; import { page } from "$app/stores"; - import { f, post, s, sat, sats } from "$lib/utils"; + import { f, fail, post, s, sat, sats } from "$lib/utils"; export let data; export let form; @@ -32,15 +32,19 @@ rate = fiat ? (sats * amountFiat) / amount : r; let id; - ({ id, hash } = await post(`/invoice`, { - invoice: { - amount, - rate: rates[subject.currency], - prompt: subject.prompt, - type: "lightning", - }, - user: subject, - })); + try { + ({ id, hash } = await post(`/invoice`, { + invoice: { + amount, + rate: rates[subject.currency], + prompt: subject.prompt, + type: "lightning", + }, + user: subject, + })); + } catch (e) { + fail(e.message); + } goto(`/send/invoice/${id}`); }; |
