From e447e9c9d76af97cef42ad7e24ddb9250d579b58 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 21 Feb 2024 15:23:12 -0800 Subject: fix back button, abort long requests --- src/lib/utils.js | 17 ++++++++++++++--- src/routes/(app)/send/bitcoin/[address]/+page.svelte | 9 +++++---- .../[address]/[amount]/[...feeRate]/+page.server.js | 1 + 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib/utils.js b/src/lib/utils.js index 53437f56..d3ad0da3 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -41,16 +41,24 @@ export let get = (url, headers = {}) => { }); }; -export let post = (url, body, headers) => { +export let post = async (url, body, headers, timeout = 8000) => { headers = { - ...headers, "content-type": "application/json", accept: "application/json", + ...headers, }; - return fetch(base + url, { + + let controller = new AbortController(); + console.log(controller, timeout); + let id = setTimeout(() => { + controller.abort(new Error("Operation timed out")); + }, timeout); + + let response = await fetch(base + url, { method: "POST", body: JSON.stringify(body), headers, + signal: controller.signal, }) .then(async (r) => { if (r.ok) return r.text(); @@ -77,6 +85,9 @@ export let post = (url, body, headers) => { return body; }); + + clearTimeout(id); + return response; }; export let copy = (text) => { diff --git a/src/routes/(app)/send/bitcoin/[address]/+page.svelte b/src/routes/(app)/send/bitcoin/[address]/+page.svelte index bd54cf83..84f14c39 100644 --- a/src/routes/(app)/send/bitcoin/[address]/+page.svelte +++ b/src/routes/(app)/send/bitcoin/[address]/+page.svelte @@ -4,13 +4,13 @@ import Numpad from "$comp/Numpad.svelte"; import { page } from "$app/stores"; import { rate } from "$lib/store"; - import { back, fail, s } from "$lib/utils"; + import { fail, s } from "$lib/utils"; export let data; - let { rates } = data; + let { rates, user } = data; let { address } = $page.params; - let { balance, currency } = data.user; + let { balance, currency, username } = user; let amount = 0; let submit, fiat; @@ -24,13 +24,14 @@ }; + +

{$t("payments.send")}

diff --git a/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.js b/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.js index 318e33c4..9d86537d 100644 --- a/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.js +++ b/src/routes/(app)/send/bitcoin/[address]/[amount]/[...feeRate]/+page.server.js @@ -26,6 +26,7 @@ export const actions = { try { let r = await post("/bitcoin/send", await fd(request), auth(cookies)); } catch (e) { + console.log("problem sending bitcoin", e); return fail(400, { address, amount, feeRate, message: e.message }); } -- cgit v1.2.3