From 83154d7e6d84ee3818a1abb3195bca366dcd3d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20Sj=C3=B6berg?= Date: Sat, 1 May 2021 19:06:02 +0200 Subject: [PATCH] Autorefresh --- config/config.ts_TEMPLATE | 10 ++++++++++ frontend/app.tsx | 2 ++ frontend/state/index.ts | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/config/config.ts_TEMPLATE b/config/config.ts_TEMPLATE index 38939be..e83fcc9 100644 --- a/config/config.ts_TEMPLATE +++ b/config/config.ts_TEMPLATE @@ -33,6 +33,12 @@ interface Config { threshold: number; }; + // Configuration specifically for the frontend site + frontend: { + // How often to autrefresh, in seconds. Set to null to disable + autoRefreshInterval: number | null; + }; + // Donation configuration, right now only supports lnd donation?: { // Backend type, currently only supports lnd @@ -74,6 +80,10 @@ const config: Config = { threshold: 1845, }, + frontend: { + autoRefreshInterval: 120, + }, + // donation: { // type: "lnd", // data: { diff --git a/frontend/app.tsx b/frontend/app.tsx index 540b409..8e6fc32 100644 --- a/frontend/app.tsx +++ b/frontend/app.tsx @@ -6,10 +6,12 @@ import store, { useStoreActions } from "./state/index.ts"; function StoreStarter({ Page, pageProps }: { Page: ComponentType; pageProps: any }) { const [gotBlocks, setGotBlocks] = useState(false); const getBlocks = useStoreActions((store) => store.getBlocks); + const autoRefresh = useStoreActions((store) => store.autoRefresh); useEffect(() => { (async () => { await getBlocks(); + autoRefresh(); setGotBlocks(true); })(); }, []); diff --git a/frontend/state/index.ts b/frontend/state/index.ts index 064c05c..cf46c98 100644 --- a/frontend/state/index.ts +++ b/frontend/state/index.ts @@ -6,6 +6,7 @@ import config from "../back/config/config.ts"; export interface IStoreModel { getBlocks: Thunk; setBlocks: Action; + autoRefresh: Thunk; blocks: IBlock[]; } @@ -50,6 +51,24 @@ export const model: IStoreModel = { } }), + autoRefresh: thunk((actions) => { + if (!config.frontend.autoRefreshInterval) { + return; + } + + setInterval(async () => { + try { + console.log("Fetching blocks"); + const result = await fetch("/blocks"); + const json = (await result.json()) as IBlock[]; + console.log(json); + actions.setBlocks(json); + } catch (error) { + console.log("Couldn't fetch /blocks", error.message); + } + }, config.frontend.autoRefreshInterval * 1000); + }), + setBlocks: action((state, payload) => { state.blocks = payload; }),