Refresh Hub's belonged data with push_patch when it changes (#2384)

This commit is contained in:
Alexandre de Souza 2023-12-08 09:49:02 -03:00 committed by GitHub
parent aeef92f564
commit f0979ea492
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 94 deletions

View file

@ -13,13 +13,27 @@ defmodule LivebookWeb.Hub.EditLive do
Hubs.Broadcasts.subscribe([:connection])
end
{:ok, assign(socket, hub: nil, type: nil, page_title: "Hub - Livebook", params: %{})}
{:ok,
assign(socket,
hub: nil,
counter: 0,
type: nil,
page_title: "Hub - Livebook",
params: %{}
)}
end
@impl true
def handle_params(params, _url, socket) do
{id, params} = Map.pop(params, "id")
{:noreply, socket |> load_hub(id) |> assign(:params, params)}
{:noreply,
socket
|> load_hub(id)
# Hub-specific components load data, such as secrets and we use
# a counter to force re-render on every patch.
|> update(:counter, &(&1 + 1))
|> assign(:params, params)}
end
@impl true
@ -30,7 +44,13 @@ defmodule LivebookWeb.Hub.EditLive do
current_user={@current_user}
saved_hubs={@saved_hubs}
>
<.hub_component type={@type} hub={@hub} live_action={@live_action} params={@params} />
<.hub_component
type={@type}
hub={@hub}
counter={@counter}
live_action={@live_action}
params={@params}
/>
</LayoutHelpers.layout>
"""
end
@ -41,6 +61,7 @@ defmodule LivebookWeb.Hub.EditLive do
module={LivebookWeb.Hub.Edit.PersonalComponent}
hub={@hub}
params={@params}
counter={@counter}
live_action={@live_action}
id="personal-form"
/>
@ -54,6 +75,7 @@ defmodule LivebookWeb.Hub.EditLive do
hub={@hub}
live_action={@live_action}
params={@params}
counter={@counter}
id="team-form"
/>
"""
@ -94,6 +116,7 @@ defmodule LivebookWeb.Hub.EditLive do
defp load_hub(socket, id) do
hub = Hubs.fetch_hub!(id)
type = Provider.type(hub)
assign(socket, hub: hub, type: type)
end
end

View file

@ -125,7 +125,7 @@ defmodule LivebookWeb.Hub.FileSystemFormComponent do
{:noreply,
socket
|> put_flash(:success, message)
|> push_redirect(to: socket.assigns.return_to)}
|> push_patch(to: socket.assigns.return_to)}
else
{:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign(socket, changeset: changeset)}
{:transport_error, message} -> {:noreply, assign(socket, error_message: message)}

View file

@ -83,7 +83,7 @@ defmodule LivebookWeb.Hub.FileSystemListComponent do
:ok ->
socket
|> put_flash(:success, "File storage deleted successfully")
|> push_navigate(to: ~p"/hub/#{hub.id}")
|> push_patch(to: ~p"/hub/#{hub.id}")
{:transport_error, reason} ->
put_flash(socket, :error, reason)

View file

@ -96,7 +96,7 @@ defmodule LivebookWeb.Hub.SecretFormComponent do
{:noreply,
socket
|> put_flash(:success, message)
|> push_redirect(to: socket.assigns.return_to)}
|> push_patch(to: socket.assigns.return_to)}
else
{:error, changeset} ->
{:noreply, assign(socket, changeset: Map.replace!(changeset, :action, :validate))}

View file

@ -97,7 +97,7 @@ defmodule LivebookWeb.Hub.SecretListComponent do
:ok ->
socket
|> put_flash(:success, "Secret #{secret.name} deleted successfully")
|> push_navigate(to: attrs["return_to"])
|> push_patch(to: attrs["return_to"])
{:transport_error, reason} ->
put_flash(socket, :error, reason)

View file

@ -103,7 +103,7 @@ defmodule LivebookWeb.Hub.Teams.DeploymentGroupFormComponent do
{:noreply,
socket
|> put_flash(:success, message)
|> push_redirect(to: ~p"/hub/#{socket.assigns.hub.id}/deployment-groups/edit/#{id}")}
|> push_patch(to: ~p"/hub/#{socket.assigns.hub.id}/deployment-groups/edit/#{id}")}
else
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}

View file

@ -82,7 +82,7 @@ defmodule LivebookWeb.Hub.Teams.DeploymentGroupListComponent do
:ok ->
socket
|> put_flash(:success, "Deployment group #{deployment_group.name} deleted successfully")
|> push_navigate(to: ~p"/hub/#{hub.id}")
|> push_patch(to: ~p"/hub/#{hub.id}")
{:transport_error, reason} ->
put_flash(socket, :error, reason)

View file

@ -100,12 +100,8 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
|> render_submit(attrs)
assert_receive {:secret_created, ^secret}
%{"success" => "Secret TEAM_ADD_SECRET added successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret TEAM_ADD_SECRET added successfully"
assert render(element(view, "#hub-secrets-list")) =~ secret.name
assert secret in Livebook.Hubs.get_secrets(hub)
end
@ -148,11 +144,8 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
updated_secret = %{secret | value: new_value}
assert_receive {:secret_updated, ^updated_secret}
%{"success" => "Secret TEAM_EDIT_SECRET updated successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret TEAM_EDIT_SECRET updated successfully"
assert render(element(view, "#hub-secrets-list")) =~ secret.name
assert updated_secret in Livebook.Hubs.get_secrets(hub)
end
@ -174,11 +167,8 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
render_confirm(view)
assert_receive {:secret_deleted, ^secret}
%{"success" => "Secret TEAM_DELETE_SECRET deleted successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret TEAM_DELETE_SECRET deleted successfully"
refute render(element(view, "#hub-secrets-list")) =~ secret.name
refute secret in Livebook.Hubs.get_secrets(hub)
end
@ -221,12 +211,8 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
|> render_submit(attrs)
assert_receive {:file_system_created, %{id: ^id} = file_system}
%{"success" => "File storage added successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "File storage added successfully"
assert render(element(view, "#hub-file-systems-list")) =~ file_system.bucket_url
assert file_system in Livebook.Hubs.get_file_systems(hub)
end
@ -265,13 +251,10 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
|> render_submit(put_in(attrs.file_system.access_key_id, "new key"))
updated_file_system = %{file_system | access_key_id: "new key"}
assert_receive {:file_system_updated, ^updated_file_system}
%{"success" => "File storage updated successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "File storage updated successfully"
assert render(element(view, "#hub-file-systems-list")) =~ file_system.bucket_url
assert updated_file_system in Livebook.Hubs.get_file_systems(hub)
end
@ -297,12 +280,8 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
render_confirm(view)
assert_receive {:file_system_deleted, ^file_system}
%{"success" => "File storage deleted successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "File storage deleted successfully"
refute render(element(view, "#hub-file-systems-list")) =~ file_system.bucket_url
refute file_system in Livebook.Hubs.get_file_systems(hub)
end
@ -347,14 +326,11 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
|> render_submit(attrs)
assert_receive {:deployment_group_created,
%DeploymentGroup{name: "TEAM_ADD_DEPLOYMENT_GROUP"} = deployment_group}
%DeploymentGroup{id: id, name: "TEAM_ADD_DEPLOYMENT_GROUP"} =
deployment_group}
%{"success" => "Deployment group TEAM_ADD_DEPLOYMENT_GROUP added successfully"} =
assert_redirect(view, "/hub/#{hub.id}/deployment-groups/edit/#{deployment_group.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert render(element(view, "#hub-deployment-groups-list")) =~ deployment_group.name
assert_patch(view, "/hub/#{hub.id}/deployment-groups/edit/#{id}")
assert render(view) =~ "Deployment group TEAM_ADD_DEPLOYMENT_GROUP added successfully"
assert deployment_group in Livebook.Teams.get_deployment_groups(hub)
end
@ -408,13 +384,8 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
updated_deployment_group = %{deployment_group | mode: new_mode}
assert_receive {:deployment_group_updated, ^updated_deployment_group}
%{"success" => "Deployment group TEAM_EDIT_DEPLOYMENT_GROUP updated successfully"} =
assert_redirect(view, "/hub/#{hub.id}/deployment-groups/edit/#{deployment_group.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert render(element(view, "#hub-deployment-groups-list")) =~ deployment_group.name
assert_patch(view, "/hub/#{hub.id}/deployment-groups/edit/#{deployment_group.id}")
assert render(view) =~ "Deployment group TEAM_EDIT_DEPLOYMENT_GROUP updated successfully"
assert updated_deployment_group in Livebook.Teams.get_deployment_groups(hub)
end
@ -443,10 +414,8 @@ defmodule LivebookWeb.Integration.Hub.EditLiveTest do
assert_receive {:deployment_group_deleted,
%DeploymentGroup{name: "TEAM_DELETE_DEPLOYMENT_GROUP"}}
%{"success" => "Deployment group TEAM_DELETE_DEPLOYMENT_GROUP deleted successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Deployment group TEAM_DELETE_DEPLOYMENT_GROUP deleted successfully"
refute render(element(view, "#hub-deployment-groups-list")) =~ deployment_group.name
refute deployment_group in Livebook.Teams.get_deployment_groups(hub)
end

View file

@ -81,12 +81,8 @@ defmodule LivebookWeb.Hub.EditLiveTest do
|> render_submit(attrs)
assert_receive {:secret_created, ^secret}
%{"success" => "Secret PERSONAL_ADD_SECRET added successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret PERSONAL_ADD_SECRET added successfully"
assert render(element(view, "#hub-secrets-list")) =~ secret.name
assert secret in Livebook.Hubs.get_secrets(hub)
end
@ -128,11 +124,8 @@ defmodule LivebookWeb.Hub.EditLiveTest do
updated_secret = %{secret | value: new_value}
assert_receive {:secret_updated, ^updated_secret}
%{"success" => "Secret PERSONAL_EDIT_SECRET updated successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret PERSONAL_EDIT_SECRET updated successfully"
assert render(element(view, "#hub-secrets-list")) =~ secret.name
assert updated_secret in Livebook.Hubs.get_secrets(hub)
end
@ -153,11 +146,8 @@ defmodule LivebookWeb.Hub.EditLiveTest do
render_confirm(view)
assert_receive {:secret_deleted, ^secret}
%{"success" => "Secret PERSONAL_DELETE_SECRET deleted successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "Secret PERSONAL_DELETE_SECRET deleted successfully"
refute render(element(view, "#hub-secrets-list")) =~ secret.name
refute secret in Livebook.Hubs.get_secrets(hub)
end
@ -194,12 +184,8 @@ defmodule LivebookWeb.Hub.EditLiveTest do
|> render_submit(attrs)
assert_receive {:file_system_created, ^file_system}
%{"success" => "File storage added successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "File storage added successfully"
assert render(element(view, "#hub-file-systems-list")) =~ file_system.bucket_url
assert file_system in Livebook.Hubs.get_file_systems(hub)
end
@ -235,13 +221,10 @@ defmodule LivebookWeb.Hub.EditLiveTest do
|> render_submit(put_in(attrs.file_system.access_key_id, "new key"))
updated_file_system = %{file_system | access_key_id: "new key"}
assert_receive {:file_system_updated, ^updated_file_system}
%{"success" => "File storage updated successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "File storage updated successfully"
assert render(element(view, "#hub-file-systems-list")) =~ file_system.bucket_url
assert updated_file_system in Livebook.Hubs.get_file_systems(hub)
end
@ -265,11 +248,8 @@ defmodule LivebookWeb.Hub.EditLiveTest do
assert_receive {:file_system_deleted, ^file_system}
%{"success" => "File storage deleted successfully"} =
assert_redirect(view, "/hub/#{hub.id}")
{:ok, view, _html} = live(conn, ~p"/hub/#{hub.id}")
assert_patch(view, "/hub/#{hub.id}")
assert render(view) =~ "File storage deleted successfully"
refute render(element(view, "#hub-file-systems-list")) =~ file_system.bucket_url
refute file_system in Livebook.Hubs.get_file_systems(hub)
end