diff --git a/lib/philomena_web/controllers/profile/tag_change_controller.ex b/lib/philomena_web/controllers/profile/tag_change_controller.ex index 358990e69..21524d4bf 100644 --- a/lib/philomena_web/controllers/profile/tag_change_controller.ex +++ b/lib/philomena_web/controllers/profile/tag_change_controller.ex @@ -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 @@ -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 @@ -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 diff --git a/lib/philomena_web/templates/profile/tag_change/index.html.slime b/lib/philomena_web/templates/profile/tag_change/index.html.slime index 8fe9a9a49..563a7fc3b 100644 --- a/lib/philomena_web/templates/profile/tag_change/index.html.slime +++ b/lib/philomena_web/templates/profile/tag_change/index.html.slime @@ -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