Skip to content

Commit

Permalink
Add custom double opt-in message + double opt-in redirect URL
Browse files Browse the repository at this point in the history
  • Loading branch information
wmnnd committed Aug 20, 2024
1 parent bca3105 commit 1ed442b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 37 deletions.
4 changes: 4 additions & 0 deletions lib/keila/contacts/schemas/form_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule Keila.Contacts.Form.Settings do
field(:double_opt_in_required, :boolean, default: false)
field(:double_opt_in_subject, :string)
field(:double_opt_in_markdown_body, :string)
field(:double_opt_in_message, :string)
field(:double_opt_in_url, :string)
field(:csrf_disabled, :boolean, default: true)
field(:intro_text, :string)
field(:fine_print, :string)
Expand All @@ -32,6 +34,8 @@ defmodule Keila.Contacts.Form.Settings do
:double_opt_in_required,
:double_opt_in_subject,
:double_opt_in_markdown_body,
:double_opt_in_message,
:double_opt_in_url,
:csrf_disabled,
:intro_text,
:fine_print,
Expand Down
11 changes: 9 additions & 2 deletions lib/keila_web/controllers/public_form_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule KeilaWeb.PublicFormController do
{:ok, form_params = %FormParams{}} ->
conn
|> assign(:email, form_params.params[:email])
|> render("double_opt_in_required.html")
|> render_double_opt_in_required_or_redirect()

{:error, changeset} ->
render_form(conn, 400, changeset, form)
Expand Down Expand Up @@ -88,7 +88,7 @@ defmodule KeilaWeb.PublicFormController do
{:ok, form_params = %FormParams{}} ->
conn
|> assign(:email, form_params.params[:email])
|> render("double_opt_in_required.html")
|> render_double_opt_in_required_or_redirect()

{:error, changeset} ->
render_form(conn, 400, changeset, form)
Expand All @@ -102,6 +102,13 @@ defmodule KeilaWeb.PublicFormController do
end
end

defp render_double_opt_in_required_or_redirect(conn) do
case conn.assigns.form.settings.double_opt_in_url do
url when url not in [nil, ""] -> redirect(conn, external: url)
_other -> render(conn, "double_opt_in_required.html")
end
end

def cancel_double_opt_in(conn, %{"hmac" => hmac}) do
form = conn.assigns.form
form_params = conn.assigns.form_params
Expand Down
58 changes: 44 additions & 14 deletions lib/keila_web/templates/form/edit_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,6 @@
<%= text_input(fs, :intro_text, class: "text-black") %>
<% end %>
</div>
<div class="flex flex-col">
<%= label(fs, :success_text, gettext("Thank you message")) %>
<%= with_validation(fs, :success_text) do %>
<%= 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 All @@ -125,6 +113,18 @@
<%= gettext("Require captcha") %>
<% end %>
<% end %>
<div class="flex flex-col">
<%= label(fs, :success_text, gettext("Thank you message")) %>
<%= with_validation(fs, :success_text) do %>
<%= text_input(fs, :success_text, class: "text-black") %>
<% end %>
</div>
<div class="flex flex-col">
<%= label(fs, :success_text, gettext("Thank you redirect URL")) %>
<%= with_validation(fs, :success_url) do %>
<%= url_input(fs, :success_url, class: "text-black") %>
<% end %>
</div>
<%= with_validation(fs, :csrf_disabled) do %>
<%= label(fs, :csrf_disabled) do %>
<%= checkbox(fs, :csrf_disabled, class: "text-emerald-500") %>
Expand Down Expand Up @@ -392,7 +392,7 @@

<template x-if="enable">
<div class="form-row">
<%= label(fs, :double_opt_in_subject, "Custom email text") %>
<%= label(fs, :double_opt_in_markdown_body, "Custom email text") %>
<div class="text-xs">
<%= gettext_md("""
You may use Liquid and Markdown in the email body.
Expand All @@ -401,7 +401,7 @@
""") %>
</div>
<br />
<%= with_validation(fs, :double_opt_in_text_body) do %>
<%= with_validation(fs, :double_opt_in_markdown_body) do %>
<%= textarea(fs, :double_opt_in_markdown_body,
class: "text-black",
rows: 6,
Expand All @@ -415,6 +415,36 @@
<% end %>
</div>
</template>

<template x-if="enable">
<div class="form-row">
<%= label(fs, :double_opt_in_message, gettext("Double Opt-in Info Message")) %>
<%= with_validation(fs, :double_opt_in_message) do %>
<div class="text-xs">
<%= gettext_md("""
Custom message that is shown when a user submits the form and double opt-in
is required.
""") %>
</div>
<%= text_input(fs, :double_opt_in_message, class: "text-black") %>
<% end %>
</div>
</template>

<template x-if="enable">
<div class="form-row">
<%= label(fs, :success_text, gettext("Double Opt-in Info Redirect URL")) %>
<%= with_validation(fs, :double_opt_in_url) do %>
<div class="text-xs">
<%= gettext_md("""
Custom URL to which users are redirected when they submit the form and double opt-in
is required.
""") %>
</div>
<%= url_input(fs, :double_opt_in_url, class: "text-black") %>
<% end %>
</div>
</template>
<% end) %>
</div>
<% else %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
<div style={form_style_attr(@form)} class={form_class_attr(@form)}>
<%= render_h1(@form) %>
<h2 class="text-xl">
<%= gettext("Please confirm your email") %>
</h2>
<p>
<%= gettext(
"We’ve just sent an email to %{email}. Please click the link in that email to confirm your subscription.",
email: @email
) %>
</p>
<%= render_fine_print(@form) %>
</div>
<%= render_form_double_opt_in_required(@form, @email) %>
38 changes: 30 additions & 8 deletions lib/keila_web/views/public_form_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ defmodule KeilaWeb.PublicFormView do

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

defp form_class_attr(_form) do
@form_classes
end

defp form_style_attr(form) do
build_form_styles(form)
end

defp input_styles(form) do
build_styles(%{
"background-color" => form.settings.input_bg_color,
Expand Down Expand Up @@ -276,10 +268,40 @@ defmodule KeilaWeb.PublicFormView do
end
end

def render_form_double_opt_in_required(form, email) do
content_tag(:div, class: @form_classes, style: build_form_styles(form)) do
[
render_h1(form),
render_double_opt_in_required(form, email),
render_fine_print(form)
]
end
end

defp render_success(form) do
content_tag(:div, form.settings.success_text || gettext("Thank you!"), class: "text-xl")
end

defp render_double_opt_in_required(form, email) do
case form.settings.double_opt_in_message do
message when message not in [nil, ""] ->
content_tag(:p, message)

_other ->
[
content_tag(:h2, class: "text-xl") do
gettext("Please confirm your email")
end,
content_tag(:p) do
gettext(
"We’ve just sent an email to %{email}. Please click the link in that email to confirm your subscription.",
email: email
)
end
]
end
end

def render_unsubscribe_form(form) do
form_styles = build_form_styles(form)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ defmodule KeilaWeb.PublicFormControllerDoubleOptInTest do
args: %{"form_params_id" => form_params.id}
)
end

test "redirects to double_opt_in_url if set", %{conn: conn} do
{conn, project} = with_login_and_project(conn)

form =
insert!(:contacts_form,
project_id: project.id,
settings: %{
captcha_required: false,
double_opt_in_required: true,
double_opt_in_url: "https://example.com"
}
)

params = params(:contact) |> Map.delete(:project_id)
conn = post(conn, Routes.public_form_path(conn, :show, form.id), contact: params)
assert redirected_to(conn, 302) == form.settings.double_opt_in_url
end
end

@tag :double_opt_in
Expand Down

0 comments on commit 1ed442b

Please sign in to comment.