From 4eb00e38bce31a05ac80ab3f8aaa6719da6057fd Mon Sep 17 00:00:00 2001 From: DutchEllie Date: Fri, 25 Mar 2022 13:51:25 +0100 Subject: [PATCH] Blogposts are a thing now --- src/bloglinks.go | 70 +++++++++++++++++++++++++++++ src/blogpage.go | 32 +++++++++++-- src/main.go | 3 +- web/blocks/blogposts/testpost1.html | 6 +++ web/static/style.css | 9 ++++ 5 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 web/blocks/blogposts/testpost1.html diff --git a/src/bloglinks.go b/src/bloglinks.go index dc290db..bf3bca8 100644 --- a/src/bloglinks.go +++ b/src/bloglinks.go @@ -1,4 +1,74 @@ package main +import ( + "encoding/json" + "io" + "net/http" + + "dutchellie.nl/DutchEllie/proper-website-2/entity" + "github.com/maxence-charriere/go-app/v9/pkg/app" +) + type blogLinks struct { + app.Compo + + blogposts []entity.BlogPost +} + +func (b *blogLinks) OnMount(ctx app.Context) { + b.LoadPosts(ctx) +} + +func (b *blogLinks) Render() app.UI { + return newUIBlock(). + Class("left"). + Class("leftbarblock"). + Class("blogpost-bar"). + UI( + app.P(). + Class("p-h3"). + Style("margin-left", "10px"). + Style("margin-top", "10px"). + Style("text-decoration", "underline"). + Text("Posts"), + app.Range(b.blogposts).Slice(func(i int) app.UI { + return app.P(). + Class("blogpost-titles"). + Style("cursor", "pointer"). + Text(b.blogposts[i].Title). + OnClick(func(ctx app.Context, e app.Event) { + //e.PreventDefault() + + // Calling the update-blogpost action defined in the blogpage.go file. + // There it's updated + ctx.NewAction("update-blogpost", app.T("blogpost", b.blogposts[i].Path)) + }) + }), + ) +} + +func (b *blogLinks) LoadPosts(ctx app.Context) { + // TODO: maybe you can put this in a localbrowser storage? + url := ApiURL + "/blogpost" + ctx.Async(func() { + res, err := http.Get(url) + if err != nil { + app.Log(err) + return + } + defer res.Body.Close() + jsondata, err := io.ReadAll(res.Body) + if err != nil { + app.Log(err) + return + } + + ctx.Dispatch(func(ctx app.Context) { + err = json.Unmarshal(jsondata, &b.blogposts) + if err != nil { + app.Log(err) + return + } + }) + }) } diff --git a/src/blogpage.go b/src/blogpage.go index cc65a70..1a75212 100644 --- a/src/blogpage.go +++ b/src/blogpage.go @@ -1,9 +1,16 @@ package main -import "github.com/maxence-charriere/go-app/v9/pkg/app" +import ( + "fmt" + + "github.com/maxence-charriere/go-app/v9/pkg/app" +) type BlogPage struct { app.Compo + + display bool + currentPostPath string } // TODO: write the backend API for this @@ -18,11 +25,30 @@ func NewBlogPage() *BlogPage { return &BlogPage{} } +func (b *BlogPage) OnMount(ctx app.Context) { + ctx.Handle("update-blogpost", b.onUpdateBlogPost) + b.display = false +} + func (b *BlogPage) Render() app.UI { return newPage(). Title("Blog"). LeftBar( - newHTMLBlock(). - Class("left leftbarblock"), + &blogLinks{}, + ). + Main( + app.If(b.display, + newHTMLBlock(). + Class("right"). + Class("contentblock"). + Src(b.currentPostPath), + ), ) } + +func (b *BlogPage) onUpdateBlogPost(ctx app.Context, a app.Action) { + fmt.Printf("Called the update-blogpost ActionHandler\n") + blogpost := a.Tags.Get("blogpost") + b.currentPostPath = blogpost + b.display = true +} diff --git a/src/main.go b/src/main.go index fdd3f47..f6f60df 100644 --- a/src/main.go +++ b/src/main.go @@ -20,12 +20,11 @@ func main() { aboutpage := NewAboutPage() galaxiespage := NewGalaxiesPage() undertalePage := NewUndertalePage() - emptyPage := NewEmptyPage() app.Route("/", homepage) app.Route("/about", aboutpage) app.Route("/galaxies", galaxiespage) app.Route("/undertale", undertalePage) - app.Route("/empty", emptyPage) + app.Route("/blog", NewBlogPage()) app.Handle(getHTML, handleGetHTML) diff --git a/web/blocks/blogposts/testpost1.html b/web/blocks/blogposts/testpost1.html new file mode 100644 index 0000000..5532cf1 --- /dev/null +++ b/web/blocks/blogposts/testpost1.html @@ -0,0 +1,6 @@ +

Test blogpost

+

This is some text under the title

+

Header 2

+

Header 3

+

Regular text here

+

Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus, omnis nesciunt beatae dolorem laudantium quidem? Eos fugiat doloribus, ipsam, suscipit repudiandae culpa laboriosam accusamus alias atque esse rerum, tenetur illum.

\ No newline at end of file diff --git a/web/static/style.css b/web/static/style.css index 547d51d..76d221c 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -138,6 +138,15 @@ body { margin-bottom: 5px; } +.blogpost-bar { + +} + +.blogpost-titles { + margin-left: 10px; + text-decoration: none; + color: rgb(252, 230, 255); +} /* .content p {