From be182fd2221bb59ab80c5522e4573e9b311b644f Mon Sep 17 00:00:00 2001 From: darius Date: Tue, 25 Jun 2024 00:26:56 +0200 Subject: [PATCH] make projects editable ui added TODO add option to save changes --- api/apiRoutes.go | 1 + api/handlers/authHandler.go | 22 ++++++++++++++++++++ web/components/edit.go | 23 +++++++++++++++++++++ web/components/login.go | 5 ++++- web/components/projectList.go | 32 ++++++++++++++++++++++++++++++ web/handlers/projectPageHandler.go | 14 +++++++++++++ web/webRoutes.go | 1 + 7 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 web/components/edit.go diff --git a/api/apiRoutes.go b/api/apiRoutes.go index 561ca67..fbc78f7 100644 --- a/api/apiRoutes.go +++ b/api/apiRoutes.go @@ -20,6 +20,7 @@ func ApiRoutes() *http.ServeMux { //auth mux.HandleFunc("POST /login", handlers.Login) mux.HandleFunc("POST /register", handlers.CreateUserHandler) + mux.HandleFunc("GET /htmx/canEdit", handlers.CanEdit) //Project mux.HandleFunc("POST /project", handlers.CreateProjectHandler) diff --git a/api/handlers/authHandler.go b/api/handlers/authHandler.go index e4666cb..1cfc0c4 100644 --- a/api/handlers/authHandler.go +++ b/api/handlers/authHandler.go @@ -63,3 +63,25 @@ func Login(w http.ResponseWriter, r *http.Request) { return } } + +func CanEdit(w http.ResponseWriter, r *http.Request) { + jwtCookie, _ := r.Cookie("jwt") + + if jwtCookie != nil { + _, audience, err := jwt.VerifyJWT(jwtCookie.Value) + if err != nil { + InternalServerErrorHandler(w, err) + return + } + if audience == "owner" || audience == "visitor" { + w.WriteHeader(http.StatusOK) + w.Write([]byte("")) + } else { + w.WriteHeader(http.StatusOK) + w.Write([]byte("")) + } + return + } + w.WriteHeader(http.StatusOK) + w.Write([]byte("")) +} diff --git a/web/components/edit.go b/web/components/edit.go new file mode 100644 index 0000000..3078cb6 --- /dev/null +++ b/web/components/edit.go @@ -0,0 +1,23 @@ +package components + +import ( + g "github.com/maragudk/gomponents" + hx "github.com/maragudk/gomponents-htmx" + . "github.com/maragudk/gomponents/html" + b "github.com/willoma/bulma-gomponents" +) + +func Edit() g.Node { + return Div( + Class("px-3 py-2"), + hx.Post("/projects/edit"), + hx.Swap("outerHTML"), + hx.SelectOOB("true"), + hx.Target("#main"), + b.Button( + hx.Trigger("load"), + hx.Get(BaseUrl+"/htmx/canEdit"), + hx.Target("this"), + hx.Swap("outerHTML")), + ) +} diff --git a/web/components/login.go b/web/components/login.go index a2406b8..66b2896 100644 --- a/web/components/login.go +++ b/web/components/login.go @@ -7,8 +7,11 @@ import ( b "github.com/willoma/bulma-gomponents" ) +// todo var BaseUrl = "https://api.portfolio.dariusklein.nl" +var BaseUrl = "http://localhost:4001" + func Login() g.Node { - return FormEl(hx.Post("https://api.portfolio.dariusklein.nl/login"), + return FormEl(hx.Post(BaseUrl+"/login"), //https://api.portfolio.dariusklein.nl/login Class("max-w-xl m-auto py-32"), b.Box( Email(false, false, nil), diff --git a/web/components/projectList.go b/web/components/projectList.go index 0184412..3af6bba 100644 --- a/web/components/projectList.go +++ b/web/components/projectList.go @@ -37,3 +37,35 @@ func Project(project *ent.Project) g.Node { ), ) } + +func EditProjectList(projects []*ent.Project) g.Node { + return Div(Class("py-2 px-2"), g.Group(g.Map(projects, func(p *ent.Project) g.Node { + return EditProject(p) + })), + ) +} + +func EditProject(project *ent.Project) g.Node { + return b.Card( + b.Media( + b.MediaLeft( + b.ImageImg( + project.ImageURL, + e.Alt("project image"), + b.ImgSq64, + ), + ), + b.Label("Name"), + b.Textarea(project.Name, b.Rows(1)), + b.Subtitle( + 6, + b.Label("Repo"), + b.Textarea(project.URL, b.Rows(1)), + b.Label("Docs"), + b.Textarea(project.DocURL, b.Rows(1))), + ), + b.Content( + b.Textarea(project.Description), + ), + ) +} diff --git a/web/handlers/projectPageHandler.go b/web/handlers/projectPageHandler.go index a8ab838..50470e7 100644 --- a/web/handlers/projectPageHandler.go +++ b/web/handlers/projectPageHandler.go @@ -26,7 +26,21 @@ func createProjectBody(w http.ResponseWriter, r *http.Request) g.Node { } return Body( + components.Edit(), components.ProjectList(projects), ) } + +func CreateProjectEditBody(w http.ResponseWriter, r *http.Request) { + + projects, err := query.GetProjects(context.Background()) + if err != nil { + return + } + + w.WriteHeader(http.StatusOK) + Body( + components.EditProjectList(projects), + ).Render(w) +} diff --git a/web/webRoutes.go b/web/webRoutes.go index 2b9607e..daa6e8e 100644 --- a/web/webRoutes.go +++ b/web/webRoutes.go @@ -13,6 +13,7 @@ func WebRoutes() *http.ServeMux { // Register the routes and webHandler mux.HandleFunc("/", handlers.HomePageHandler) mux.HandleFunc("/projects", handlers.ProjectPageHandler) + mux.HandleFunc("/projects/edit", handlers.CreateProjectEditBody) mux.HandleFunc("/about", handlers.AboutPageHandler) mux.HandleFunc("/login", handlers.LoginPageHandler)