diff --git a/lib/keila/contacts/schemas/form_settings.ex b/lib/keila/contacts/schemas/form_settings.ex index 1bb994c6..53379252 100644 --- a/lib/keila/contacts/schemas/form_settings.ex +++ b/lib/keila/contacts/schemas/form_settings.ex @@ -22,6 +22,7 @@ defmodule Keila.Contacts.Form.Settings do field(:input_border_color, :string, default: "#6b7280") field(:input_text_color, :string, default: "#111827") field(:success_text, :string) + field(:success_url, :string) end def changeset(struct \\ %__MODULE__{}, params) do @@ -43,7 +44,8 @@ defmodule Keila.Contacts.Form.Settings do :submit_label, :submit_bg_color, :submit_text_color, - :success_text + :success_text, + :success_url ]) end end diff --git a/lib/keila_web/controllers/public_form_controller.ex b/lib/keila_web/controllers/public_form_controller.ex index f81f6e88..a1b6c0c3 100644 --- a/lib/keila_web/controllers/public_form_controller.ex +++ b/lib/keila_web/controllers/public_form_controller.ex @@ -33,7 +33,8 @@ defmodule KeilaWeb.PublicFormController do {:ok, contact = %Contact{}} -> data = if form.settings.captcha_required, do: %{"captcha" => true}, else: %{} Tracking.log_event("subscribe", contact.id, data) - render(conn, "success.html") + + render_success_or_redirect(conn) {:ok, form_params = %FormParams{}} -> conn @@ -82,7 +83,7 @@ defmodule KeilaWeb.PublicFormController do Tracking.log_event("subscribe", id, data) Contacts.delete_form_params(form_params.id) - render(conn, "success.html") + render_success_or_redirect(conn) {:ok, form_params = %FormParams{}} -> conn @@ -94,6 +95,13 @@ defmodule KeilaWeb.PublicFormController do end end + defp render_success_or_redirect(conn) do + case conn.assigns.form.settings.success_url do + url when url not in [nil, ""] -> redirect(conn, external: url) + _other -> render(conn, "success.html") + end + end + def cancel_double_opt_in(conn, %{"hmac" => hmac}) do form = conn.assigns.form form_params = conn.assigns.form_params diff --git a/lib/keila_web/templates/form/edit_live.html.heex b/lib/keila_web/templates/form/edit_live.html.heex index b2a41245..6993627e 100644 --- a/lib/keila_web/templates/form/edit_live.html.heex +++ b/lib/keila_web/templates/form/edit_live.html.heex @@ -107,6 +107,12 @@ <%= text_input(fs, :success_text, class: "text-black") %> <% end %> +
+ <%= label(fs, :success_text, gettext("Success redirect URL")) %> + <%= with_validation(fs, :success_url) do %> + <%= url_input(fs, :success_url, class: "text-black") %> + <% end %> +
<%= label(fs, :fine_print, gettext("Fine print")) %> <%= with_validation(fs, :fine_print) do %> @@ -390,7 +396,7 @@
<%= gettext_md(""" You may use Liquid and Markdown in the email body. - + Use `[Confirm]({{ double_opt_in_link }})` to include the opt-in link. """) %>
diff --git a/test/keila_web/controllers/public_form_controller_test.exs b/test/keila_web/controllers/public_form_controller_test.exs index 3ce9e2d4..6ee51375 100644 --- a/test/keila_web/controllers/public_form_controller_test.exs +++ b/test/keila_web/controllers/public_form_controller_test.exs @@ -34,6 +34,20 @@ defmodule KeilaWeb.PublicFormControllerTest do assert html_response(conn, 400) =~ ~r{can't be blank} assert [] == Contacts.get_project_contacts(project.id) end + + test "redirects if settings.success_url is set", %{conn: conn} do + {conn, project} = with_login_and_project(conn) + + form = + insert!(:contacts_form, + project_id: project.id, + settings: %{captcha_required: false, success_url: "https://example.com"} + ) + + params = params(:contact, project_id: project.id) + conn = post(conn, Routes.public_form_path(conn, :show, form.id), contact: params) + assert redirected_to(conn, 302) == "https://example.com" + end end describe "GET /unsubscribe/:p_id/:c_id" do