diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index 3ec6ce7d6..7a8c531a6 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -393,6 +393,17 @@ defmodule PlausibleWeb.SiteController do end end + def delete_custom_domain(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + |> Repo.preload(:custom_domain) + + Repo.delete!(site.custom_domain) + + conn + |> put_flash(:success, "Custom domain deleted succesfully") + |> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings") + end + defp insert_site(user_id, params) do site_changeset = Plausible.Site.changeset(%Plausible.Site{}, params) diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index 394842998..c399e16e2 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -138,6 +138,7 @@ defmodule PlausibleWeb.Router do get "/sites/:website/custom-domains/dns-setup", SiteController, :custom_domain_dns_setup get "/sites/:website/custom-domains/snippet", SiteController, :custom_domain_snippet post "/sites/:website/custom-domains", SiteController, :add_custom_domain + delete "/sites/:website/custom-domains/:id", SiteController, :delete_custom_domain get "/sites/:website/weekly-report/unsubscribe", UnsubscribeController, :weekly_report get "/sites/:website/monthly-report/unsubscribe", UnsubscribeController, :monthly_report diff --git a/lib/plausible_web/templates/site/settings.html.eex b/lib/plausible_web/templates/site/settings.html.eex index cd2439dcf..0eb1125b8 100644 --- a/lib/plausible_web/templates/site/settings.html.eex +++ b/lib/plausible_web/templates/site/settings.html.eex @@ -211,6 +211,7 @@
<%= if @site.custom_domain do %> Configured domain: <%= @site.custom_domain.domain %> + <%= link("Remove custom domain", to: "/sites/#{URI.encode_www_form(@site.domain)}/custom-domains/#{@site.custom_domain.id}", class: "inline-block mt-4 px-4 py-2 border border-gray-300 text-sm leading-5 font-medium rounded-md text-red-700 bg-white hover:text-red-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150", method: "delete") %> <% else %> <%= link("Add custom domain", to: "/sites/#{URI.encode_www_form(@site.domain)}/custom-domains/new", class: "button") %> <% end %> diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index c3366e381..eeda8fc22 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -408,4 +408,16 @@ defmodule PlausibleWeb.SiteControllerTest do assert html_response(conn, 200) =~ "DNS for #{domain.domain}" end end + + describe "DELETE sites/:website/custom-domains/:id" do + setup [:create_user, :log_in, :create_site] + + test "lists goals for the site", %{conn: conn, site: site} do + domain = insert(:custom_domain, site: site) + + delete(conn, "/sites/#{site.domain}/custom-domains/#{domain.id}") + + assert Repo.aggregate(Plausible.Site.CustomDomain, :count, :id) == 0 + end + end end