Implement deleting funnels

pull/2859/head
Adam Rutkowski 4 years ago
parent d7a6eb34ce
commit ab4f1433da

@ -38,6 +38,16 @@ defmodule Plausible.Funnels do
)
end
def delete(%Plausible.Site{id: site_id}, funnel_id) do
Repo.delete_all(
from f in Funnel,
where: f.site_id == ^site_id,
where: f.id == ^funnel_id
)
:ok
end
def get(%Plausible.Site{id: site_id}, by) do
get(site_id, by)
end

@ -63,6 +63,12 @@ defmodule PlausibleWeb.Live.FunnelSettings do
{:noreply, assign(socket, add_funnel?: false)}
end
def handle_event("delete-funnel", %{"funnel-id" => id}, socket) do
id = String.to_integer(id)
:ok = Funnels.delete(socket.assigns.site, id)
{:noreply, assign(socket, funnels: Funnels.list(socket.assigns.site))}
end
def handle_info({:funnel_saved, funnel}, socket) do
{:noreply, assign(socket, add_funnel?: false, funnels: [funnel | socket.assigns.funnels])}
end

@ -16,7 +16,12 @@ defmodule PlausibleWeb.Live.FunnelSettings.List do
<%= funnel.steps_count %>-step funnel
</span>
</span>
<%= button(to: "", method: :delete, class: "text-sm text-red-600", data: [confirm: "Are you sure you want to remove funnel '#{funnel.name}'? This will just affect the UI, all of your analytics data will stay intact."]) do %>
<button
phx-click="delete-funnel"
phx-value-funnel-id={funnel.id}
class="text-sm text-red-600"
data-confirm="Are you sure you want to remove funnel '#{funnel.name}'? This will just affect the UI, all of your analytics data will stay intact."
>
<svg
class="feather feather-sm"
xmlns="http://www.w3.org/2000/svg"
@ -33,7 +38,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.List do
<line x1="10" y1="11" x2="10" y2="17"></line>
<line x1="14" y1="11" x2="14" y2="17"></line>
</svg>
<% end %>
</button>
</div>
<% end %>
</div>

@ -14,6 +14,10 @@ defmodule PlausibleWeb.Router do
plug PlausibleWeb.LastSeenPlug
end
pipeline :liveview do
plug :fetch_live_flash
end
pipeline :shared_link do
plug :accepts, ["html"]
plug :put_secure_browser_headers
@ -148,6 +152,11 @@ defmodule PlausibleWeb.Router do
post "/share/:slug/authenticate", StatsController, :authenticate_shared_link
end
scope "/:website/settings/funnels/", PlausibleWeb do
pipe_through [:browser, :csrf, :liveview]
get "/", SiteController, :settings_funnels
end
scope "/", PlausibleWeb do
pipe_through [:browser, :csrf]
@ -247,8 +256,6 @@ defmodule PlausibleWeb.Router do
get "/:website/settings/visibility", SiteController, :settings_visibility
get "/:website/settings/goals", SiteController, :settings_goals
get "/:website/settings/funnels", SiteController, :settings_funnels
get "/:website/settings/search-console", SiteController, :settings_search_console
get "/:website/settings/email-reports", SiteController, :settings_email_reports
get "/:website/settings/custom-domain", SiteController, :settings_custom_domain

@ -7,7 +7,7 @@
<% end %>
</header>
<script>
</script>
<%= live_render(@conn, PlausibleWeb.Live.FunnelSettings, session: %{"site_id" => @site.id, "domain" => @site.domain}) %>
</div>

@ -54,6 +54,20 @@ defmodule Plausible.FunnelsTest do
assert [%{step_order: 1}, %{step_order: 2}, %{step_order: 3}] = funnel.steps
end
test "funnels can be deleted by id and a site", %{site: site, steps: [g1, g2 | _]} do
{:ok, funnel} =
Funnels.create(
site,
"Sample funnel",
[g1, g2]
)
assert :ok = Funnels.delete(site, funnel.id)
# deletion is idempotent
assert :ok = Funnels.delete(site, funnel.id)
assert {:error, "Funnel not found"} = Funnels.get(site, funnel.id)
end
test "a funnel cannot be made of < 2 steps", %{site: site, steps: [g1 | _]} do
assert {:error, :invalid_funnel_size} =
Funnels.create(

Loading…
Cancel
Save