only show action buttons when appropriate
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
defmodule MemexWeb.NoteLive.Index do
|
||||
use MemexWeb, :live_view
|
||||
alias Memex.{Notes, Notes.Note}
|
||||
alias Memex.{Accounts.User, Notes, Notes.Note}
|
||||
|
||||
@impl true
|
||||
def mount(%{"search" => search}, _session, socket) do
|
||||
@ -76,4 +76,13 @@ defmodule MemexWeb.NoteLive.Index do
|
||||
defp display_notes(%{assigns: %{search: search}} = socket) do
|
||||
socket |> assign(notes: Notes.list_public_notes(search))
|
||||
end
|
||||
|
||||
@spec is_owner_or_admin?(Note.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?(Note.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
|
||||
|
@ -30,13 +30,15 @@
|
||||
notes={@notes}
|
||||
>
|
||||
<:actions :let={note}>
|
||||
<%= if @current_user do %>
|
||||
<%= if is_owner?(note, @current_user) do %>
|
||||
<.link
|
||||
patch={Routes.note_index_path(@socket, :edit, note)}
|
||||
data-qa={"note-edit-#{note.id}"}
|
||||
>
|
||||
<%= dgettext("actions", "edit") %>
|
||||
</.link>
|
||||
<% end %>
|
||||
<%= if is_owner_or_admin?(note, @current_user) do %>
|
||||
<.link
|
||||
href="#"
|
||||
phx-click="delete"
|
||||
|
@ -1,7 +1,7 @@
|
||||
defmodule MemexWeb.NoteLive.Show do
|
||||
use MemexWeb, :live_view
|
||||
|
||||
alias Memex.Notes
|
||||
alias Memex.{Accounts.User, Notes, Notes.Note}
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
@ -38,4 +38,13 @@ defmodule MemexWeb.NoteLive.Show do
|
||||
|
||||
defp page_title(:show), do: gettext("show note")
|
||||
defp page_title(:edit), do: gettext("edit note")
|
||||
|
||||
@spec is_owner_or_admin?(Note.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?(Note.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
|
||||
|
@ -19,14 +19,15 @@
|
||||
</p>
|
||||
|
||||
<div class="self-end flex space-x-4">
|
||||
<.link class="btn btn-primary" patch={Routes.note_index_path(@socket, :index)}>
|
||||
<.link class="btn btn-primary" navigate={Routes.note_index_path(@socket, :index)}>
|
||||
<%= dgettext("actions", "back") %>
|
||||
</.link>
|
||||
<%= if @current_user do %>
|
||||
<%= if is_owner?(@note, @current_user) do %>
|
||||
<.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note)}>
|
||||
<%= dgettext("actions", "edit") %>
|
||||
</.link>
|
||||
|
||||
<% end %>
|
||||
<%= if is_owner_or_admin?(@note, @current_user) do %>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
|
Reference in New Issue
Block a user