diff --git a/lib/aggiedit_web/live/post_live/form_component.ex b/lib/aggiedit_web/live/post_live/form_component.ex index 66f2477..c316c0b 100644 --- a/lib/aggiedit_web/live/post_live/form_component.ex +++ b/lib/aggiedit_web/live/post_live/form_component.ex @@ -14,9 +14,7 @@ defmodule AggieditWeb.PostLive.FormComponent do {:ok, socket |> assign(assigns) - |> assign(:changeset, changeset) - |> assign(:current_user, current_user) - |> assign(:uploaded_files, []) + |> assign(%{changeset: changeset, current_user: current_user, uploaded_files: []}) |> allow_upload(:upload, accept: ~w(.jpg .jpeg .png .gif), max_entries: 1) } end diff --git a/lib/aggiedit_web/live/post_live/helper.ex b/lib/aggiedit_web/live/post_live/helper.ex new file mode 100644 index 0000000..5b8e5be --- /dev/null +++ b/lib/aggiedit_web/live/post_live/helper.ex @@ -0,0 +1,18 @@ +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 \ No newline at end of file diff --git a/lib/aggiedit_web/live/post_live/index.ex b/lib/aggiedit_web/live/post_live/index.ex index e78342d..26c078c 100644 --- a/lib/aggiedit_web/live/post_live/index.ex +++ b/lib/aggiedit_web/live/post_live/index.ex @@ -8,31 +8,27 @@ defmodule AggieditWeb.PostLive.Index do alias Aggiedit.Repo @impl true - def mount(%{"id" => room_id} = params, session, socket) do - socket = assign_socket_user(session, socket) + 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 case socket.assigns do - %{:current_user => user} -> - room = Rooms.get_room!(room_id) - case Roles.guard?(socket.assigns.current_user, socket.assigns.live_action, room) do - true -> {:ok, assign(socket, :posts, list_posts(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))} + %{:room => room} -> + {:ok, assign(socket, %{:posts => room |> Repo.preload(:posts) |> Map.get(:posts)})} + _ -> {:ok, socket} end - end @impl true def handle_params(%{"id" => id}=params, _url, socket) do - if socket.assigns.live_action != :index do - post = Rooms.get_post!(id) - if Roles.guard?(socket.assigns.current_user, socket.assigns.live_action, post) do - {:noreply, apply_action(socket, socket.assigns.live_action, params)} - else - {:noreply, socket |> put_flash(:error, "You do not have permission to edit this post.") |> redirect(to: Routes.post_index_path(socket, :index))} - end + post = Rooms.get_post!(id) + if Roles.guard?(socket.assigns.current_user, socket.assigns.live_action, post) do + {:noreply, apply_action(socket, socket.assigns.live_action, params)} else - {:noreply, socket} + {:noreply, socket |> put_flash(:error, "You do not have permission to edit this post.") |> redirect(to: Routes.post_index_path(socket, :index, socket.assigns.room))} end end @@ -65,13 +61,9 @@ defmodule AggieditWeb.PostLive.Index do post = Rooms.get_post!(id) if Roles.guard?(socket.assigns.current_user, :delete, post) do Rooms.delete_post(post) - {:noreply, socket |> put_flash(:success, "Post deleted.") |> redirect(to: Routes.post_index_path(socket, :index))} + {:noreply, socket |> put_flash(:success, "Post deleted.") |> redirect(to: Routes.post_index_path(socket, :index, socket.assigns.room))} else - {:noreply, socket |> put_flash(:error, "You do not have permission to delete this post.") |> redirect(to: Routes.post_index_path(socket, :index))} + {:noreply, socket |> put_flash(:error, "You do not have permission to delete this post.") |> redirect(to: Routes.post_index_path(socket, :index, socket.assigns.room))} end end - - defp list_posts(%Room{id: room_id}) do - Rooms.posts_in_room(room_id) - end end diff --git a/lib/aggiedit_web/live/post_live/index.html.heex b/lib/aggiedit_web/live/post_live/index.html.heex index 1804971..329f84c 100644 --- a/lib/aggiedit_web/live/post_live/index.html.heex +++ b/lib/aggiedit_web/live/post_live/index.html.heex @@ -1,7 +1,7 @@