From 6a0411142a381389adc0d7151a76fbbb33a81fa2 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 12 Mar 2021 09:19:43 +0100 Subject: no search results message + try to prevent repeat api requests --- components/preferencesContext.tsx | 16 +++++++++------- pages/index.tsx | 31 ++++++++++++++++++------------- pages/search.tsx | 8 ++++++++ pages/user.tsx | 37 +++++++++++++++++++------------------ 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/components/preferencesContext.tsx b/components/preferencesContext.tsx index 3a930af..6b7914f 100644 --- a/components/preferencesContext.tsx +++ b/components/preferencesContext.tsx @@ -27,14 +27,16 @@ export function PreferencesContextWrapper(props: { children?: ReactNode }) { applyPreferences(local_prefs_json); } - var preferencesReq = await axios.request<{ preferences: userPreferences; }>({ - method: "get", - url: `/api/user/preferences`, - headers: {"content-type": "application/json"} - }); + if (!preferences) { + var preferencesReq = await axios.request<{ preferences: userPreferences; }>({ + method: "get", + url: `/api/user/preferences`, + headers: {"content-type": "application/json"} + }); - window.localStorage.setItem("preferences", JSON.stringify(preferencesReq.data.preferences)); - setPreferences(preferencesReq.data.preferences); + window.localStorage.setItem("preferences", JSON.stringify(preferencesReq.data.preferences)); + setPreferences(preferencesReq.data.preferences); + } setGotData(true); })()}); diff --git a/pages/index.tsx b/pages/index.tsx index 2aa8243..719f9ea 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -127,19 +127,24 @@ export default function HomePage() { setLoggedIn(document.cookie.includes("token")); if (!loggedIn) return; // don't request user info if not logged in - var userInfoReq = await axios.request({ - method: "get", - url: `/api/user/info`, - headers: {"content-type": "application/json"} - }) - setUserInfo(userInfoReq.data); - - var userGamesReq = await axios.request({ - method: "get", - url: `/api/user/games`, - headers: {"content-type": "application/json"} - }) - setGameInfo(userGamesReq.data); + + if (!userInfo) { + var userInfoReq = await axios.request({ + method: "get", + url: `/api/user/info`, + headers: {"content-type": "application/json"} + }) + setUserInfo(userInfoReq.data); + } + + if (!gameInfo) { + var userGamesReq = await axios.request({ + method: "get", + url: `/api/user/games`, + headers: {"content-type": "application/json"} + }) + setGameInfo(userGamesReq.data); + } setGotData(true); })()}) diff --git a/pages/search.tsx b/pages/search.tsx index 92de828..66e16d2 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -74,10 +74,12 @@ function SearchBar(props: { } export default function HomePage() { + var [searched, setSearched] = useState(false); var [results, setResults] = useState>([]); var getSearchResults = (event?: FormEvent) => { event.preventDefault(); search(results => setResults(results)); + setSearched(true); } return
@@ -86,6 +88,12 @@ export default function HomePage() { Zoeken + { searched && results.length == 0 &&

Geen zoekresultaten gevonden

}
} diff --git a/pages/user.tsx b/pages/user.tsx index 1b0e170..71177d3 100644 --- a/pages/user.tsx +++ b/pages/user.tsx @@ -84,35 +84,36 @@ export default function AccountPage() { if (id || loggedIn) { var self_id = ""; - if (loggedIn) { + if (loggedIn && !self_id) { var selfReq = await axios.request({ method: "get", url: `/api/user/info`, headers: {"content-type": "application/json"} }); - self_id = selfReq?.data.id; } if (id == self_id || !id) setOwnPage(true); - var userReq = await axios.request({ - method: "post", - url: `/api/user/info`, - headers: {"content-type": "application/json"}, - data: { "id": id || self_id } - }); - - setUser(userReq.data); - - var userGamesReq = await axios.request({ - method: "post", - url: `/api/user/games`, - headers: {"content-type": "application/json"}, - data: { "id": id || self_id } - }); + if (!user) { + var userReq = await axios.request({ + method: "post", + url: `/api/user/info`, + headers: {"content-type": "application/json"}, + data: { "id": id || self_id } + }); + setUser(userReq.data); + } - setGameInfo(userGamesReq.data); + if (!gameInfo) { + var userGamesReq = await axios.request({ + method: "post", + url: `/api/user/games`, + headers: {"content-type": "application/json"}, + data: { "id": id || self_id } + }); + setGameInfo(userGamesReq.data); + } } else { window.history.go(-1); } -- cgit v1.2.3