From e3d36e6a3ab966b8acadba546b8f68ed81906729 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 7 Jul 2022 11:37:26 +0300 Subject: [PATCH] feat: extract utils to static/js --- lnbits/extensions/satspay/__init__.py | 9 ++++++ lnbits/extensions/satspay/static/js/utils.js | 31 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 lnbits/extensions/satspay/static/js/utils.js diff --git a/lnbits/extensions/satspay/__init__.py b/lnbits/extensions/satspay/__init__.py index f33f3aa52..37245c21d 100644 --- a/lnbits/extensions/satspay/__init__.py +++ b/lnbits/extensions/satspay/__init__.py @@ -1,6 +1,7 @@ import asyncio from fastapi import APIRouter +from fastapi.staticfiles import StaticFiles from lnbits.db import Database from lnbits.helpers import template_renderer @@ -11,6 +12,14 @@ db = Database("ext_satspay") satspay_ext: APIRouter = APIRouter(prefix="/satspay", tags=["satspay"]) +satspay_static_files = [ + { + "path": "/satspay/static", + "app": StaticFiles(directory="lnbits/extensions/satspay/static"), + "name": "satspay_static", + } +] + def satspay_renderer(): return template_renderer(["lnbits/extensions/satspay/templates"]) diff --git a/lnbits/extensions/satspay/static/js/utils.js b/lnbits/extensions/satspay/static/js/utils.js new file mode 100644 index 000000000..a2aaa0f0d --- /dev/null +++ b/lnbits/extensions/satspay/static/js/utils.js @@ -0,0 +1,31 @@ +const sleep = ms => new Promise(r => setTimeout(r, ms)) +const retryWithDelay = async function (fn, retryCount = 0) { + try { + await sleep(25) + // Do not return the call directly, use result. + // Otherwise the error will not be cought in this try-catch block. + const result = await fn() + return result + } catch (err) { + if (retryCount > 100) throw err + await sleep((retryCount + 1) * 1000) + return retryWithDelay(fn, retryCount + 1) + } +} + +const mapCharge = obj => { + obj._data = _.clone(obj) + obj.theTime = obj.time * 60 - (Date.now() / 1000 - obj.timestamp) + obj.time = obj.time + 'mins' + + if (obj.time_elapsed) { + obj.date = 'Time elapsed' + } else { + obj.date = Quasar.utils.date.formatDate( + new Date((obj.theTime - 3600) * 1000), + 'HH:mm:ss' + ) + } + obj.displayUrl = ['/satspay/', obj.id].join('') + return obj +}