save post xhr. fixes #33

This commit is contained in:
Ryan Stafford 2023-07-12 11:23:13 -04:00
parent ca47acec84
commit 7b423eebb7
6 changed files with 21 additions and 8 deletions

View file

@ -315,9 +315,9 @@ function setup() {
var posts = document.getElementsByClassName("post") var posts = document.getElementsByClassName("post")
for (var i = 0; i < posts.length; i++) { for (var i = 0; i < posts.length; i++) {
posts[i].addEventListener("click", postClick) posts[i].addEventListener("click", postClick)
var voteForm = posts[i].getElementsByClassName("link-btn") var forms = posts[i].getElementsByClassName("link-btn")
if (voteForm.length) { for (var f = 0; f < forms.length; f++) {
voteForm[0].addEventListener("submit", formSubmit) forms[f].addEventListener("submit", formSubmit)
} }
var url = posts[i].getElementsByClassName("url")[0].href var url = posts[i].getElementsByClassName("url")[0].href
if (id = parseYoutube(url)) { if (id = parseYoutube(url)) {

View file

@ -924,6 +924,14 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if err != nil { if err != nil {
fmt.Println(err) 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": case "save_comment":
commentid, _ := strconv.Atoi(r.FormValue("commentid")) commentid, _ := strconv.Atoi(r.FormValue("commentid"))
_, err := state.Client.SaveComment(context.Background(), types.SaveComment{ _, err := state.Client.SaveComment(context.Background(), types.SaveComment{

View file

@ -56,6 +56,6 @@
{{ template "sidebar.html" . }} {{ template "sidebar.html" . }}
</main> </main>
{{ end }} {{ end }}
<script src="/_/static/utils.js?v=15"></script> <script src="/_/static/utils.js?v=17"></script>
</body> </body>
</html> </html>

View file

@ -133,7 +133,7 @@
{{ end }} {{ end }}
{{ end }} {{ end }}
<script src="/_/static/utils.js?v=16"></script> <script src="/_/static/utils.js?v=17"></script>
{{ template "sidebar.html" . }} {{ template "sidebar.html" . }}
</main> </main>
{{ end }} {{ end }}

View file

@ -1,10 +1,11 @@
{{ if ne .State.Op "vote_post" }} {{ if and (ne .State.Op "vote_post") (ne .State.Op "save_post") }}
<div class="post{{if .Post.Deleted}} deleted{{end}}{{ if or .Post.FeaturedCommunity .Post.FeaturedLocal }} distinguished{{end}}"> <div class="post{{if .Post.Deleted}} deleted{{end}}{{ if or .Post.FeaturedCommunity .Post.FeaturedLocal }} distinguished{{end}}">
{{ if gt .Rank 0 }} {{ if gt .Rank 0 }}
<div class="rank"> {{ .Rank }} </div> <div class="rank"> {{ .Rank }} </div>
{{ end }} {{ end }}
<div class="score"> <div class="score">
{{ end }} {{ end }}
{{ if or (ne .State.Op "save_post") (eq .State.Op "vote_post") }}
{{ if .State.Session }} {{ if .State.Session }}
<form class="link-btn {{ if lt .Rank 1 }}squish{{end}}{{ if eq .MyVote.String "1" }} like{{else if eq .MyVote.String "-1"}} dislike{{end}}" method="POST"> <form class="link-btn {{ if lt .Rank 1 }}squish{{end}}{{ if eq .MyVote.String "1" }} like{{else if eq .MyVote.String "-1"}} dislike{{end}}" method="POST">
<input type="submit" name="vote" value="▲"> <input type="submit" name="vote" value="▲">
@ -19,7 +20,8 @@
{{ else }} {{ else }}
<div style="margin-top: 19px;">{{ .Counts.Score }}</div> <div style="margin-top: 19px;">{{ .Counts.Score }}</div>
{{ end }} {{ end }}
{{ if ne .State.Op "vote_post" }} {{ end }}
{{ if and (ne .State.Op "vote_post") (ne .State.Op "save_post") }}
</div> </div>
<div class="thumb"> <div class="thumb">
<a class="url" href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}"> <a class="url" href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}">
@ -59,6 +61,8 @@
{{ end }} {{ end }}
</form> </form>
{{ end}} {{ end}}
{{ end }}
{{ if or (ne .State.Op "vote_post") (eq .State.Op "save_post") }}
{{ if .State.Session }} {{ if .State.Session }}
<form class="link-btn" method="POST"> <form class="link-btn" method="POST">
<input type="hidden" name="postid" value="{{.Post.ID }}"> <input type="hidden" name="postid" value="{{.Post.ID }}">
@ -70,6 +74,8 @@
{{ end }} {{ end }}
</form> </form>
{{end}} {{end}}
{{ end }}
{{ if and (ne .State.Op "vote_post") (ne .State.Op "save_post") }}
{{ if .State.PostID }} {{ if .State.PostID }}
<a id="hidechildren" class="scripting" href="">hide all child comments</a> <a id="hidechildren" class="scripting" href="">hide all child comments</a>
{{ end }} {{ end }}

View file

@ -6,7 +6,6 @@
{{ template "activities.html" . }} {{ template "activities.html" . }}
{{ else }} {{ else }}
{{ range $post := .Posts }} {{ range $post := .Posts }}
{{ template "post.html" $post }} {{ template "post.html" $post }}
{{ end }} {{ end }}
{{ end }} {{ end }}