Improvement to adding posts
This commit is contained in:
+31
-3
@@ -97,7 +97,7 @@ func main() {
|
||||
|
||||
posts, err := database.GetPosts(db, threadId)
|
||||
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)
|
||||
return
|
||||
}
|
||||
@@ -168,7 +168,7 @@ func main() {
|
||||
slug := chi.URLParam(r, "slug")
|
||||
threads, err := database.GetThreads(db, slug)
|
||||
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)
|
||||
return
|
||||
}
|
||||
@@ -177,7 +177,7 @@ func main() {
|
||||
for _, thread := range threads {
|
||||
posts, err := database.GetPosts(db, thread.Id)
|
||||
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)
|
||||
return
|
||||
}
|
||||
@@ -198,6 +198,34 @@ func main() {
|
||||
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")
|
||||
|
||||
+22
-15
@@ -7,16 +7,34 @@ import (
|
||||
"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) {
|
||||
@shared.Layout() {
|
||||
@BoardHeader(board)
|
||||
<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>
|
||||
<tbody>
|
||||
<tr class="new-post-form-field">
|
||||
<th>Comment</th>
|
||||
<td><textarea name="body" required></textarea></td>
|
||||
<td><textarea id="newPostBody" name="body" required></textarea></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -36,19 +54,8 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
|
||||
<p>{ posts[0].Body }</p>
|
||||
</section>
|
||||
</article>
|
||||
for _, post := range (posts[1:]) {
|
||||
<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>
|
||||
}
|
||||
<div hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", board.Slug, thread.Id) }
|
||||
hx-trigger="load, refreshPosts from:body"></div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user