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([])
end
def failure_changeset(user) do
changeset = change(user)
user = changeset.data
user
|> change(%{
failed_attempts: user.failed_attempts + 1,
})
def filter_changeset(user, filter) do
change(user)
|> put_change(:current_filter_id, filter.id)
end
def create_totp_secret_changeset(user) do

View file

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

View file

@ -24,7 +24,7 @@
strong Your current filter
- else
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
em
= @filter.description