Skip to content

Commit

Permalink
save post xhr. fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
rystaf committed Jul 12, 2023
1 parent ca47ace commit 7b423ee
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions public/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion templates/frontpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
{{ template "sidebar.html" . }}
</main>
{{ end }}
<script src="/_/static/utils.js?v=15"></script>
<script src="/_/static/utils.js?v=17"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
{{ end }}
{{ end }}

<script src="/_/static/utils.js?v=16"></script>
<script src="/_/static/utils.js?v=17"></script>
{{ template "sidebar.html" . }}
</main>
{{ end }}
Expand Down
10 changes: 8 additions & 2 deletions templates/post.html
Original file line number Diff line number Diff line change
@@ -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}}">
{{ if gt .Rank 0 }}
<div class="rank"> {{ .Rank }} </div>
{{ end }}
<div class="score">
{{ end }}
{{ if or (ne .State.Op "save_post") (eq .State.Op "vote_post") }}
{{ 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">
<input type="submit" name="vote" value="">
Expand All @@ -19,7 +20,8 @@
{{ else }}
<div style="margin-top: 19px;">{{ .Counts.Score }}</div>
{{ end }}
{{ if ne .State.Op "vote_post" }}
{{ end }}
{{ if and (ne .State.Op "vote_post") (ne .State.Op "save_post") }}
</div>
<div class="thumb">
<a class="url" href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}">
Expand Down Expand Up @@ -59,6 +61,8 @@
{{ end }}
</form>
{{ end}}
{{ end }}
{{ if or (ne .State.Op "vote_post") (eq .State.Op "save_post") }}
{{ if .State.Session }}
<form class="link-btn" method="POST">
<input type="hidden" name="postid" value="{{.Post.ID }}">
Expand All @@ -70,6 +74,8 @@
{{ end }}
</form>
{{end}}
{{ end }}
{{ if and (ne .State.Op "vote_post") (ne .State.Op "save_post") }}
{{ if .State.PostID }}
<a id="hidechildren" class="scripting" href="">hide all child comments</a>
{{ end }}
Expand Down
1 change: 0 additions & 1 deletion templates/xhr.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
{{ template "activities.html" . }}
{{ else }}
{{ range $post := .Posts }}

{{ template "post.html" $post }}
{{ end }}
{{ end }}

0 comments on commit 7b423ee

Please sign in to comment.