aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-03-12 09:19:43 +0100
committerlonkaars <l.leblansch@gmail.com>2021-03-12 09:19:43 +0100
commit6a0411142a381389adc0d7151a76fbbb33a81fa2 (patch)
tree3621b7ac8404c9514eca54940b313366752ea5d3
parent599108b21979039117ffe1de1817e71644a20fb4 (diff)
no search results message + try to prevent repeat api requests
-rw-r--r--components/preferencesContext.tsx16
-rw-r--r--pages/index.tsx31
-rw-r--r--pages/search.tsx8
-rw-r--r--pages/user.tsx37
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<userInfo>({
- method: "get",
- url: `/api/user/info`,
- headers: {"content-type": "application/json"}
- })
- setUserInfo(userInfoReq.data);
-
- var userGamesReq = await axios.request<userGames>({
- method: "get",
- url: `/api/user/games`,
- headers: {"content-type": "application/json"}
- })
- setGameInfo(userGamesReq.data);
+
+ 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);
})()})
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<Array<userInfo>>([]);
var getSearchResults = (event?: FormEvent<HTMLFormElement>) => {
event.preventDefault();
search(results => setResults(results));
+ setSearched(true);
}
return <div>
@@ -86,6 +88,12 @@ export default function HomePage() {
<PageTitle>Zoeken</PageTitle>
<SearchBar searchFunction={getSearchResults}/>
<SearchResults userList={results}/>
+ { searched && results.length == 0 && <h1 style={{
+ opacity: .6,
+ color: "var(--text)",
+ textAlign: "center",
+ margin: "24px 32px"
+ }}>Geen zoekresultaten gevonden</h1> }
</CenteredPage>
</div>
}
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<userInfo>({
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<userInfo>({
- method: "post",
- url: `/api/user/info`,
- headers: {"content-type": "application/json"},
- data: { "id": id || self_id }
- });
-
- setUser(userReq.data);
-
- var userGamesReq = await axios.request<userGames>({
- method: "post",
- url: `/api/user/games`,
- headers: {"content-type": "application/json"},
- data: { "id": id || self_id }
- });
+ if (!user) {
+ var userReq = await axios.request<userInfo>({
+ 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<userGames>({
+ method: "post",
+ url: `/api/user/games`,
+ headers: {"content-type": "application/json"},
+ data: { "id": id || self_id }
+ });
+ setGameInfo(userGamesReq.data);
+ }
} else {
window.history.go(-1);
}