Move assignment to live_helpers, fix bug when user updates email

This commit is contained in:
Logan Hunt 2022-04-13 16:55:02 -06:00
parent 51298ea998
commit 2ee849378a
9 changed files with 30 additions and 40 deletions

View File

@ -5,9 +5,9 @@ defmodule Aggiedit.Accounts do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Aggiedit.Repo alias Aggiedit.Repo
alias Aggiedit.Rooms
alias Aggiedit.Accounts.{User, UserToken, UserNotifier} alias Aggiedit.Accounts.{User, UserToken, UserNotifier}
alias Aggiedit.Rooms
## Database getters ## Database getters
@ -140,7 +140,7 @@ defmodule Aggiedit.Accounts do
with {:ok, query} <- UserToken.verify_change_email_token_query(token, context), with {:ok, query} <- UserToken.verify_change_email_token_query(token, context),
%UserToken{sent_to: email} <- Repo.one(query), %UserToken{sent_to: email} <- Repo.one(query),
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)), {:ok, %{:user => user}} <- Repo.transaction(user_email_multi(user, email, context)),
{:ok, _user} <- set_user_room(user) do {:ok, _user} <- set_user_room(user) do
:ok :ok
else else

View File

@ -1,7 +1,6 @@
defmodule Aggiedit.Rooms.Post do defmodule Aggiedit.Rooms.Post do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Aggiedit.Repo
schema "posts" do schema "posts" do
field :body, :string field :body, :string

View File

@ -3,7 +3,6 @@ defmodule Aggiedit.Utils do
domain_split = Regex.named_captures(~r/^.*@(?<domain>.*)$/, email)["domain"] domain_split = Regex.named_captures(~r/^.*@(?<domain>.*)$/, email)["domain"]
|> String.downcase() |> String.downcase()
|> String.split(".") |> String.split(".")
IO.puts(inspect(domain_split))
if Enum.count(domain_split) >= 2 do if Enum.count(domain_split) >= 2 do
Enum.join(Enum.take(domain_split, -2), ".") Enum.join(Enum.take(domain_split, -2), ".")

View File

@ -7,6 +7,11 @@ defmodule AggieditWeb.LiveHelpers do
alias Aggiedit.Accounts alias Aggiedit.Accounts
alias Aggiedit.Accounts.User alias Aggiedit.Accounts.User
alias Aggiedit.Rooms
alias Aggiedit.Roles
alias AggieditWeb.Router.Helpers, as: Routes
@doc """ @doc """
Renders a live component inside a modal. Renders a live component inside a modal.
@ -69,4 +74,17 @@ defmodule AggieditWeb.LiveHelpers do
_ -> socket _ -> socket
end end
end end
def assign_socket_room_and_user_or_error(%{"room_id" => room_id}, session, socket) do
socket = assign_socket_user(session, socket)
case socket.assigns do
%{:current_user => user} ->
room = Rooms.get_room!(room_id)
case Roles.guard?(user, :index, room) do
true -> {:ok, assign(socket, %{:room => room})}
_ -> {:ok, socket |> put_flash(:error, "You cannot view that room") |> redirect(to: Routes.page_path(socket, :index))}
end
_ -> {:ok, socket |> put_flash(:error, "You must log in to access this page.") |> redirect(to: Routes.user_session_path(socket, :new))}
end
end
end end

View File

@ -4,7 +4,6 @@ defmodule AggieditWeb.PostLive.FormComponent do
alias Aggiedit.Rooms alias Aggiedit.Rooms
alias Aggiedit.Rooms.Post alias Aggiedit.Rooms.Post
alias Aggiedit.Uploads alias Aggiedit.Uploads
alias Aggiedit.Uploads.Upload
alias Aggiedit.Repo alias Aggiedit.Repo
@impl true @impl true

View File

@ -26,8 +26,8 @@
</div> </div>
</div> </div>
<% end %> <% end %>
<%= else %> <% else %>
<% if !is_nil(@post.upload) do %> <%= if !is_nil(@post.upload) do %>
<div class="row"> <div class="row">
<div class="column"> <div class="column">
<img style="height:80px" src={Routes.static_path(@socket, "/uploads/#{@post.upload.file}")} /> <img style="height:80px" src={Routes.static_path(@socket, "/uploads/#{@post.upload.file}")} />

View File

@ -1,18 +0,0 @@
defmodule AggieditWeb.PostLive.Helper do
use AggieditWeb, :live_view
alias Aggiedit.Rooms
alias Aggiedit.Roles
def assign_socket_room_and_user_or_error(%{"room_id" => room_id}=params, session, socket) do
socket = assign_socket_user(session, socket)
case socket.assigns do
%{:current_user => user} ->
room = Rooms.get_room!(room_id)
case Roles.guard?(socket.assigns.current_user, :index, room) do
true -> {:ok, assign(socket, %{:room => room})}
_ -> {:ok, socket |> put_flash(:error, "You cannot view that room") |> redirect(to: Routes.page_path(socket, :index))}
end
_ -> {:ok, socket |> put_flash(:error, "You must log in to access this page.") |> redirect(to: Routes.user_session_path(socket, :new))}
end
end
end

View File

@ -1,23 +1,16 @@
defmodule AggieditWeb.PostLive.Index do defmodule AggieditWeb.PostLive.Index do
use AggieditWeb, :live_view use AggieditWeb, :live_view
alias Aggiedit.Accounts.User
alias Aggiedit.Roles alias Aggiedit.Roles
alias Aggiedit.Rooms alias Aggiedit.Rooms
alias Aggiedit.Rooms.{Post, Room} alias Aggiedit.Rooms.Post
alias Aggiedit.Repo alias Aggiedit.Repo
@impl true @impl true
def mount(%{"room_id" => room_id} = params, session, socket) do def mount(%{"room_id" => _room_id} = params, session, socket) do
{:ok, socket} = AggieditWeb.PostLive.Helper.assign_socket_room_and_user_or_error(params, session, socket) {:ok, socket} = assign_socket_room_and_user_or_error(params, session, socket)
# if !is_nil(socket.assigns[:room]) do
# {:ok, assign(socket, %{:posts => socket.assigns.room |> Repo.preload(:posts) |> Map.get(:posts)})}
# else
# {:ok, socket}
# end
case socket.assigns do case socket.assigns do
%{:room => room} -> %{:room => room} -> {:ok, assign(socket, %{:posts => room |> Repo.preload(:posts) |> Map.get(:posts)})}
{:ok, assign(socket, %{:posts => room |> Repo.preload(:posts) |> Map.get(:posts)})}
_ -> {:ok, socket} _ -> {:ok, socket}
end end
end end
@ -38,7 +31,7 @@ defmodule AggieditWeb.PostLive.Index do
{:noreply, apply_action(socket, socket.assigns.live_action, params)} {:noreply, apply_action(socket, socket.assigns.live_action, params)}
end end
defp apply_action(socket, :edit, %{"id" => id}=params) do defp apply_action(socket, :edit, %{"id" => id}) do
socket socket
|> assign(:page_title, "Edit Post") |> assign(:page_title, "Edit Post")
|> assign(:post, Rooms.get_post!(id) |> Repo.preload(:upload)) |> assign(:post, Rooms.get_post!(id) |> Repo.preload(:upload))

View File

@ -6,12 +6,12 @@ defmodule AggieditWeb.PostLive.Show do
alias Aggiedit.Repo alias Aggiedit.Repo
@impl true @impl true
def mount(%{"room_id" => room_id} = params, session, socket) do def mount(%{"room_id" => _room_id} = params, session, socket) do
AggieditWeb.PostLive.Helper.assign_socket_room_and_user_or_error(params, session, socket) assign_socket_room_and_user_or_error(params, session, socket)
end end
@impl true @impl true
def handle_params(%{"id" => id}=params, _, socket) do def handle_params(%{"id" => id}, _, socket) do
post = Rooms.get_post!(id) post = Rooms.get_post!(id)
|> Repo.preload(:upload) |> Repo.preload(:upload)
if Roles.guard?(socket.assigns.current_user, socket.assigns.live_action, post) do if Roles.guard?(socket.assigns.current_user, socket.assigns.live_action, post) do