Improvement to adding posts

This commit is contained in:
Dominic Ferrando
2025-04-18 13:22:28 -04:00
parent 00d1b8c948
commit ac9c516550
2 changed files with 53 additions and 18 deletions
+31 -3
View File
@@ -97,7 +97,7 @@ func main() {
posts, err := database.GetPosts(db, threadId) posts, err := database.GetPosts(db, threadId)
if err != nil { if err != nil {
http.Error(w, "Failed to get thread posts", http.StatusInternalServerError) http.Error(w, "Failed to get posts", http.StatusInternalServerError)
log.Printf("Failed to get thread %q posts: %v", threadIdStr, err) log.Printf("Failed to get thread %q posts: %v", threadIdStr, err)
return return
} }
@@ -168,7 +168,7 @@ func main() {
slug := chi.URLParam(r, "slug") slug := chi.URLParam(r, "slug")
threads, err := database.GetThreads(db, slug) threads, err := database.GetThreads(db, slug)
if err != nil { if err != nil {
http.Error(w, "Failed to get board threads", http.StatusInternalServerError) http.Error(w, "Failed to get threads", http.StatusInternalServerError)
log.Printf("GetThreads: %v", err) log.Printf("GetThreads: %v", err)
return return
} }
@@ -177,7 +177,7 @@ func main() {
for _, thread := range threads { for _, thread := range threads {
posts, err := database.GetPosts(db, thread.Id) posts, err := database.GetPosts(db, thread.Id)
if err != nil { if err != nil {
http.Error(w, "Error getting thread posts", http.StatusBadRequest) http.Error(w, "Failed to get posts", http.StatusBadRequest)
log.Printf("GetPosts: %v", err) log.Printf("GetPosts: %v", err)
return return
} }
@@ -198,6 +198,34 @@ func main() {
views.ThreadsCatalog(previews).Render(r.Context(), w) views.ThreadsCatalog(previews).Render(r.Context(), w)
}) })
// THREAD POSTS
r.Get("/hx/{slug}/threads/{threadId}/posts", func(w http.ResponseWriter, r *http.Request) {
// slug := chi.URLParam(r, "slug")
// TODO: use relative thread_nums (see issue #1)
threadIdStr := chi.URLParam(r, "threadId")
threadId, err := strconv.Atoi(threadIdStr)
if err != nil {
http.Error(w, "Invalid thread id", http.StatusBadRequest)
return
}
posts, err := database.GetPosts(db, threadId)
if err != nil {
http.Error(w, "Failed to get posts", http.StatusBadRequest)
log.Printf("GetPosts: %v", err)
return
}
if len(posts) == 0 {
http.Error(w, "Malformed thread", http.StatusInternalServerError)
log.Printf("Thread %d has no posts", threadId)
return
}
// dont pass the op post. only replies
views.ThreadPosts(posts[1:]).Render(r.Context(), w)
})
// ----------------- // -----------------
fmt.Println("Listening on :8080") fmt.Println("Listening on :8080")
+22 -15
View File
@@ -7,16 +7,34 @@ import (
"strconv" "strconv"
) )
templ ThreadPosts(posts []database.Post) {
for _, post := range (posts) {
<article class="post">
<header class="post-header">
<span class="post-author">{ post.Author }</span>
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
<span class="post-num">No. { strconv.Itoa(post.Id) }</span>
</header>
<section>
<p>{ post.Body }</p>
</section>
</article>
}
}
templ Thread(board database.Board, thread database.Thread, posts []database.Post) { templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
@shared.Layout() { @shared.Layout() {
@BoardHeader(board) @BoardHeader(board)
<div class="new-post-container"> <div class="new-post-container">
<form id="newPostForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id) }> <form id="newPostForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id) } _="on htmx:afterRequest
set #newPostBody.value to ''
then trigger refreshPosts on body">
<table> <table>
<tbody> <tbody>
<tr class="new-post-form-field"> <tr class="new-post-form-field">
<th>Comment</th> <th>Comment</th>
<td><textarea name="body" required></textarea></td> <td><textarea id="newPostBody" name="body" required></textarea></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@@ -36,19 +54,8 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
<p>{ posts[0].Body }</p> <p>{ posts[0].Body }</p>
</section> </section>
</article> </article>
for _, post := range (posts[1:]) { <div hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", board.Slug, thread.Id) }
<article class="post"> hx-trigger="load, refreshPosts from:body"></div>
<header class="post-header">
<span class="post-author">{ post.Author }</span>
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
<span class="post-num">No. { strconv.Itoa(post.Id) }</span>
</header>
<section>
<p>{ post.Body }</p>
</section>
</article>
}
</div> </div>
} }
} }