htmgo/news/views.go

45 lines
713 B
Go
Raw Normal View History

2024-01-22 15:22:16 +00:00
package news
import (
"fmt"
2024-09-11 00:52:18 +00:00
"mhtml/database"
2024-01-22 15:22:16 +00:00
"mhtml/h"
2024-09-11 17:31:40 +00:00
"time"
2024-01-22 15:22:16 +00:00
)
func StoryList() *h.Node {
2024-09-11 00:52:18 +00:00
posts, _ := database.GetOrSet[[]Post]("posts", func() []Post {
p, _ := List()
return p
})
2024-01-22 15:22:16 +00:00
2024-09-11 17:31:40 +00:00
time.Sleep(200 * time.Millisecond)
2024-09-11 00:52:18 +00:00
if len(*posts) == 0 {
2024-01-22 15:22:16 +00:00
return h.P("No results found")
}
return h.Fragment(
2024-09-11 17:31:40 +00:00
h.Div(h.List(*posts, func(item Post) *h.Node {
2024-01-22 15:22:16 +00:00
return StoryCard(item)
})),
)
}
func StoryCard(post Post) *h.Node {
url := fmt.Sprintf("/news/%d", post.Id)
2024-09-11 17:31:40 +00:00
return h.Div(
2024-09-11 00:52:18 +00:00
h.Class("items-center bg-indigo-200 p-4 rounded"),
2024-01-22 15:22:16 +00:00
h.A(post.Title, h.Href(url)),
)
}
func StoryFull(id string) *h.Node {
post, err := Get(id)
if err != nil {
return h.P(err.Error())
}
return StoryCard(post)
}