aboutsummaryrefslogtreecommitdiff
path: root/pages/index.tsx
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-03-16 11:21:46 +0100
committerlonkaars <l.leblansch@gmail.com>2021-03-16 11:21:46 +0100
commit9abc6a25c89251659c5ffe2f59f6379cbf9f8e04 (patch)
treecbea5cb90c3b0cd4c88428a4f1a3dc063f367ae3 /pages/index.tsx
parentb2e0569eb74cddc92c1e8576755137aa64f95d86 (diff)
prevent repeat api requests
Diffstat (limited to 'pages/index.tsx')
-rw-r--r--pages/index.tsx54
1 files changed, 24 insertions, 30 deletions
diff --git a/pages/index.tsx b/pages/index.tsx
index 41da36e..71a83d3 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -62,7 +62,9 @@ function LoginOrRegisterBox() {
position: "absolute",
fontSize: 14,
left: 0, right: 0, top: 0,
- margin: "0 100px"
+ margin: "0 auto",
+ minWidth: 240,
+ maxWidth: 350
}}>Log in of maak een account aan om je scores op te slaan en toegang te krijgen tot meer functies</span>
<div style={{
display: "grid",
@@ -115,39 +117,31 @@ function AccountBox(props: {
}
export default function HomePage() {
- var [gotData, setGotData] = useState(false);
- var [loggedIn, setLoggedIn] = useState(false);
+ var server = typeof window === "undefined";
+ var loggedIn = !server && document.cookie.includes("token");
+
var [userInfo, setUserInfo] = useState<userInfo>();
var [gameInfo, setGameInfo] = useState<userGames>();
useEffect(() => {( async () => {
- if (gotData) return;
-
- if (typeof window === "undefined") return; // return if run on server
- setLoggedIn(document.cookie.includes("token"));
-
- if (!loggedIn) return; // don't request user info if not logged in
-
- if (!userInfo) {
- var userInfoReq = await axios.request<userInfo>({
- method: "get",
- url: `/api/user/info`,
- headers: {"content-type": "application/json"}
- })
- setUserInfo(userInfoReq.data);
- }
-
- if (!gameInfo) {
- var userGamesReq = await axios.request<userGames>({
- method: "get",
- url: `/api/user/games`,
- headers: {"content-type": "application/json"}
- })
- setGameInfo(userGamesReq.data);
- }
-
- setGotData(true);
- })()})
+ if (!loggedIn) return;
+ var userInfoReq = await axios.request<userInfo>({
+ method: "get",
+ url: `/api/user/info`,
+ headers: {"content-type": "application/json"}
+ });
+ setUserInfo(userInfoReq.data);
+ })()}, []);
+
+ useEffect(() => {( async () => {
+ if (!loggedIn) return;
+ var userGamesReq = await axios.request<userGames>({
+ method: "get",
+ url: `/api/user/games`,
+ headers: {"content-type": "application/json"}
+ });
+ setGameInfo(userGamesReq.data);
+ })()}, []);
return <div>
<NavBar/>