Skip to content

Commit

Permalink
Tag change search (#234)
Browse files Browse the repository at this point in the history
* profile/tag_change: add search box to show only a single tag

* Minor fixup

---------

Co-authored-by: prg <[email protected]>
  • Loading branch information
liamwhite and prg authored Apr 27, 2024
1 parent f1a75e8 commit eb79ee4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
23 changes: 22 additions & 1 deletion lib/philomena_web/controllers/profile/tag_change_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Profile.TagChangeController do

alias Philomena.Users.User
alias Philomena.Images.Image
alias Philomena.Tags.Tag
alias Philomena.TagChanges.TagChange
alias Philomena.Repo
import Ecto.Query
Expand All @@ -16,19 +17,27 @@ defmodule PhilomenaWeb.Profile.TagChangeController do
tag_changes =
TagChange
|> join(:inner, [tc], i in Image, on: tc.image_id == i.id)
|> only_tag_join(params)
|> where(
[tc, i],
tc.user_id == ^user.id and not (i.user_id == ^user.id and i.anonymous == true)
)
|> added_filter(params)
|> only_tag_filter(params)
|> preload([:tag, :user, image: [:user, :sources, tags: :aliases]])
|> order_by(desc: :id)
|> Repo.paginate(conn.assigns.scrivener)

# params.permit(:added, :only_tag) ...
pagination_params =
[added: conn.params["added"], only_tag: conn.params["only_tag"]]
|> Keyword.filter(fn {k, _v} -> Map.has_key?(conn.params, "#{k}") end)

render(conn, "index.html",
title: "Tag Changes for User `#{user.name}'",
user: user,
tag_changes: tag_changes
tag_changes: tag_changes,
pagination_params: pagination_params
)
end

Expand All @@ -40,4 +49,16 @@ defmodule PhilomenaWeb.Profile.TagChangeController do

defp added_filter(query, _params),
do: query

defp only_tag_join(query, %{"only_tag" => only_tag}) when only_tag != "",
do: join(query, :inner, [tc], t in Tag, on: tc.tag_id == t.id)

defp only_tag_join(query, _params),
do: query

defp only_tag_filter(query, %{"only_tag" => only_tag}) when only_tag != "",
do: where(query, [_, _, t], t.name == ^only_tag)

defp only_tag_filter(query, _params),
do: query
end
14 changes: 7 additions & 7 deletions lib/philomena_web/templates/profile/tag_change/index.html.slime
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ h1
= @user.name

- route = fn p -> Routes.profile_tag_change_path(@conn, :index, @user, p) end
- params = if @conn.params["added"], do: [added: @conn.params["added"]]
- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @tag_changes, route: route, conn: @conn, params: params
- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @tag_changes, route: route, conn: @conn, params: @pagination_params

.block
.block__header
span.block__header__title
| Display only:
= form_for @conn, Routes.profile_tag_change_path(@conn, :index, @user), [method: "get", enforce_utf8: false], fn f ->
= text_input f, :only_tag, class: "input", placeholder: "Tag", title: "Only show this tag", autocapitalize: "none"
= submit "Search", class: "button", title: "Search"

= link "Removed", to: Routes.profile_tag_change_path(@conn, :index, @user, added: 0)
= link "Added", to: Routes.profile_tag_change_path(@conn, :index, @user, added: 1)
= link "All", to: Routes.profile_tag_change_path(@conn, :index, @user)
= link "Removed", to: Routes.profile_tag_change_path(@conn, :index, @user, Keyword.merge(@pagination_params, added: 0))
= link "Added", to: Routes.profile_tag_change_path(@conn, :index, @user, Keyword.merge(@pagination_params, added: 1))
= link "All", to: Routes.profile_tag_change_path(@conn, :index, @user, Keyword.delete(@pagination_params, :added))

= render PhilomenaWeb.TagChangeView, "index.html", conn: @conn, tag_changes: @tag_changes, pagination: pagination

0 comments on commit eb79ee4

Please sign in to comment.