aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-04-25 14:47:20 -0700
committerAdam Soltys <[email protected]>2024-04-25 14:47:20 -0700
commit89e69e740e0011858eca3af7d402194e3e9019f3 (patch)
tree6cb322ae5dbc388d43fd61644e468030007ed9fe /src/lib
parent1a63abd1d8cf89ead9d5e7be73b6ba82098da491 (diff)
downloadcoinos-ui-89e69e740e0011858eca3af7d402194e3e9019f3.tar.gz
coinos-ui-89e69e740e0011858eca3af7d402194e3e9019f3.zip
update js
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/invoice.js20
-rw-r--r--src/lib/samples.js26
-rw-r--r--src/lib/utils.js43
3 files changed, 57 insertions, 32 deletions
diff --git a/src/lib/invoice.js b/src/lib/invoice.js
index 06a9a94e..d2d3d13d 100644
--- a/src/lib/invoice.js
+++ b/src/lib/invoice.js
@@ -6,16 +6,16 @@ export default async ({ cookies, request, url }) => {
let rates = await get("/rates");
let amount = parseInt(form.get("amount"));
- let request_id = form.get("request_id");
- if (request_id === "undefined") request_id = undefined;
let invoice = {
amount,
+ items: JSON.parse(form.get("items")),
+ memo: form.get("memo"),
+ memoPrompt: form.get("memoPrompt"),
tip: parseInt(form.get("tip")) || 0,
type: form.get("type"),
prompt: form.get("prompt") === "true",
rate: parseFloat(form.get("rate")) || rates[form.get("currency")],
- request_id,
};
let user = { username: form.get("username"), currency: form.get("currency") };
@@ -23,14 +23,10 @@ export default async ({ cookies, request, url }) => {
invoice = await post("/invoice", { invoice, user }, auth(cookies));
let { id } = invoice;
- if (request_id) {
- if (url.pathname.endsWith("tip")) {
- redirect(307, `/send/invoice/${id}`);
- }
+ if (invoice.prompt && invoice.tip === null)
+ redirect(307, `/${user.username}/invoice/${id}/tip`);
- redirect(307, `/${user.username}/request/${request_id}`);
- }
-
- if (invoice.prompt) redirect(307, `/${user.username}/invoice/${id}/tip`);
- else redirect(307, `/${user.username}/invoice/${id}`);
+ if (invoice.memoPrompt && !invoice.memo) {
+ redirect(307, `/${user.username}/invoice/${id}/memo`);
+ } else redirect(307, `/${user.username}/invoice/${id}`);
};
diff --git a/src/lib/samples.js b/src/lib/samples.js
index 6624064b..85a85ef8 100644
--- a/src/lib/samples.js
+++ b/src/lib/samples.js
@@ -5,34 +5,34 @@ let api = PUBLIC_DOMAIN.includes("localhost")
: `https://${PUBLIC_DOMAIN}/api`;
export default {
- register: `curl "https://swapee.me/api/register" -H "content-type: application/json" -d '{
+ register: `curl "${api}/register" -H "content-type: application/json" -d '{
"user": {
"username": "demo",
"password": "hunter2"
}
}'`,
- login: `curl "https://swapee.me/api/login" -H "content-type: application/json" -d '{
+ login: `curl "${api}/login" -H "content-type: application/json" -d '{
"username": "demo",
"password": "hunter2"
}'`,
- payments: `curl "https://swapee.me/api/payments"
+ payments: `curl "${api}/payments"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
`,
- lightning: `curl "https://swapee.me/api/payments"
+ lightning: `curl "${api}/payments"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
-d '{
"payreq": "lnbc1pj94d8fsp5n77k340ps4m9jn7kp8he8yynpddvurv6mcsrrqpnq5l2jxdxzlwqpp5m96pqhc5nrlk8cqsu9ufdxxa43sarp8vwf9egvm2pg9nl0zu9r8qdq2vdhkjmn0wvxqztgcqpjrzjqwhmav82kntsppmkp8jp4vg4h9nns78tsy8mg7ve4lq5txrkp0h56zlarvqqdtcqqsqqqqqqqqqqp6cq9q9qyysgqzxlypywzphyujm3ga5j5csfcmqvlnae0fgnvymkaaw94eeg7py5rqzysyjkr3ev2snq63qpsc69vf54adkd0szvmvwt5cuadjnuy95sq4xfa40"
}'`,
- bitcoin: `curl "https://swapee.me/api/payments"
+ bitcoin: `curl "${api}/bitcoin/send"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
-d '{
"amount": 5000,
"address": "bc1q3unh97w4rmelflrm2hvdwz37d8kray3vn4d5ca"
}'`,
- internal: `HASH=$(curl "https://swapee.me/api/invoice"
+ internal: `HASH=$(curl "${api}/invoice"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
-d '{
@@ -45,17 +45,17 @@ export default {
}
}' | jq -r '.hash');
- curl "https://swapee.me/api/payments"
+ curl "${api}/payments"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
-d '{
"amount": 5000,
"hash": $HASH
}'`,
- rates: `curl "https://swapee.me/api/rates"
+ rates: `curl "${api}/rates"
-H "content-type: application/json"
}'`,
- lightningInvoice: `curl "https://swapee.me/api/invoice"
+ lightningInvoice: `curl "${api}/invoice"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
-d '{
@@ -64,7 +64,7 @@ export default {
"type": "lightning"
}
}'`,
- bitcoinAddress: `curl "https://swapee.me/api/invoice"
+ bitcoinAddress: `curl "${api}/invoice"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
-d '{
@@ -73,7 +73,7 @@ export default {
"type": "bitcoin"
}
}'`,
- webhook: `curl "https://swapee.me/api/invoice"
+ webhook: `curl "${api}/invoice"
-H "content-type: application/json"
-H "Authorization: Bearer $token"
-d '{
@@ -83,7 +83,7 @@ export default {
"secret": "webhooksecret"
}
}'`,
- fetchInvoice: `curl "https://swapee.me/api/invoice/bc1qmhfk9stzffhd9umzmld92vff7zg3mdlh7rvvaj"
+ fetchInvoice: `curl "${api}/invoice/bc1qmhfk9stzffhd9umzmld92vff7zg3mdlh7rvvaj"
-H "content-type: application/json"'`,
invoiceResponse: `{
"amount":3141
@@ -98,5 +98,5 @@ export default {
"received":0
"created":1677537428134
}`,
- socketAuth: `echo '{"type":"login","data":{"username":"user","password":"password"}}' | websocat -n https://swapee.me/ws`
+ socketAuth: `echo '{"type":"login","data":{"username":"user","password":"password"}}' | websocat -n https://coinos.io/ws`,
};
diff --git a/src/lib/utils.js b/src/lib/utils.js
index 8448e3c6..6902c928 100644
--- a/src/lib/utils.js
+++ b/src/lib/utils.js
@@ -41,13 +41,14 @@ export let get = (url, headers = {}) => {
});
};
-export let post = (url, body, headers) => {
- headers = {
- ...headers,
+export let post = async (url, body, headers) => {
+ headers = {
"content-type": "application/json",
accept: "application/json",
+ ...headers,
};
- return fetch(base + url, {
+
+ let response = await fetch(base + url, {
method: "POST",
body: JSON.stringify(body),
headers,
@@ -77,6 +78,8 @@ export let post = (url, body, headers) => {
return body;
});
+
+ return response;
};
export let copy = (text) => {
@@ -101,7 +104,11 @@ export function reverseFormat(val, locale) {
export let protectedRoutes = [/customers/, /settings/, /payments/];
+let recent = [];
export let success = (m, clear = true) => {
+ if (recent.includes(m)) return;
+ recent.push(m);
+ setTimeout(() => (recent = []), 5000);
if (clear) toast.pop();
toast.push(m, {
theme: {
@@ -152,13 +159,14 @@ export let login = async (user, cookies, ip) => {
if (text.startsWith("2fa")) throw new Error("2fa");
}
- let { token } = await res.json();
+ let { user: u, token } = await res.json();
if (!token) throw new Error("Login failed");
let expires = new Date();
expires.setSeconds(expires.getSeconds() + maxAge);
let opts = { path: "/", expires };
+ if (u.language) cookies.set("lang", u.language, opts);
cookies.set("username", user.username, opts);
cookies.set("token", token, opts);
};
@@ -208,9 +216,13 @@ export let sats = 100000000;
export let back = () =>
browser && (history.length ? history.go(-1) : goto("/"));
+
export let focus = (el) =>
browser && screen.width > 1280 && setTimeout(() => el.focus(), 1);
+export let select = (el) =>
+ browser && screen.width > 1280 && setTimeout(() => el.select(), 1);
+
export let sleep = (n) => new Promise((r) => setTimeout(r, n));
export let wait = async (f, n = 100, s = 300) => {
let i = 0;
@@ -271,7 +283,6 @@ export let types = {
lightning: "lightning",
internal: "internal",
fund: "fund",
- classic: "classic",
};
export let ease = (t) =>
@@ -301,4 +312,22 @@ export let isLiquid = (text) =>
(text.startsWith("ert1q") && text.length === 43) ||
(text.startsWith("ex1q") && text.length === 42) ||
text.startsWith("el1qq") ||
- text.startsWith("lq1qq"); \ No newline at end of file
+ text.startsWith("lq1qq");
+
+export function getCookie(name) {
+ let nameEQ = name + "=";
+ let ca = document.cookie.split(";");
+ for (let i = 0; i < ca.length; i++) {
+ let c = ca[i];
+ while (c.charAt(0) === " ") c = c.substring(1);
+ if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
+ }
+ return null;
+}
+
+export function setCookie(name, value, seconds) {
+ const now = new Date();
+ now.setTime(now.getTime() + seconds * 1000);
+ const expires = "expires=" + now.toUTCString();
+ document.cookie = name + "=" + value + ";" + expires + ";path=/";
+}