meta/lib/recycledcloud_web/controllers/support_controller.ex

42 lines
1.2 KiB
Elixir
Raw Normal View History

2021-02-02 18:49:11 +01:00
defmodule RecycledCloudWeb.SupportController do
use RecycledCloudWeb, :controller
alias RecycledCloud.SupportRequest
2021-02-03 09:37:00 +01:00
alias RecycledCloud.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