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
alias Aggiedit.Repo
alias Aggiedit.Rooms
alias Aggiedit.Accounts.{User, UserToken, UserNotifier}
alias Aggiedit.Rooms
## Database getters
@ -140,7 +140,7 @@ defmodule Aggiedit.Accounts do
with {:ok, query} <- UserToken.verify_change_email_token_query(token, context),
%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
else

View File

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

View File

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

View File

@ -7,6 +7,11 @@ defmodule AggieditWeb.LiveHelpers do
alias Aggiedit.Accounts
alias Aggiedit.Accounts.User
alias Aggiedit.Rooms
alias Aggiedit.Roles
alias AggieditWeb.Router.Helpers, as: Routes
@doc """
Renders a live component inside a modal.
@ -69,4 +74,17 @@ defmodule AggieditWeb.LiveHelpers do
_ -> socket
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

View File

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

View File

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

View File

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