Skip to content

Commit

Permalink
Make yearTag editable by users
Browse files Browse the repository at this point in the history
  • Loading branch information
foodelevator committed Nov 7, 2024
1 parent e2c8d42 commit 9f80bd0
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 95 deletions.
6 changes: 6 additions & 0 deletions database/sql/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ where kthid = $1;
update users
set member_to = $2
where kthid = $1;

-- name: UpdateUser :one
update users
set year_tag = coalesce($2, year_tag) -- TODO: would be nice if we could make this function take year_tag as a pointer so it can actually be null here
where kthid = $1
returning *;
28 changes: 28 additions & 0 deletions database/user.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func MountRoutes(s *service.Service) {
http.Handle("GET /{$}", httputil.Route(s, index))
http.Handle("GET /logout", httputil.Route(s, logout))
http.Handle("GET /account", httputil.Route(s, account))
http.Handle("PATCH /account", httputil.Route(s, updateAccount))
http.Handle("GET /invite/{id}", httputil.Route(s, acceptInvite))

// admin.go
Expand Down
24 changes: 24 additions & 0 deletions handlers/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"log/slog"
"net/http"
"time"

Expand Down Expand Up @@ -91,6 +92,29 @@ func account(s *service.Service, w http.ResponseWriter, r *http.Request) httputi
return templates.Account(*user, passkeys, isAdmin)
}

func updateAccount(s *service.Service, w http.ResponseWriter, r *http.Request) httputil.ToResponse {
user, err := s.GetLoggedInUser(r)
if err != nil {
return err
}
if user == nil {
return httputil.Unauthorized()
}
if err := r.ParseForm(); err != nil {
return httputil.BadRequest("Invalid form body")
}
yearTagList := r.Form["year-tag"]
if len(yearTagList) > 0 {
var err error
*user, err = s.UpdateUser(r.Context(), user.KTHID, yearTagList[0])
slog.Info("Updated user year tag", "value", user.YearTag)
if err != nil {
return err
}
}
return templates.AccountYearForm(user.YearTag)
}

func acceptInvite(s *service.Service, w http.ResponseWriter, r *http.Request) httputil.ToResponse {
idString := r.PathValue("id")
if idString == "-" {
Expand Down
18 changes: 4 additions & 14 deletions pkg/static/public/style.dist.css
Original file line number Diff line number Diff line change
Expand Up @@ -860,24 +860,14 @@ html {
text-transform: uppercase;
}

.text-neutral-100 {
--tw-text-opacity: 1;
color: rgb(245 245 245 / var(--tw-text-opacity));
}

.text-cerise-regular {
--tw-text-opacity: 1;
color: rgb(232 61 132 / var(--tw-text-opacity));
}

.text-cerise-strong {
.text-cerise-light {
--tw-text-opacity: 1;
color: rgb(238 42 123 / var(--tw-text-opacity));
color: rgb(236 95 153 / var(--tw-text-opacity));
}

.text-cerise-light {
.text-neutral-100 {
--tw-text-opacity: 1;
color: rgb(236 95 153 / var(--tw-text-opacity));
color: rgb(245 245 245 / var(--tw-text-opacity));
}

.outline-none {
Expand Down
36 changes: 26 additions & 10 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@ import (
"github.com/jackc/pgx/v5"
)

func (s *Service) GetUser(ctx context.Context, kthid string) (*models.User, error) {
user, err := s.DB.GetUser(ctx, kthid)
if err == pgx.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
func dbUserToModel(user database.User) models.User {
var memberTo time.Time
if user.MemberTo.Valid {
memberTo = user.MemberTo.Time
}
return &models.User{
return models.User{
KTHID: user.Kthid,
UGKTHID: user.UgKthid,
Email: user.Email,
Expand All @@ -38,7 +31,30 @@ func (s *Service) GetUser(ctx context.Context, kthid string) (*models.User, erro
YearTag: user.YearTag,
MemberTo: memberTo,
WebAuthnID: user.WebauthnID,
}, nil
}
}

func (s *Service) GetUser(ctx context.Context, kthid string) (*models.User, error) {
user, err := s.DB.GetUser(ctx, kthid)
if err == pgx.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
u := dbUserToModel(user)
return &u, nil
}

func (s *Service) UpdateUser(ctx context.Context, kthid string, yearTag string) (models.User, error) {
user, err := s.DB.UpdateUser(ctx, database.UpdateUserParams{
Kthid: kthid,
YearTag: yearTag,
})
if err != nil {
return models.User{}, err
}
return dbUserToModel(user), nil
}

func (s *Service) LoginUser(ctx context.Context, kthid string) httputil.ToResponse {
Expand Down
16 changes: 10 additions & 6 deletions templates/user.templ
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ templ Account(user models.User, passkeys []models.Passkey, isAdmin bool) {
<p class="text-xl text-cerise-light">Email address</p>
<p>{ user.Email }</p>
</div>
<form>
<label for="year-tag" class="text-xl text-cerise-light">Year</label>
<br/>
<input class={ input + " w-16" } name="year-tag" id="year-tag" value={ user.YearTag } _="on input show next <button/>"/>
<button style="display: none" class={ button }>Save</button>
</form>
@AccountYearForm(user.YearTag)
<div>
if user.MemberTo == (time.Time{}) {
<p>Not a chapter member</p>
Expand All @@ -61,6 +56,15 @@ templ Account(user models.User, passkeys []models.Passkey, isAdmin bool) {
}
}

templ AccountYearForm(yearTag string) {
<form hx-patch="/account" hx-swap="outerHTML">
<label for="year-tag" class="text-xl text-cerise-light">Year</label>
<br/>
<input class={ input + " w-16" } name="year-tag" id="year-tag" value={ yearTag } _="on input show next <button/>"/>
<button style="display: none" class={ button }>Save</button>
</form>
}

templ AcceptInvite() {
@modal() {
<div class="p-8 flex flex-col gap-4">
Expand Down
Loading

0 comments on commit 9f80bd0

Please sign in to comment.