41 lines
1.2 KiB
Elixir
41 lines
1.2 KiB
Elixir
defmodule MetaWeb.SupportController do
|
|
use MetaWeb, :controller
|
|
|
|
alias Meta.SupportRequest
|
|
alias Meta.Captcha
|
|
|
|
def new(conn, _params) do
|
|
{captcha_text, captcha_result} = Captcha.generate()
|
|
|
|
conn
|
|
|> assign(:request_changeset, SupportRequest.changeset(%SupportRequest{}, %{}))
|
|
|> assign(:captcha, captcha_text)
|
|
|> put_session(:captcha, captcha_result)
|
|
|> render("new.html")
|
|
end
|
|
|
|
def create(conn, %{"support_request" => raw} = params) do
|
|
attrs = raw |> Map.put("expected_captcha", get_session(conn, :captcha))
|
|
action = SupportRequest.changeset(%SupportRequest{}, attrs)
|
|
|> Ecto.Changeset.apply_action(:update)
|
|
|
|
case action do
|
|
{:ok, request} ->
|
|
# FIXME: check for error?
|
|
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} ->
|
|
{captcha_text, captcha_result} = Captcha.generate()
|
|
|
|
conn
|
|
|> assign(:request_changeset, changeset)
|
|
|> assign(:captcha, captcha_text)
|
|
|> put_session(:captcha, captcha_result)
|
|
|> render("new.html")
|
|
end
|
|
end
|
|
end
|