Skip to content

Commit

Permalink
allow filtering users by year
Browse files Browse the repository at this point in the history
  • Loading branch information
foodelevator committed Nov 13, 2024
1 parent e8b1612 commit 322f55d
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 51 deletions.
10 changes: 10 additions & 0 deletions database/sql/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,20 @@ where case
when @search::text = '' then true
else kthid = @search or first_name ~* @search or family_name ~* @search
end
and case
when @year::text = '' then true
else @year = year_tag
end
order by kthid
limit $1
offset $2;

-- name: GetAllYears :many
select distinct year_tag
from users
where year_tag != ''
order by year_tag;

-- name: UserSetMemberTo :exec
update users
set member_to = $2
Expand Down
39 changes: 38 additions & 1 deletion database/user.sql.go

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

8 changes: 7 additions & 1 deletion handlers/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ func adminUsersForm(s *service.Service, w http.ResponseWriter, r *http.Request)
search := r.FormValue("search")
offsetStr := r.FormValue("offset")
offset, err := strconv.ParseInt(offsetStr, 10, 32)
year := r.FormValue("year")
if err != nil && offsetStr != "" {
return httputil.BadRequest("Invalid int for offset")
}
users, err := s.DB.ListUsers(r.Context(), database.ListUsersParams{
Search: search,
Limit: 21,
Offset: int32(offset),
Year: year,
})
if err != nil {
return err
Expand All @@ -104,7 +106,11 @@ func adminUsersForm(s *service.Service, w http.ResponseWriter, r *http.Request)
users = users[0:20:20]
more = true
}
return templates.MemberList(service.DBUsersToModel(users), search, int(offset), more)
years, err := s.DB.GetAllYears(r.Context())
if err != nil {
return err
}
return templates.MemberList(service.DBUsersToModel(users), search, int(offset), more, years, year)
}

func invites(s *service.Service, w http.ResponseWriter, r *http.Request) httputil.ToResponse {
Expand Down
23 changes: 19 additions & 4 deletions templates/admin.templ
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ templ Members() {
@AdminPage() {
<div class="p-8">
<section hx-get="/admin/users" hx-trigger="load">
@MemberList([]models.User{}, "", 0, false)
@MemberList([]models.User{}, "", 0, false, []string{}, "")
</section>
@uploadForm()
@UploadStatus(false)
</div>
}
}

templ MemberList(users []models.User, search string, offset int, more bool) {
templ MemberList(users []models.User, search string, offset int, more bool, years []string, selectedYear string) {
<div class="grid grid-cols-[repeat(4,auto)] gap-x-2 [&>*]:h-7">
<div class="grid grid-cols-subgrid col-span-full">
<span>Username</span>
Expand Down Expand Up @@ -62,11 +62,26 @@ templ MemberList(users []models.User, search string, offset int, more bool) {
autofocus
}
/>
<select
hx-get="/admin/users"
name="year"
hx-include='input[name="search"]'
class={ selectStyle }
>
<option value="">Year</option>
for _, year := range years {
<option
if year == selectedYear {
selected
}
>{ year }</option>
}
</select>
<button
hx-get="/admin/users"
class={ button }
hx-vals={ templ.JSONString(map[string]any{"offset": max(0, offset-20)}) }
hx-include='input[name="search"]'
hx-include='input[name="search"], select[name="year"]'
if offset == 0 {
disabled
}
Expand All @@ -75,7 +90,7 @@ templ MemberList(users []models.User, search string, offset int, more bool) {
hx-get="/admin/users"
class={ button }
hx-vals={ templ.JSONString(map[string]any{"offset": offset + 20}) }
hx-include='input[name="search"]'
hx-include='input[name="search"], select[name="year"]'
if !more {
disabled
}
Expand Down
Loading

0 comments on commit 322f55d

Please sign in to comment.