2024-07-11 01:14:16 +05:00
|
|
|
|
defmodule ComfycampWeb.UserRegistrationLive do
|
|
|
|
|
use ComfycampWeb, :live_view
|
|
|
|
|
|
|
|
|
|
alias Comfycamp.Accounts
|
|
|
|
|
alias Comfycamp.Accounts.User
|
|
|
|
|
|
|
|
|
|
def render(assigns) do
|
|
|
|
|
~H"""
|
2024-07-22 21:12:29 +05:00
|
|
|
|
<div class="">
|
2024-07-11 01:14:16 +05:00
|
|
|
|
<.header class="text-center">
|
2024-07-22 21:12:29 +05:00
|
|
|
|
Создайте аккаунт
|
2024-07-11 01:14:16 +05:00
|
|
|
|
<:subtitle>
|
2024-07-22 21:12:29 +05:00
|
|
|
|
Уже зарегистрированы?
|
2024-07-11 01:14:16 +05:00
|
|
|
|
<.link navigate={~p"/users/log_in"} class="font-semibold text-brand hover:underline">
|
2024-07-22 21:12:29 +05:00
|
|
|
|
Войти
|
2024-07-11 01:14:16 +05:00
|
|
|
|
</.link>
|
2024-07-22 21:12:29 +05:00
|
|
|
|
в аккаунт.
|
2024-07-11 01:14:16 +05:00
|
|
|
|
</:subtitle>
|
|
|
|
|
</.header>
|
|
|
|
|
|
|
|
|
|
<.simple_form
|
|
|
|
|
for={@form}
|
|
|
|
|
id="registration_form"
|
|
|
|
|
phx-submit="save"
|
|
|
|
|
phx-change="validate"
|
|
|
|
|
phx-trigger-action={@trigger_submit}
|
|
|
|
|
action={~p"/users/log_in?_action=registered"}
|
|
|
|
|
method="post"
|
|
|
|
|
>
|
|
|
|
|
<.error :if={@check_errors}>
|
2024-07-22 21:12:29 +05:00
|
|
|
|
Что-то пошло не так. Проверьте ошибки ниже.
|
2024-07-11 01:14:16 +05:00
|
|
|
|
</.error>
|
|
|
|
|
|
|
|
|
|
<.input field={@form[:email]} type="email" label="Email" required />
|
2024-07-22 21:12:29 +05:00
|
|
|
|
<.input field={@form[:password]} type="password" label="Пароль" required />
|
2024-08-25 23:04:41 +05:00
|
|
|
|
<.input
|
|
|
|
|
field={@form[:info]}
|
|
|
|
|
type="textarea"
|
|
|
|
|
label="Почему вы хотите получить доступ?"
|
|
|
|
|
required
|
|
|
|
|
/>
|
2024-08-16 00:40:09 +05:00
|
|
|
|
<p>
|
|
|
|
|
Ваш небольшой рассказ помогает защитить сервисы.
|
|
|
|
|
Можете указать ссылки на соцсети.
|
|
|
|
|
</p>
|
2024-07-11 01:14:16 +05:00
|
|
|
|
|
|
|
|
|
<:actions>
|
2024-07-22 21:12:29 +05:00
|
|
|
|
<.button phx-disable-with="Создаю аккаунт..." class="w-full">
|
|
|
|
|
Создать аккаунт
|
|
|
|
|
</.button>
|
2024-07-11 01:14:16 +05:00
|
|
|
|
</:actions>
|
|
|
|
|
</.simple_form>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def mount(_params, _session, socket) do
|
|
|
|
|
changeset = Accounts.change_user_registration(%User{})
|
|
|
|
|
|
|
|
|
|
socket =
|
|
|
|
|
socket
|
|
|
|
|
|> assign(trigger_submit: false, check_errors: false)
|
|
|
|
|
|> assign_form(changeset)
|
|
|
|
|
|
|
|
|
|
{:ok, socket, temporary_assigns: [form: nil]}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_event("save", %{"user" => user_params}, socket) do
|
|
|
|
|
case Accounts.register_user(user_params) do
|
|
|
|
|
{:ok, user} ->
|
|
|
|
|
{:ok, _} =
|
|
|
|
|
Accounts.deliver_user_confirmation_instructions(
|
|
|
|
|
user,
|
|
|
|
|
&url(~p"/users/confirm/#{&1}")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
changeset = Accounts.change_user_registration(user)
|
|
|
|
|
{:noreply, socket |> assign(trigger_submit: true) |> assign_form(changeset)}
|
|
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
|
{:noreply, socket |> assign(check_errors: true) |> assign_form(changeset)}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_event("validate", %{"user" => user_params}, socket) do
|
|
|
|
|
changeset = Accounts.change_user_registration(%User{}, user_params)
|
|
|
|
|
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
|
|
|
|
form = to_form(changeset, as: "user")
|
|
|
|
|
|
|
|
|
|
if changeset.valid? do
|
|
|
|
|
assign(socket, form: form, check_errors: false)
|
|
|
|
|
else
|
|
|
|
|
assign(socket, form: form)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|