Skip to content

Commit

Permalink
Installation: don't crash when socket reconnects and finds no changes (
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol authored Sep 3, 2024
1 parent 6c7f123 commit acefd79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/plausible_web/live/installation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,15 @@ defmodule PlausibleWeb.Live.Installation do

flash = snippet_change_flash(socket.assigns.script_config, new_params)

socket =
if flash do
put_live_flash(socket, :success, flash)
else
socket
end

socket = update_uri_params(socket, new_params)
{:noreply, put_live_flash(socket, :success, flash)}
{:noreply, socket}
end

def handle_params(params, _uri, socket) do
Expand Down Expand Up @@ -518,13 +525,14 @@ defmodule PlausibleWeb.Live.Installation do

defp snippet_change_flash(old_config, new_config) do
change =
Enum.find(new_config, fn {key, value} ->
if old_config[key] != new_config[key] do
{key, value}
end
Enum.find(new_config, fn {key, _value} ->
old_config[key] != new_config[key]
end)

case change do
nil ->
nil

{k, false} when k in ["outbound-links", "file-downloads", "404"] ->
"Snippet updated and goal deleted. Please insert the newest snippet into your site"

Expand Down
14 changes: 14 additions & 0 deletions test/plausible_web/live/installation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ defmodule PlausibleWeb.Live.InstallationTest do

assert render(lv) =~ "Snippet updated. Please insert the newest snippet into your site"
end

test "no changes", %{conn: conn, site: site} do
{lv, _html} = get_lv(conn, site, "?installation_type=manual")

lv
|> element(~s|form#snippet-form|)
|> render_change(%{})

lv
|> element(~s|form#snippet-form|)
|> render_change(%{})

refute render(lv) =~ "Snippet updated"
end
end

defp stub_fetch_body(f) when is_function(f, 1) do
Expand Down

0 comments on commit acefd79

Please sign in to comment.