Skip to content

Commit

Permalink
Add success URL redirect to forms
Browse files Browse the repository at this point in the history
  • Loading branch information
wmnnd committed Jul 31, 2024
1 parent a5be393 commit fbc750e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/keila/contacts/schemas/form_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
12 changes: 10 additions & 2 deletions lib/keila_web/controllers/public_form_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/keila_web/templates/form/edit_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@
<%= text_input(fs, :success_text, class: "text-black") %>
<% end %>
</div>
<div class="flex flex-col">
<%= label(fs, :success_text, gettext("Success redirect URL")) %>
<%= with_validation(fs, :success_url) do %>
<%= url_input(fs, :success_url, class: "text-black") %>
<% end %>
</div>
<div class="flex flex-col">
<%= label(fs, :fine_print, gettext("Fine print")) %>
<%= with_validation(fs, :fine_print) do %>
Expand Down Expand Up @@ -390,7 +396,7 @@
<div class="text-xs">
<%= gettext_md("""
You may use Liquid and Markdown in the email body.
Use `[Confirm]({{ double_opt_in_link }})` to include the opt-in link.
""") %>
</div>
Expand Down
14 changes: 14 additions & 0 deletions test/keila_web/controllers/public_form_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ defmodule KeilaWeb.PublicFormControllerTest do
assert html_response(conn, 400) =~ ~r{can&#39;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
Expand Down

0 comments on commit fbc750e

Please sign in to comment.