2021-04-12 15:44:12 +02:00
|
|
|
defmodule MetaWeb.SupportController do
|
|
|
|
use MetaWeb, :controller
|
2021-02-02 18:49:11 +01:00
|
|
|
|
2021-04-12 15:44:12 +02:00
|
|
|
alias Meta.SupportRequest
|
|
|
|
alias Meta.Captcha
|
2021-02-02 18:49:11 +01:00
|
|
|
|
|
|
|
def new(conn, _params) do
|
2021-02-03 09:37:00 +01:00
|
|
|
{captcha_text, captcha_result} = Captcha.generate()
|
|
|
|
|
2021-02-02 18:49:11 +01:00
|
|
|
conn
|
|
|
|
|> assign(:request_changeset, SupportRequest.changeset(%SupportRequest{}, %{}))
|
2021-02-03 09:37:00 +01:00
|
|
|
|> assign(:captcha, captcha_text)
|
|
|
|
|> put_session(:captcha, captcha_result)
|
2021-02-02 18:49:11 +01:00
|
|
|
|> render("new.html")
|
|
|
|
end
|
|
|
|
|
2021-02-03 09:37:00 +01:00
|
|
|
def create(conn, %{"support_request" => raw} = params) do
|
|
|
|
attrs = raw |> Map.put("expected_captcha", get_session(conn, :captcha))
|
|
|
|
action = SupportRequest.changeset(%SupportRequest{}, attrs)
|
2021-02-02 18:49:11 +01:00
|
|
|
|> Ecto.Changeset.apply_action(:update)
|
2021-02-03 09:37:00 +01:00
|
|
|
|
2021-02-02 18:49:11 +01:00
|
|
|
case action do
|
|
|
|
{:ok, request} ->
|
2021-02-03 09:37:00 +01:00
|
|
|
# FIXME: check for error?
|
2021-02-02 18:49:11 +01:00
|
|
|
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} ->
|
2021-02-03 09:37:00 +01:00
|
|
|
{captcha_text, captcha_result} = Captcha.generate()
|
|
|
|
|
2021-02-02 18:49:11 +01:00
|
|
|
conn
|
|
|
|
|> assign(:request_changeset, changeset)
|
2021-02-03 09:37:00 +01:00
|
|
|
|> assign(:captcha, captcha_text)
|
|
|
|
|> put_session(:captcha, captcha_result)
|
2021-02-02 18:49:11 +01:00
|
|
|
|> render("new.html")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|