Implements delete hub button (#1705)

This commit is contained in:
Alexandre de Souza 2023-02-13 01:52:25 -03:00 committed by GitHub
parent 8c9c3ce94d
commit d0e83dc288
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 3 deletions

View file

@ -88,7 +88,10 @@ defmodule Livebook.Hubs do
struct
end
@doc false
@doc """
Deletes a hub with the given id.
"""
@spec delete_hub(String.t()) :: :ok
def delete_hub(id) do
with {:ok, hub} <- get_hub(id) do
:ok = Broadcasts.hub_changed()

View file

@ -44,8 +44,23 @@ defmodule LivebookWeb.Hub.EditLive do
saved_hubs={@saved_hubs}
>
<div class="p-4 md:px-12 md:py-7 max-w-screen-md mx-auto space-y-8">
<div>
<div class="flex relative">
<PageHelpers.title text="Edit Hub" socket={@socket} />
<button
phx-click={
with_confirm(
JS.push("delete_hub", value: %{id: @hub.id}),
title: "Delete hub",
description: "Are you sure you want to delete this hub?",
confirm_text: "Delete",
confirm_icon: "close-circle-line"
)
}
class="absolute right-0 button-base bg-red-500"
>
Delete hub
</button>
</div>
<%= if @type == "fly" do %>
@ -69,4 +84,14 @@ defmodule LivebookWeb.Hub.EditLive do
</LayoutHelpers.layout>
"""
end
@impl true
def handle_event("delete_hub", %{"id" => id}, socket) do
Hubs.delete_hub(id)
{:noreply,
socket
|> put_flash(:success, "Hub deleted successfully")
|> push_redirect(to: "/")}
end
end

View file

@ -264,7 +264,7 @@ defmodule LivebookWeb.LayoutHelpers do
"h-7 flex items-center hover:text-white #{text_color} border-l-4 #{border_color} hover:border-white"
if tooltip = Provider.connection_error(hub) do
[to: to, data_tooltip: tooltip, class: "tooltip top " <> class]
[to: to, data_tooltip: tooltip, class: "tooltip right " <> class]
else
[to: to, class: class]
end

View file

@ -63,6 +63,30 @@ defmodule LivebookWeb.Hub.EditLiveTest do
refute Hubs.fetch_hub!(hub.id) == hub
end
test "deletes hub", %{conn: conn, bypass: bypass} do
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end)
app_id = Livebook.Utils.random_short_id()
hub_id = "fly-#{app_id}"
hub = insert_hub(:fly, id: hub_id, application_id: app_id)
fly_bypass(bypass, app_id, pid)
{:ok, view, html} = live(conn, Routes.hub_path(conn, :edit, hub.id))
assert {:ok, view, _html} =
view
|> render_click("delete_hub", %{"id" => hub_id})
|> follow_redirect(conn)
hubs_html = view |> element("#hubs") |> render()
refute hubs_html =~ hub.hub_emoji
refute hubs_html =~ Routes.hub_path(conn, :edit, hub.id)
refute hubs_html =~ hub.hub_name
assert Hubs.get_hub(hub_id) == :error
end
test "add env var", %{conn: conn, bypass: bypass} do
{:ok, pid} = Agent.start(fn -> %{fun: &fetch_app_response/2, type: :mount} end)