only show action buttons when appropriate

This commit is contained in:
shibao 2022-11-24 17:15:10 -05:00
parent 632c2b3480
commit 44c8cf77bb
14 changed files with 96 additions and 33 deletions

View File

@ -1,6 +1,6 @@
defmodule MemexWeb.ContextLive.Index do defmodule MemexWeb.ContextLive.Index do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.{Contexts, Contexts.Context} alias Memex.{Accounts.User, Contexts, Contexts.Context}
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do def mount(%{"search" => search}, _session, socket) do
@ -77,4 +77,13 @@ defmodule MemexWeb.ContextLive.Index do
defp display_contexts(%{assigns: %{search: search}} = socket) do defp display_contexts(%{assigns: %{search: search}} = socket) do
socket |> assign(contexts: Contexts.list_public_contexts(search)) socket |> assign(contexts: Contexts.list_public_contexts(search))
end 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 end

View File

@ -30,13 +30,15 @@
contexts={@contexts} contexts={@contexts}
> >
<:actions :let={context}> <:actions :let={context}>
<%= if @current_user do %> <%= if is_owner?(context, @current_user) do %>
<.link <.link
patch={Routes.context_index_path(@socket, :edit, context)} patch={Routes.context_index_path(@socket, :edit, context)}
data-qa={"context-edit-#{context.id}"} data-qa={"context-edit-#{context.id}"}
> >
<%= dgettext("actions", "edit") %> <%= dgettext("actions", "edit") %>
</.link> </.link>
<% end %>
<%= if is_owner_or_admin?(context, @current_user) do %>
<.link <.link
href="#" href="#"
phx-click="delete" phx-click="delete"

View File

@ -1,7 +1,7 @@
defmodule MemexWeb.ContextLive.Show do defmodule MemexWeb.ContextLive.Show do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.Contexts alias Memex.{Accounts.User, Contexts, Contexts.Context}
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
@ -38,4 +38,13 @@ defmodule MemexWeb.ContextLive.Show do
defp page_title(:show), do: gettext("show context") defp page_title(:show), do: gettext("show context")
defp page_title(:edit), do: gettext("edit context") defp page_title(:edit), do: gettext("edit context")
@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 end

View File

@ -19,14 +19,15 @@
</p> </p>
<div class="self-end flex space-x-4"> <div class="self-end flex space-x-4">
<.link class="btn btn-primary" patch={Routes.context_index_path(@socket, :index)}> <.link class="btn btn-primary" navigate={Routes.context_index_path(@socket, :index)}>
<%= dgettext("actions", "back") %> <%= dgettext("actions", "back") %>
</.link> </.link>
<%= if @current_user do %> <%= if is_owner?(@context, @current_user) do %>
<.link class="btn btn-primary" patch={Routes.context_show_path(@socket, :edit, @context)}> <.link class="btn btn-primary" patch={Routes.context_show_path(@socket, :edit, @context)}>
<%= dgettext("actions", "edit") %> <%= dgettext("actions", "edit") %>
</.link> </.link>
<% end %>
<%= if is_owner_or_admin?(@context, @current_user) do %>
<button <button
type="button" type="button"
class="btn btn-primary" class="btn btn-primary"

View File

@ -1,6 +1,6 @@
defmodule MemexWeb.NoteLive.Index do defmodule MemexWeb.NoteLive.Index do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.{Notes, Notes.Note} alias Memex.{Accounts.User, Notes, Notes.Note}
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do def mount(%{"search" => search}, _session, socket) do
@ -76,4 +76,13 @@ defmodule MemexWeb.NoteLive.Index do
defp display_notes(%{assigns: %{search: search}} = socket) do defp display_notes(%{assigns: %{search: search}} = socket) do
socket |> assign(notes: Notes.list_public_notes(search)) socket |> assign(notes: Notes.list_public_notes(search))
end 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 end

View File

@ -30,13 +30,15 @@
notes={@notes} notes={@notes}
> >
<:actions :let={note}> <:actions :let={note}>
<%= if @current_user do %> <%= if is_owner?(note, @current_user) do %>
<.link <.link
patch={Routes.note_index_path(@socket, :edit, note)} patch={Routes.note_index_path(@socket, :edit, note)}
data-qa={"note-edit-#{note.id}"} data-qa={"note-edit-#{note.id}"}
> >
<%= dgettext("actions", "edit") %> <%= dgettext("actions", "edit") %>
</.link> </.link>
<% end %>
<%= if is_owner_or_admin?(note, @current_user) do %>
<.link <.link
href="#" href="#"
phx-click="delete" phx-click="delete"

View File

@ -1,7 +1,7 @@
defmodule MemexWeb.NoteLive.Show do defmodule MemexWeb.NoteLive.Show do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.Notes alias Memex.{Accounts.User, Notes, Notes.Note}
@impl true @impl true
def mount(_params, _session, socket) do 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(:show), do: gettext("show note")
defp page_title(:edit), do: gettext("edit 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 end

View File

@ -19,14 +19,15 @@
</p> </p>
<div class="self-end flex space-x-4"> <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") %> <%= dgettext("actions", "back") %>
</.link> </.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)}> <.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note)}>
<%= dgettext("actions", "edit") %> <%= dgettext("actions", "edit") %>
</.link> </.link>
<% end %>
<%= if is_owner_or_admin?(@note, @current_user) do %>
<button <button
type="button" type="button"
class="btn btn-primary" class="btn btn-primary"

View File

@ -1,6 +1,6 @@
defmodule MemexWeb.PipelineLive.Index do defmodule MemexWeb.PipelineLive.Index do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.{Pipelines, Pipelines.Pipeline} alias Memex.{Accounts.User, Pipelines, Pipelines.Pipeline}
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do def mount(%{"search" => search}, _session, socket) do
@ -77,4 +77,13 @@ defmodule MemexWeb.PipelineLive.Index do
defp display_pipelines(%{assigns: %{search: search}} = socket) do defp display_pipelines(%{assigns: %{search: search}} = socket) do
socket |> assign(pipelines: Pipelines.list_public_pipelines(search)) socket |> assign(pipelines: Pipelines.list_public_pipelines(search))
end end
@spec is_owner_or_admin?(Pipeline.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?(Pipeline.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 end

View File

@ -30,13 +30,15 @@
pipelines={@pipelines} pipelines={@pipelines}
> >
<:actions :let={pipeline}> <:actions :let={pipeline}>
<%= if @current_user do %> <%= if is_owner?(pipeline, @current_user) do %>
<.link <.link
patch={Routes.pipeline_index_path(@socket, :edit, pipeline)} patch={Routes.pipeline_index_path(@socket, :edit, pipeline)}
data-qa={"pipeline-edit-#{pipeline.id}"} data-qa={"pipeline-edit-#{pipeline.id}"}
> >
<%= dgettext("actions", "edit") %> <%= dgettext("actions", "edit") %>
</.link> </.link>
<% end %>
<%= if is_owner_or_admin?(pipeline, @current_user) do %>
<.link <.link
href="#" href="#"
phx-click="delete" phx-click="delete"

View File

@ -1,7 +1,7 @@
defmodule MemexWeb.PipelineLive.Show do defmodule MemexWeb.PipelineLive.Show do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.Pipelines alias Memex.{Accounts.User, Pipelines, Pipelines.Pipeline}
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
@ -38,4 +38,13 @@ defmodule MemexWeb.PipelineLive.Show do
defp page_title(:show), do: gettext("show pipeline") defp page_title(:show), do: gettext("show pipeline")
defp page_title(:edit), do: gettext("edit pipeline") defp page_title(:edit), do: gettext("edit pipeline")
@spec is_owner_or_admin?(Pipeline.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?(Pipeline.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 end

View File

@ -19,14 +19,15 @@
</p> </p>
<div class="self-end flex space-x-4"> <div class="self-end flex space-x-4">
<.link class="btn btn-primary" patch={Routes.pipeline_index_path(@socket, :index)}> <.link class="btn btn-primary" navigate={Routes.pipeline_index_path(@socket, :index)}>
<%= dgettext("actions", "back") %> <%= dgettext("actions", "back") %>
</.link> </.link>
<%= if @current_user do %> <%= if is_owner?(@pipeline, @current_user) do %>
<.link class="btn btn-primary" patch={Routes.pipeline_show_path(@socket, :edit, @pipeline)}> <.link class="btn btn-primary" patch={Routes.pipeline_show_path(@socket, :edit, @pipeline)}>
<%= dgettext("actions", "edit") %> <%= dgettext("actions", "edit") %>
</.link> </.link>
<% end %>
<%= if is_owner_or_admin?(@pipeline, @current_user) do %>
<button <button
type="button" type="button"
class="btn btn-primary" class="btn btn-primary"

View File

@ -67,12 +67,12 @@ msgstr ""
msgid "create invite" msgid "create invite"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:47 #: lib/memex_web/live/context_live/index.html.heex:49
#: lib/memex_web/live/context_live/show.html.heex:37 #: lib/memex_web/live/context_live/show.html.heex:38
#: lib/memex_web/live/note_live/index.html.heex:47 #: lib/memex_web/live/note_live/index.html.heex:49
#: lib/memex_web/live/note_live/show.html.heex:37 #: lib/memex_web/live/note_live/show.html.heex:38
#: lib/memex_web/live/pipeline_live/index.html.heex:47 #: lib/memex_web/live/pipeline_live/index.html.heex:49
#: lib/memex_web/live/pipeline_live/show.html.heex:37 #: lib/memex_web/live/pipeline_live/show.html.heex:38
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "delete" msgid "delete"
msgstr "" msgstr ""
@ -108,17 +108,17 @@ msgstr ""
msgid "log in" msgid "log in"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:56 #: lib/memex_web/live/context_live/index.html.heex:58
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "new context" msgid "new context"
msgstr "" msgstr ""
#: lib/memex_web/live/note_live/index.html.heex:56 #: lib/memex_web/live/note_live/index.html.heex:58
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "new note" msgid "new note"
msgstr "" msgstr ""
#: lib/memex_web/live/pipeline_live/index.html.heex:56 #: lib/memex_web/live/pipeline_live/index.html.heex:58
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "new pipeline" msgid "new pipeline"
msgstr "" msgstr ""

View File

@ -141,12 +141,12 @@ msgstr ""
msgid "are you sure you want to make %{invite_name} unlimited?" msgid "are you sure you want to make %{invite_name} unlimited?"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:44 #: lib/memex_web/live/context_live/index.html.heex:46
#: lib/memex_web/live/context_live/show.html.heex:34 #: lib/memex_web/live/context_live/show.html.heex:35
#: lib/memex_web/live/note_live/index.html.heex:44 #: lib/memex_web/live/note_live/index.html.heex:46
#: lib/memex_web/live/note_live/show.html.heex:34 #: lib/memex_web/live/note_live/show.html.heex:35
#: lib/memex_web/live/pipeline_live/index.html.heex:44 #: lib/memex_web/live/pipeline_live/index.html.heex:46
#: lib/memex_web/live/pipeline_live/show.html.heex:34 #: lib/memex_web/live/pipeline_live/show.html.heex:35
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "are you sure?" msgid "are you sure?"
msgstr "" msgstr ""