From 7b423eebb7e7d821fea4aa9b25c75c6155c4eae1 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Wed, 12 Jul 2023 11:23:13 -0400 Subject: [PATCH] save post xhr. fixes #33 --- public/utils.js | 6 +++--- routes.go | 8 ++++++++ templates/frontpage.html | 2 +- templates/main.html | 2 +- templates/post.html | 10 ++++++++-- templates/xhr.html | 1 - 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/public/utils.js b/public/utils.js index d15f1fb..afd224d 100644 --- a/public/utils.js +++ b/public/utils.js @@ -315,9 +315,9 @@ function setup() { var posts = document.getElementsByClassName("post") for (var i = 0; i < posts.length; i++) { posts[i].addEventListener("click", postClick) - var voteForm = posts[i].getElementsByClassName("link-btn") - if (voteForm.length) { - voteForm[0].addEventListener("submit", formSubmit) + var forms = posts[i].getElementsByClassName("link-btn") + for (var f = 0; f < forms.length; f++) { + forms[f].addEventListener("submit", formSubmit) } var url = posts[i].getElementsByClassName("url")[0].href if (id = parseYoutube(url)) { diff --git a/routes.go b/routes.go index 630291a..96cef97 100644 --- a/routes.go +++ b/routes.go @@ -924,6 +924,14 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if err != nil { fmt.Println(err) } + if r.FormValue("xhr") != "" { + state.GetPost(postid) + state.PostID = 0 + state.Op = "save_post" + state.XHR = true + Render(w, "index.html", state) + return + } case "save_comment": commentid, _ := strconv.Atoi(r.FormValue("commentid")) _, err := state.Client.SaveComment(context.Background(), types.SaveComment{ diff --git a/templates/frontpage.html b/templates/frontpage.html index 95b1e1f..d27dc5b 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -56,6 +56,6 @@ {{ template "sidebar.html" . }} {{ end }} - + diff --git a/templates/main.html b/templates/main.html index 0697372..7bae19d 100644 --- a/templates/main.html +++ b/templates/main.html @@ -133,7 +133,7 @@ {{ end }} {{ end }} - + {{ template "sidebar.html" . }} {{ end }} diff --git a/templates/post.html b/templates/post.html index e0d9fcf..e61d304 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,10 +1,11 @@ -{{ if ne .State.Op "vote_post" }} +{{ if and (ne .State.Op "vote_post") (ne .State.Op "save_post") }}
{{ if gt .Rank 0 }}
{{ .Rank }}
{{ end }}
{{ end }} +{{ if or (ne .State.Op "save_post") (eq .State.Op "vote_post") }} {{ if .State.Session }}
@@ -59,6 +61,8 @@ {{ end }} {{ end}} +{{ end }} +{{ if or (ne .State.Op "vote_post") (eq .State.Op "save_post") }} {{ if .State.Session }} {{end}} +{{ end }} +{{ if and (ne .State.Op "vote_post") (ne .State.Op "save_post") }} {{ if .State.PostID }} hide all child comments {{ end }} diff --git a/templates/xhr.html b/templates/xhr.html index 6862039..15e1d1f 100644 --- a/templates/xhr.html +++ b/templates/xhr.html @@ -6,7 +6,6 @@ {{ template "activities.html" . }} {{ else }} {{ range $post := .Posts }} - {{ template "post.html" $post }} {{ end }} {{ end }}