From d5a96181d2183e4781f4ad012a9c7686d574e822 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Sun, 23 Jul 2023 22:12:01 -0400 Subject: [PATCH] remove excess community api calls --- public/utils.js | 20 +++++++++++++------- state.go | 29 ++++++++++++++++++----------- templates/frontpage.html | 2 +- templates/main.html | 2 +- templates/nav.html | 10 +++++++++- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/public/utils.js b/public/utils.js index 04c8a92..1ff1c50 100644 --- a/public/utils.js +++ b/public/utils.js @@ -245,19 +245,25 @@ function toggleMyCommunities(e) { return false } mycommunities.className = "open" - mycommunities.innerHTML = "
loading
" - request(e.target.href + "&xhr=1", "", function(res) { - mycommunities.innerHTML = '
view all »' - mycommunities.innerHTML += res - }, function() { - mycommunities.className = "" - }) + if (mycommunities.innerHTML == "") { + mycommunities.innerHTML = "
loading
" + request(e.target.href + "&xhr=1", "", function(res) { + mycommunities.innerHTML = '
view all »' + mycommunities.innerHTML += res + }, function() { + mycommunities.className = "" + }) + } return false } function openSettings(e) { e.preventDefault() var settings = document.getElementById("settingspopup") + if (settings.className == "open") { + settings.className = "" + return false + } settings.className = "open" request(e.target.href + "?xhr=1", "", function(res) { settings.innerHTML = res diff --git a/state.go b/state.go index 0b8a6bc..474d893 100644 --- a/state.go +++ b/state.go @@ -59,8 +59,9 @@ type Post struct { } type Session struct { - UserName string - UserID int + UserName string + UserID int + Communities []types.CommunityView } type State struct { @@ -267,6 +268,18 @@ func (state *State) GetSite() { return } state.Site = resp + if !state.Site.MyUser.IsValid() { + return + } + for _, c := range state.Site.MyUser.MustValue().Follows { + state.Session.Communities = append(state.Session.Communities, types.CommunityView{ + Community: c.Community, + Subscribed: "Subscribed", + }) + } + sort.Slice(state.Session.Communities, func(a, b int) bool { + return state.Session.Communities[a].Community.Name < state.Session.Communities[b].Community.Name + }) } func (state *State) GetComment(commentid int) { @@ -532,16 +545,10 @@ func (state *State) Search(searchtype string) { if state.Page > 1 { return } - state.GetSite() - for _, c := range state.Site.MyUser.MustValue().Follows { - state.Communities = append(state.Communities, types.CommunityView{ - Community: c.Community, - Subscribed: "Subscribed", - }) + if state.Site == nil { + state.GetSite() } - sort.Slice(state.Communities, func(a, b int) bool { - return state.Communities[a].Community.Name < state.Communities[b].Community.Name - }) + state.Communities = state.Session.Communities return } resp, err := state.Client.Communities(context.Background(), types.ListCommunities{ diff --git a/templates/frontpage.html b/templates/frontpage.html index 14f746f..85a4af5 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -59,6 +59,6 @@ {{ template "sidebar.html" . }} {{ end }} - + diff --git a/templates/main.html b/templates/main.html index a110926..893f703 100644 --- a/templates/main.html +++ b/templates/main.html @@ -140,7 +140,7 @@ {{ end }} {{ end }} - + {{ template "sidebar.html" . }} {{ end }} diff --git a/templates/nav.html b/templates/nav.html index d093b83..f455ace 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -1,3 +1,4 @@ +{{ $state := . }}