Remove delete prompt for empty sections (#829)

* Remove delete prompt for empty sections

If a section has no cell views or only empty cell views, avoid
prompting the user to delete the section and just go ahead and delete
it.

Closes #800

* Move delete prompt logic to SessionLive

This avoids creating two separate paths in the view for displaying
delete buttons (triggering either a prompt, or deleting the empty
section).

Instead, the `delete_section` callback is always triggered, and the
"display prompt" logic is kept here.

Couple of things I'm unsure about so will discuss on the PR.

See https://github.com/livebook-dev/livebook/pull/829#discussion_r775560671

* Check only against empty cell list in `delete_section`

* Fix indentation

* Handle section not existing on deletion

* Match empty cell list in case expression

Also explicitly set the section and then re-use it. I think this is a
bit nicer than just matching against the empty list since it matches
the following match too
This commit is contained in:
Lee Jarvis 2021-12-27 17:42:27 +00:00 committed by GitHub
parent 91dcb97456
commit 4bacba6b1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View file

@ -664,6 +664,31 @@ defmodule LivebookWeb.SessionLive do
{:noreply, socket}
end
def handle_event("delete_section", %{"section_id" => section_id}, socket) do
socket =
case Notebook.fetch_section(socket.private.data.notebook, section_id) do
{:ok, %{cells: []} = section} ->
Session.delete_section(socket.assigns.session.pid, section.id, true)
socket
{:ok, section} ->
push_patch(socket,
to:
Routes.session_path(
socket,
:delete_section,
socket.assigns.session.id,
section.id
)
)
:error ->
socket
end
{:noreply, socket}
end
def handle_event("queue_cell_evaluation", %{"cell_id" => cell_id}, socket) do
Session.queue_cell_evaluation(socket.assigns.session.pid, cell_id)

View file

@ -77,12 +77,12 @@ defmodule LivebookWeb.SessionLive.SectionComponent do
{if @section_view.has_children?,
do: [class: "tooltip left", data_tooltip: "Cannot delete this section because\nother sections branch from it"],
else: [class: "tooltip top", data_tooltip: "Delete"]}>
<%= live_patch to: Routes.session_path(@socket, :delete_section, @session_id, @section_view.id),
class: "icon-button #{if @section_view.has_children?, do: "disabled"}",
aria_label: "delete section",
role: "button" do %>
<button class={"icon-button #{if @section_view.has_children?, do: "disabled"}"}
aria-label="delete section"
phx-click="delete_section"
phx-value-section_id={@section_view.id}>
<.remix_icon icon="delete-bin-6-line" class="text-xl" />
<% end %>
</button>
</span>
</div>
</div>