Skip to content

Commit 1ed442b

Browse files
committed
Add custom double opt-in message + double opt-in redirect URL
1 parent bca3105 commit 1ed442b

File tree

6 files changed

+106
-37
lines changed

6 files changed

+106
-37
lines changed

lib/keila/contacts/schemas/form_settings.ex

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule Keila.Contacts.Form.Settings do
99
field(:double_opt_in_required, :boolean, default: false)
1010
field(:double_opt_in_subject, :string)
1111
field(:double_opt_in_markdown_body, :string)
12+
field(:double_opt_in_message, :string)
13+
field(:double_opt_in_url, :string)
1214
field(:csrf_disabled, :boolean, default: true)
1315
field(:intro_text, :string)
1416
field(:fine_print, :string)
@@ -32,6 +34,8 @@ defmodule Keila.Contacts.Form.Settings do
3234
:double_opt_in_required,
3335
:double_opt_in_subject,
3436
:double_opt_in_markdown_body,
37+
:double_opt_in_message,
38+
:double_opt_in_url,
3539
:csrf_disabled,
3640
:intro_text,
3741
:fine_print,

lib/keila_web/controllers/public_form_controller.ex

+9-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule KeilaWeb.PublicFormController do
3939
{:ok, form_params = %FormParams{}} ->
4040
conn
4141
|> assign(:email, form_params.params[:email])
42-
|> render("double_opt_in_required.html")
42+
|> render_double_opt_in_required_or_redirect()
4343

4444
{:error, changeset} ->
4545
render_form(conn, 400, changeset, form)
@@ -88,7 +88,7 @@ defmodule KeilaWeb.PublicFormController do
8888
{:ok, form_params = %FormParams{}} ->
8989
conn
9090
|> assign(:email, form_params.params[:email])
91-
|> render("double_opt_in_required.html")
91+
|> render_double_opt_in_required_or_redirect()
9292

9393
{:error, changeset} ->
9494
render_form(conn, 400, changeset, form)
@@ -102,6 +102,13 @@ defmodule KeilaWeb.PublicFormController do
102102
end
103103
end
104104

105+
defp render_double_opt_in_required_or_redirect(conn) do
106+
case conn.assigns.form.settings.double_opt_in_url do
107+
url when url not in [nil, ""] -> redirect(conn, external: url)
108+
_other -> render(conn, "double_opt_in_required.html")
109+
end
110+
end
111+
105112
def cancel_double_opt_in(conn, %{"hmac" => hmac}) do
106113
form = conn.assigns.form
107114
form_params = conn.assigns.form_params

lib/keila_web/templates/form/edit_live.html.heex

+44-14
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,6 @@
101101
<%= text_input(fs, :intro_text, class: "text-black") %>
102102
<% end %>
103103
</div>
104-
<div class="flex flex-col">
105-
<%= label(fs, :success_text, gettext("Thank you message")) %>
106-
<%= with_validation(fs, :success_text) do %>
107-
<%= text_input(fs, :success_text, class: "text-black") %>
108-
<% end %>
109-
</div>
110-
<div class="flex flex-col">
111-
<%= label(fs, :success_text, gettext("Success redirect URL")) %>
112-
<%= with_validation(fs, :success_url) do %>
113-
<%= url_input(fs, :success_url, class: "text-black") %>
114-
<% end %>
115-
</div>
116104
<div class="flex flex-col">
117105
<%= label(fs, :fine_print, gettext("Fine print")) %>
118106
<%= with_validation(fs, :fine_print) do %>
@@ -125,6 +113,18 @@
125113
<%= gettext("Require captcha") %>
126114
<% end %>
127115
<% end %>
116+
<div class="flex flex-col">
117+
<%= label(fs, :success_text, gettext("Thank you message")) %>
118+
<%= with_validation(fs, :success_text) do %>
119+
<%= text_input(fs, :success_text, class: "text-black") %>
120+
<% end %>
121+
</div>
122+
<div class="flex flex-col">
123+
<%= label(fs, :success_text, gettext("Thank you redirect URL")) %>
124+
<%= with_validation(fs, :success_url) do %>
125+
<%= url_input(fs, :success_url, class: "text-black") %>
126+
<% end %>
127+
</div>
128128
<%= with_validation(fs, :csrf_disabled) do %>
129129
<%= label(fs, :csrf_disabled) do %>
130130
<%= checkbox(fs, :csrf_disabled, class: "text-emerald-500") %>
@@ -392,7 +392,7 @@
392392

393393
<template x-if="enable">
394394
<div class="form-row">
395-
<%= label(fs, :double_opt_in_subject, "Custom email text") %>
395+
<%= label(fs, :double_opt_in_markdown_body, "Custom email text") %>
396396
<div class="text-xs">
397397
<%= gettext_md("""
398398
You may use Liquid and Markdown in the email body.
@@ -401,7 +401,7 @@
401401
""") %>
402402
</div>
403403
<br />
404-
<%= with_validation(fs, :double_opt_in_text_body) do %>
404+
<%= with_validation(fs, :double_opt_in_markdown_body) do %>
405405
<%= textarea(fs, :double_opt_in_markdown_body,
406406
class: "text-black",
407407
rows: 6,
@@ -415,6 +415,36 @@
415415
<% end %>
416416
</div>
417417
</template>
418+
419+
<template x-if="enable">
420+
<div class="form-row">
421+
<%= label(fs, :double_opt_in_message, gettext("Double Opt-in Info Message")) %>
422+
<%= with_validation(fs, :double_opt_in_message) do %>
423+
<div class="text-xs">
424+
<%= gettext_md("""
425+
Custom message that is shown when a user submits the form and double opt-in
426+
is required.
427+
""") %>
428+
</div>
429+
<%= text_input(fs, :double_opt_in_message, class: "text-black") %>
430+
<% end %>
431+
</div>
432+
</template>
433+
434+
<template x-if="enable">
435+
<div class="form-row">
436+
<%= label(fs, :success_text, gettext("Double Opt-in Info Redirect URL")) %>
437+
<%= with_validation(fs, :double_opt_in_url) do %>
438+
<div class="text-xs">
439+
<%= gettext_md("""
440+
Custom URL to which users are redirected when they submit the form and double opt-in
441+
is required.
442+
""") %>
443+
</div>
444+
<%= url_input(fs, :double_opt_in_url, class: "text-black") %>
445+
<% end %>
446+
</div>
447+
</template>
418448
<% end) %>
419449
</div>
420450
<% else %>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
<div style={form_style_attr(@form)} class={form_class_attr(@form)}>
2-
<%= render_h1(@form) %>
3-
<h2 class="text-xl">
4-
<%= gettext("Please confirm your email") %>
5-
</h2>
6-
<p>
7-
<%= gettext(
8-
"We’ve just sent an email to %{email}. Please click the link in that email to confirm your subscription.",
9-
email: @email
10-
) %>
11-
</p>
12-
<%= render_fine_print(@form) %>
13-
</div>
1+
<%= render_form_double_opt_in_required(@form, @email) %>

lib/keila_web/views/public_form_view.ex

+30-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ defmodule KeilaWeb.PublicFormView do
77

88
@form_classes "contact-form container bg-white rounded py-4 md:py-8 flex flex-col gap-4"
99

10-
defp form_class_attr(_form) do
11-
@form_classes
12-
end
13-
14-
defp form_style_attr(form) do
15-
build_form_styles(form)
16-
end
17-
1810
defp input_styles(form) do
1911
build_styles(%{
2012
"background-color" => form.settings.input_bg_color,
@@ -276,10 +268,40 @@ defmodule KeilaWeb.PublicFormView do
276268
end
277269
end
278270

271+
def render_form_double_opt_in_required(form, email) do
272+
content_tag(:div, class: @form_classes, style: build_form_styles(form)) do
273+
[
274+
render_h1(form),
275+
render_double_opt_in_required(form, email),
276+
render_fine_print(form)
277+
]
278+
end
279+
end
280+
279281
defp render_success(form) do
280282
content_tag(:div, form.settings.success_text || gettext("Thank you!"), class: "text-xl")
281283
end
282284

285+
defp render_double_opt_in_required(form, email) do
286+
case form.settings.double_opt_in_message do
287+
message when message not in [nil, ""] ->
288+
content_tag(:p, message)
289+
290+
_other ->
291+
[
292+
content_tag(:h2, class: "text-xl") do
293+
gettext("Please confirm your email")
294+
end,
295+
content_tag(:p) do
296+
gettext(
297+
"We’ve just sent an email to %{email}. Please click the link in that email to confirm your subscription.",
298+
email: email
299+
)
300+
end
301+
]
302+
end
303+
end
304+
283305
def render_unsubscribe_form(form) do
284306
form_styles = build_form_styles(form)
285307

test/keila_web/controllers/public_form_controller_double_opt_in_test.exs

+18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ defmodule KeilaWeb.PublicFormControllerDoubleOptInTest do
2929
args: %{"form_params_id" => form_params.id}
3030
)
3131
end
32+
33+
test "redirects to double_opt_in_url if set", %{conn: conn} do
34+
{conn, project} = with_login_and_project(conn)
35+
36+
form =
37+
insert!(:contacts_form,
38+
project_id: project.id,
39+
settings: %{
40+
captcha_required: false,
41+
double_opt_in_required: true,
42+
double_opt_in_url: "https://example.com"
43+
}
44+
)
45+
46+
params = params(:contact) |> Map.delete(:project_id)
47+
conn = post(conn, Routes.public_form_path(conn, :show, form.id), contact: params)
48+
assert redirected_to(conn, 302) == form.settings.double_opt_in_url
49+
end
3250
end
3351

3452
@tag :double_opt_in

0 commit comments

Comments
 (0)