From 9abc6a25c89251659c5ffe2f59f6379cbf9f8e04 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 16 Mar 2021 11:21:46 +0100 Subject: prevent repeat api requests --- components/account.tsx | 2 -- components/navbar.tsx | 2 +- components/preferencesContext.tsx | 12 +++++------- 3 files changed, 6 insertions(+), 10 deletions(-) (limited to 'components') diff --git a/components/account.tsx b/components/account.tsx index 005e0d1..7b825cd 100644 --- a/components/account.tsx +++ b/components/account.tsx @@ -1,5 +1,3 @@ -import { useState, useEffect } from 'react'; - var dummy = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQYV2P4z/j/PwAHAQL/gXZXNQAAAABJRU5ErkJggg=="; export function AccountAvatar(props: { diff --git a/components/navbar.tsx b/components/navbar.tsx index 2c38c3c..252635d 100644 --- a/components/navbar.tsx +++ b/components/navbar.tsx @@ -46,7 +46,7 @@ export function NavBar() { } setGotData(true); - })()}); + })()}, []); return
void }>({}); export function PreferencesContextWrapper(props: { children?: ReactNode }) { - var [gotData, setGotData] = useState(false); + var server = typeof window === "undefined"; + var loggedIn = !server && document.cookie.includes("token"); + var [preferences, setPreferences] = useState(); useEffect(() => {(async() => { - if (gotData) return; - if (typeof window === "undefined") return; - if (!document.cookie.includes("token")) return; + if (!loggedIn) return; var local_prefs = window.localStorage.getItem("preferences"); if (local_prefs) { @@ -37,9 +37,7 @@ export function PreferencesContextWrapper(props: { children?: ReactNode }) { window.localStorage.setItem("preferences", JSON.stringify(preferencesReq.data.preferences)); setPreferences(preferencesReq.data.preferences); } - - setGotData(true); - })()}); + })()}, []); useEffect(() => applyPreferences(preferences), [preferences]); -- cgit v1.2.3