Add minimal contact form

This commit is contained in:
Timothée Floure 2021-02-02 18:49:11 +01:00
parent 4683419fdc
commit a9a65adb8c
Signed by: tfloure
GPG key ID: 4502C902C00A1E12
5 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,45 @@
defmodule RecycledCloud.SupportRequest do
use Ecto.Schema
import Ecto.Changeset
import Bamboo.Email
alias RecycledCloud.SupportRequest
alias RecycledCloud.Mailer
embedded_schema do
field :name, :string
field :email, :string
field :message, :string
timestamps()
end
def changeset(key, attrs) do
key
|> cast(attrs, [:name, :email, :message])
|> validate_required([:name, :email, :message])
end
def send(%SupportRequest{} = request) do
admin_email = Application.get_env(:recycledcloud, :admin_email)
host = Application.get_env(:recycledcloud, RecycledCloudWeb.Endpoint)
|> Keyword.get(:url, [])
|> Keyword.get(:host, "localhost")
body = """
A new request has been received on #{host}:
From: #{request.name} // #{request.email}
~~~
#{request.message}
"""
Mailer.template()
|> to(request.email)
|> bcc(admin_email)
|> put_header("Reply-To", request.email)
|> subject("Recycled Cloud Support Request")
|> text_body(body)
|> Mailer.deliver_now()
end
end

View file

@ -0,0 +1,29 @@
defmodule RecycledCloudWeb.SupportController do
use RecycledCloudWeb, :controller
alias RecycledCloud.SupportRequest
def new(conn, _params) do
conn
|> assign(:request_changeset, SupportRequest.changeset(%SupportRequest{}, %{}))
|> render("new.html")
end
def create(conn, %{"support_request" => request} = params) do
action = SupportRequest.changeset(%SupportRequest{}, request)
|> Ecto.Changeset.apply_action(:update)
case action do
{:ok, request} ->
SupportRequest.send(request)
conn
|> put_flash(:info, "Request has been sent. We'll come back to you.")
|> redirect(to: Routes.page_path(conn, :index))
{:error, changeset} ->
conn
|> assign(:request_changeset, changeset)
|> render("new.html")
end
end
end

View file

@ -45,6 +45,9 @@ defmodule RecycledCloudWeb.Router do
post "/users/reset_password", UserResetPasswordController, :create post "/users/reset_password", UserResetPasswordController, :create
get "/users/reset_password/:token", UserResetPasswordController, :edit get "/users/reset_password/:token", UserResetPasswordController, :edit
put "/users/reset_password/:token", UserResetPasswordController, :update put "/users/reset_password/:token", UserResetPasswordController, :update
get "/support/new", SupportController, :new
post "/support/new", SupportController, :create
end end
scope "/", RecycledCloudWeb do scope "/", RecycledCloudWeb do

View file

@ -0,0 +1,26 @@
<h1>New support request</h1>
<%= form_for @request_changeset, Routes.support_path(@conn, :new), fn f -> %>
<%= if @request_changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%= text_input f, :name, placeholder: "Name" %>
<%= error_tag f, :name %>
<br />
<%= email_input f, :email, placeholder: "Email" %>
<%= error_tag f, :email %>
<br />
<%= textarea f, :message, placeholder: "Your message" %>
<%= error_tag f, :message %>
<div>
<%= submit "Send" %>
</div>
<% end %>

View file

@ -0,0 +1,3 @@
defmodule RecycledCloudWeb.SupportView do
use RecycledCloudWeb, :view
end