diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-16 11:21:46 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-16 11:21:46 +0100 |
commit | 9abc6a25c89251659c5ffe2f59f6379cbf9f8e04 (patch) | |
tree | cbea5cb90c3b0cd4c88428a4f1a3dc063f367ae3 /components/preferencesContext.tsx | |
parent | b2e0569eb74cddc92c1e8576755137aa64f95d86 (diff) |
prevent repeat api requests
Diffstat (limited to 'components/preferencesContext.tsx')
-rw-r--r-- | components/preferencesContext.tsx | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/components/preferencesContext.tsx b/components/preferencesContext.tsx index 6b7914f..1b58a4f 100644 --- a/components/preferencesContext.tsx +++ b/components/preferencesContext.tsx @@ -12,13 +12,13 @@ function applyPreferences(preferences: userPreferences) { var PreferencesContext = createContext<{ preferences?: userPreferences; updatePreference?: (newPreference: userPreferences) => 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<userPreferences>(); 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]); |