move is_owner? and is_owner_or_admin? to context methods
This commit is contained in:
		| @@ -1,6 +1,6 @@ | ||||
| defmodule MemexWeb.ContextLive.Index do | ||||
|   use MemexWeb, :live_view | ||||
|   alias Memex.{Accounts.User, Contexts, Contexts.Context} | ||||
|   alias Memex.{Contexts, Contexts.Context} | ||||
|  | ||||
|   @impl true | ||||
|   def mount(%{"search" => search}, _session, socket) do | ||||
| @@ -76,13 +76,4 @@ defmodule MemexWeb.ContextLive.Index do | ||||
|   defp display_contexts(%{assigns: %{search: search}} = socket) do | ||||
|     socket |> assign(contexts: Contexts.list_public_contexts(search)) | ||||
|   end | ||||
|  | ||||
|   @spec is_owner_or_admin?(Context.t(), User.t()) :: boolean() | ||||
|   defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true | ||||
|   defp is_owner_or_admin?(_context, %{role: :admin}), do: true | ||||
|   defp is_owner_or_admin?(_context, _other_user), do: false | ||||
|  | ||||
|   @spec is_owner?(Context.t(), User.t()) :: boolean() | ||||
|   defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true | ||||
|   defp is_owner?(_context, _other_user), do: false | ||||
| end | ||||
|   | ||||
| @@ -32,14 +32,14 @@ | ||||
|     > | ||||
|       <:actions :let={context}> | ||||
|         <.link | ||||
|           :if={is_owner?(context, @current_user)} | ||||
|           :if={Contexts.is_owner?(context, @current_user)} | ||||
|           patch={Routes.context_index_path(@socket, :edit, context.slug)} | ||||
|           aria-label={dgettext("actions", "edit %{context_slug}", context_slug: context.slug)} | ||||
|         > | ||||
|           <%= dgettext("actions", "edit") %> | ||||
|         </.link> | ||||
|         <.link | ||||
|           :if={is_owner_or_admin?(context, @current_user)} | ||||
|           :if={Contexts.is_owner_or_admin?(context, @current_user)} | ||||
|           href="#" | ||||
|           phx-click="delete" | ||||
|           phx-value-id={context.id} | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| defmodule MemexWeb.ContextLive.Show do | ||||
|   use MemexWeb, :live_view | ||||
|   alias Memex.{Accounts.User, Contexts, Contexts.Context} | ||||
|   alias Memex.Contexts | ||||
|  | ||||
|   @impl true | ||||
|   def mount(_params, _session, socket) do | ||||
| @@ -45,13 +45,4 @@ defmodule MemexWeb.ContextLive.Show do | ||||
|  | ||||
|   defp page_title(:show, %{slug: slug}), do: slug | ||||
|   defp page_title(:edit, %{slug: slug}), do: gettext("edit %{slug}", slug: slug) | ||||
|  | ||||
|   @spec is_owner_or_admin?(Context.t(), User.t()) :: boolean() | ||||
|   defp is_owner_or_admin?(%{user_id: user_id}, %{id: user_id}), do: true | ||||
|   defp is_owner_or_admin?(_context, %{role: :admin}), do: true | ||||
|   defp is_owner_or_admin?(_context, _other_user), do: false | ||||
|  | ||||
|   @spec is_owner?(Context.t(), User.t()) :: boolean() | ||||
|   defp is_owner?(%{user_id: user_id}, %{id: user_id}), do: true | ||||
|   defp is_owner?(_context, _other_user), do: false | ||||
| end | ||||
|   | ||||
| @@ -24,14 +24,14 @@ | ||||
|       <%= dgettext("actions", "back") %> | ||||
|     </.link> | ||||
|     <.link | ||||
|       :if={is_owner?(@context, @current_user)} | ||||
|       :if={Contexts.is_owner?(@context, @current_user)} | ||||
|       class="btn btn-primary" | ||||
|       patch={Routes.context_show_path(@socket, :edit, @context.slug)} | ||||
|     > | ||||
|       <%= dgettext("actions", "edit") %> | ||||
|     </.link> | ||||
|     <button | ||||
|       :if={is_owner_or_admin?(@context, @current_user)} | ||||
|       :if={Contexts.is_owner_or_admin?(@context, @current_user)} | ||||
|       type="button" | ||||
|       class="btn btn-primary" | ||||
|       phx-click="delete" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user