fix filters page for authenticated users

This commit is contained in:
byte[] 2019-11-14 22:30:19 -05:00
parent 4915fb5044
commit 9c638c0e3b
3 changed files with 22 additions and 19 deletions

View file

@ -117,14 +117,9 @@ defmodule Philomena.Users.User do
|> validate_required([]) |> validate_required([])
end end
def failure_changeset(user) do def filter_changeset(user, filter) do
changeset = change(user) change(user)
user = changeset.data |> put_change(:current_filter_id, filter.id)
user
|> change(%{
failed_attempts: user.failed_attempts + 1,
})
end end
def create_totp_secret_changeset(user) do def create_totp_secret_changeset(user) do

View file

@ -1,7 +1,8 @@
defmodule PhilomenaWeb.Filter.CurrentController do defmodule PhilomenaWeb.Filter.CurrentController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias Philomena.{Filters, Filters.Filter} alias Philomena.{Filters, Filters.Filter, Users.User}
alias Philomena.Repo
plug :load_resource, model: Filter plug :load_resource, model: Filter
@ -16,16 +17,23 @@ defmodule PhilomenaWeb.Filter.CurrentController do
Filters.default_filter() Filters.default_filter()
end end
conn = conn
if user do |> update_filter(user, filter)
nil |> put_flash(:info, "Switched to filter #{filter.name}")
else |> redirect(to: Routes.filter_path(conn, :index))
conn end
|> put_session(:filter_id, filter.id)
end defp update_filter(conn, nil, filter) do
conn
|> put_session(:filter_id, filter.id)
end
defp update_filter(conn, user, filter) do
{:ok, user} =
user
|> User.filter_changeset(filter)
|> Repo.update()
conn conn
|> put_flash(:info, "Switched to filter #{filter.name}")
|> redirect(to: "/")
end end
end end

View file

@ -24,7 +24,7 @@
strong Your current filter strong Your current filter
- else - else
li li
= button_to "Use this filter", Routes.filter_current_path(@conn, :update, @filter), method: "put", class: "button" = button_to "Use this filter", Routes.filter_current_path(@conn, :update, id: @filter), method: "put", class: "button"
p p
em em
= @filter.description = @filter.description