From 5788d5b509415174d10d26a80c82aaec9b2d0f5e Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 11 Sep 2024 11:17:28 +0200 Subject: [PATCH] Allow users to edit their name --- public/css/tw-input.css | 6 +++++ server/router/handler/user.go | 33 ++++++++++++++++++++++++++ server/router/pages/user.templ | 43 +++++++++++++++++++++++++++++++--- server/router/router.go | 1 + 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/public/css/tw-input.css b/public/css/tw-input.css index b239d52..0560ef2 100644 --- a/public/css/tw-input.css +++ b/public/css/tw-input.css @@ -23,6 +23,10 @@ transition: background-color 150ms ease-in; } + * { + transition: background-color 150ms ease-in, color 150ms ease-in; + } + #content { min-height: 85svh; } @@ -97,9 +101,11 @@ } input { + padding: 0 0.2em; color: var(--black); } + .text-muted { color: var(--muted); } diff --git a/server/router/handler/user.go b/server/router/handler/user.go index 894f796..30347a3 100644 --- a/server/router/handler/user.go +++ b/server/router/handler/user.go @@ -1,6 +1,9 @@ package handler import ( + "fmt" + "net/http" + "git.ekzyis.com/ekzyis/delphi.market/server/router/context" "git.ekzyis.com/ekzyis/delphi.market/server/router/pages" "git.ekzyis.com/ekzyis/delphi.market/types" @@ -13,3 +16,33 @@ func HandleUser(sc context.Context) echo.HandlerFunc { return pages.User(&u).Render(context.RenderContext(sc, c), c.Response().Writer) } } + +func HandleUserEdit(sc context.Context) echo.HandlerFunc { + return func(c echo.Context) error { + var ( + db = sc.Db + ctx = c.Request().Context() + u = c.Get("session").(types.User) + name = c.FormValue("name") + + maxLength = 16 + err error + ) + + if name == "" { + return echo.NewHTTPError(http.StatusBadRequest, "name is required") + } + + if len(name) > maxLength { + echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("name cannot be longer than %d characters", maxLength)) + } + + if err = db.QueryRowContext(ctx, + "UPDATE users SET name = $1 WHERE id = $2 RETURNING name", + name, u.Id).Scan(&u.Name); err != nil { + return err + } + + return pages.User(&u).Render(context.RenderContext(sc, c), c.Response().Writer) + } +} diff --git a/server/router/pages/user.templ b/server/router/pages/user.templ index 3ad0f02..513c49e 100644 --- a/server/router/pages/user.templ +++ b/server/router/pages/user.templ @@ -15,7 +15,7 @@ templ User(user *types.User) {
@components.Figlet("random", "user")
id
{ strconv.Itoa(user.Id) }
name
-
{ user.Name }
+
+ { user.Name } + + + +
joined
{ user.CreatedAt.Format(time.DateOnly) }
sats
-
{ strconv.Itoa(int(user.Msats) / 1000) }
+
+ { strconv.Itoa(int(user.Msats) / 1000) } + +
+ @components.Footer() diff --git a/server/router/router.go b/server/router/router.go index 429f968..a12f07a 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -28,6 +28,7 @@ func Init(e *echo.Echo, sc Context) { e.GET("/session", handler.HandleSessionCheck(sc)) e.GET("/user", handler.HandleUser(sc), middleware.SessionGuard(sc)) + e.PUT("/user", handler.HandleUserEdit(sc), middleware.SessionGuard(sc)) e.POST("/logout", handler.HandleLogout(sc), middleware.SessionGuard(sc)) e.GET("/invoice/:hash", handler.HandleInvoice(sc), middleware.SessionGuard(sc))