Skip to content

Commit 81cbf78

Browse files
committed
Replace index page with gomponents
1 parent 90e5cfd commit 81cbf78

5 files changed

Lines changed: 184 additions & 106 deletions

File tree

backend/web/index.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import (
88
"golang.org/x/sync/errgroup"
99

1010
"github.com/theandrew168/bloggulus/backend/finder"
11-
"github.com/theandrew168/bloggulus/backend/web/page"
11+
"github.com/theandrew168/bloggulus/backend/web/ui"
1212
"github.com/theandrew168/bloggulus/backend/web/util"
1313
)
1414

1515
// TODO: Rename p / s to page / size.
1616
func HandleIndexPage(find *finder.Finder) http.Handler {
17-
tmpl := page.NewIndex()
1817
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1918
account, isLoggedIn := util.GetContextAccount(r)
2019

@@ -98,16 +97,17 @@ func HandleIndexPage(find *finder.Finder) http.Handler {
9897
return
9998
}
10099

101-
data := page.IndexData{
102-
BaseData: util.TemplateBaseData(r, w),
100+
page := ui.Index(ui.IndexData{
101+
LayoutData: util.GetLayoutData(r, w),
103102

104103
Search: search,
105104
Articles: articles,
106105
HasMorePages: p*s < count,
107106
NextPage: p + 1,
108-
}
107+
})
108+
109109
util.Render(w, r, 200, func(w io.Writer) error {
110-
return tmpl.Render(w, data)
110+
return page.Render(w)
111111
})
112112
})
113113
}

backend/web/page/index.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

backend/web/page/index.html

Lines changed: 0 additions & 57 deletions
This file was deleted.

backend/web/ui/index.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package ui
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/theandrew168/bloggulus/backend/finder"
7+
8+
g "maragu.dev/gomponents"
9+
h "maragu.dev/gomponents/html"
10+
)
11+
12+
type IndexData struct {
13+
LayoutData
14+
15+
Search string
16+
Articles []finder.Article
17+
HasMorePages bool
18+
NextPage int
19+
}
20+
21+
func Index(data IndexData) g.Node {
22+
return Layout(data.LayoutData,
23+
// Header (search)
24+
h.Header(
25+
h.Class("articles-header"),
26+
g.If(data.Search != "", h.H1(
27+
h.Class("articles-header__title"),
28+
g.Text("Relevant Articles"),
29+
)),
30+
g.If(data.Search == "", h.H1(
31+
h.Class("articles-header__title"),
32+
g.Text("Recent Articles"),
33+
)),
34+
// TODO: Replace this with <search> once gomponents supports it.
35+
h.Div(
36+
h.Form(
37+
h.Method("GET"),
38+
h.Action("/"),
39+
h.Input(
40+
h.Class("input"),
41+
h.Type("text"),
42+
h.Name("q"),
43+
h.Value(data.Search),
44+
h.Placeholder("Search"),
45+
),
46+
),
47+
),
48+
),
49+
50+
// Articles
51+
h.Section(
52+
h.Class("articles"),
53+
g.Map(data.Articles, func(article finder.Article) g.Node {
54+
return h.Article(
55+
h.Class("article"),
56+
h.Header(
57+
h.Class("article__header"),
58+
h.Span(
59+
h.Class("article__date"),
60+
g.Text(article.PublishedAt.Format("Jan 2, 2006")),
61+
),
62+
h.Ul(
63+
h.Class("article__tags"),
64+
g.Map(article.Tags, func(tag string) g.Node {
65+
return h.Li(
66+
h.A(
67+
h.Class("article__tag"),
68+
h.Href(fmt.Sprintf("/?q=%s", tag)),
69+
g.Text(tag),
70+
),
71+
)
72+
}),
73+
),
74+
),
75+
h.P(
76+
h.A(
77+
h.Class("article__title"),
78+
h.Href(article.URL),
79+
g.Text(article.Title),
80+
),
81+
),
82+
h.P(
83+
h.A(
84+
h.Class("article__blog-title"),
85+
h.Href(article.BlogURL),
86+
g.Text(article.BlogTitle),
87+
),
88+
),
89+
)
90+
}),
91+
g.If(len(data.Articles) == 0,
92+
g.Group{
93+
g.If(data.Search != "",
94+
h.Article(
95+
h.Class("articles-cta"),
96+
h.P(
97+
g.Text("No relevant articles! Try searching for something else."),
98+
),
99+
),
100+
),
101+
g.If(data.Search == "",
102+
h.Article(
103+
h.Class("articles-cta"),
104+
h.P(
105+
g.Text("No posts found! Get started by following your favorite blogs."),
106+
),
107+
h.P(
108+
h.A(
109+
h.Class("button"),
110+
h.Href("/blogs"),
111+
g.Text("Follow Blogs"),
112+
),
113+
),
114+
),
115+
),
116+
},
117+
),
118+
),
119+
120+
// Footer (pagination)
121+
g.If(data.HasMorePages,
122+
h.Footer(
123+
h.Class("articles-footer"),
124+
g.If(data.Search == "", h.A(
125+
h.Class("button button--outline"),
126+
h.Href(fmt.Sprintf("/?p=%d&q=%s", data.NextPage, data.Search)),
127+
g.Text("See More"),
128+
)),
129+
g.If(data.Search != "", h.A(
130+
h.Class("button button--outline"),
131+
h.Href(fmt.Sprintf("/?p=%d", data.NextPage)),
132+
g.Text("See More"),
133+
)),
134+
),
135+
),
136+
)
137+
}

backend/web/util/ui.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package util
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/justinas/nosurf"
7+
8+
"github.com/theandrew168/bloggulus/backend/web/ui"
9+
)
10+
11+
func GetLayoutData(r *http.Request, w http.ResponseWriter) ui.LayoutData {
12+
data := ui.LayoutData{}
13+
14+
account, isLoggedIn := GetContextAccount(r)
15+
if isLoggedIn {
16+
data.Account = account
17+
}
18+
19+
csrfToken := nosurf.Token(r)
20+
if csrfToken != "" {
21+
data.CSRFToken = csrfToken
22+
}
23+
24+
toastCookie, err := r.Cookie(ToastCookieName)
25+
if err == nil {
26+
data.Toast = toastCookie.Value
27+
28+
cookie := NewExpiredCookie(ToastCookieName)
29+
http.SetCookie(w, &cookie)
30+
}
31+
32+
conf, ok := GetContextConfig(r)
33+
if ok && conf.PlausibleDataDomain != "" {
34+
data.PlausibleDataDomain = conf.PlausibleDataDomain
35+
}
36+
if ok && conf.GoatCounterCode != "" {
37+
data.GoatCounterCode = conf.GoatCounterCode
38+
}
39+
40+
return data
41+
}

0 commit comments

Comments
 (0)