make projects editable ui added

TODO
add option to save changes
This commit is contained in:
darius 2024-06-25 00:26:56 +02:00
parent 6c9c7cc33e
commit be182fd222
7 changed files with 97 additions and 1 deletions

View File

@ -20,6 +20,7 @@ func ApiRoutes() *http.ServeMux {
//auth //auth
mux.HandleFunc("POST /login", handlers.Login) mux.HandleFunc("POST /login", handlers.Login)
mux.HandleFunc("POST /register", handlers.CreateUserHandler) mux.HandleFunc("POST /register", handlers.CreateUserHandler)
mux.HandleFunc("GET /htmx/canEdit", handlers.CanEdit)
//Project //Project
mux.HandleFunc("POST /project", handlers.CreateProjectHandler) mux.HandleFunc("POST /project", handlers.CreateProjectHandler)

View File

@ -63,3 +63,25 @@ func Login(w http.ResponseWriter, r *http.Request) {
return 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("<button class=\"button is-link\">Edit</button>"))
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte(""))
}
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(""))
}

23
web/components/edit.go Normal file
View File

@ -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")),
)
}

View File

@ -7,8 +7,11 @@ import (
b "github.com/willoma/bulma-gomponents" b "github.com/willoma/bulma-gomponents"
) )
// todo var BaseUrl = "https://api.portfolio.dariusklein.nl"
var BaseUrl = "http://localhost:4001"
func Login() g.Node { 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"), Class("max-w-xl m-auto py-32"),
b.Box( b.Box(
Email(false, false, nil), Email(false, false, nil),

View File

@ -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),
),
)
}

View File

@ -26,7 +26,21 @@ func createProjectBody(w http.ResponseWriter, r *http.Request) g.Node {
} }
return Body( return Body(
components.Edit(),
components.ProjectList(projects), 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)
}

View File

@ -13,6 +13,7 @@ func WebRoutes() *http.ServeMux {
// Register the routes and webHandler // Register the routes and webHandler
mux.HandleFunc("/", handlers.HomePageHandler) mux.HandleFunc("/", handlers.HomePageHandler)
mux.HandleFunc("/projects", handlers.ProjectPageHandler) mux.HandleFunc("/projects", handlers.ProjectPageHandler)
mux.HandleFunc("/projects/edit", handlers.CreateProjectEditBody)
mux.HandleFunc("/about", handlers.AboutPageHandler) mux.HandleFunc("/about", handlers.AboutPageHandler)
mux.HandleFunc("/login", handlers.LoginPageHandler) mux.HandleFunc("/login", handlers.LoginPageHandler)