Add minimal contact form
This commit is contained in:
parent
4683419fdc
commit
a9a65adb8c
5 changed files with 106 additions and 0 deletions
45
lib/recycledcloud/support_request.ex
Normal file
45
lib/recycledcloud/support_request.ex
Normal 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
|
29
lib/recycledcloud_web/controllers/support_controller.ex
Normal file
29
lib/recycledcloud_web/controllers/support_controller.ex
Normal 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
|
|
@ -45,6 +45,9 @@ defmodule RecycledCloudWeb.Router do
|
|||
post "/users/reset_password", UserResetPasswordController, :create
|
||||
get "/users/reset_password/:token", UserResetPasswordController, :edit
|
||||
put "/users/reset_password/:token", UserResetPasswordController, :update
|
||||
|
||||
get "/support/new", SupportController, :new
|
||||
post "/support/new", SupportController, :create
|
||||
end
|
||||
|
||||
scope "/", RecycledCloudWeb do
|
||||
|
|
26
lib/recycledcloud_web/templates/support/new.html.eex
Normal file
26
lib/recycledcloud_web/templates/support/new.html.eex
Normal 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 %>
|
3
lib/recycledcloud_web/views/support_view.ex
Normal file
3
lib/recycledcloud_web/views/support_view.ex
Normal file
|
@ -0,0 +1,3 @@
|
|||
defmodule RecycledCloudWeb.SupportView do
|
||||
use RecycledCloudWeb, :view
|
||||
end
|
Loading…
Reference in a new issue