memEx/lib/memex_web/live/invite_live/form_component.ex

73 lines
2.2 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.InviteLive.FormComponent do
2022-02-25 22:13:35 -05:00
@moduledoc """
2022-07-25 19:31:54 -04:00
Livecomponent that can update or create an Memex.Invites.Invite
2022-02-25 22:13:35 -05:00
"""
2022-07-25 19:31:54 -04:00
use MemexWeb, :live_component
2022-02-25 22:13:35 -05:00
alias Ecto.Changeset
2022-07-25 19:31:54 -04:00
alias Memex.{Accounts.User, Invites, Invites.Invite}
2022-02-25 22:13:35 -05:00
alias Phoenix.LiveView.Socket
@impl true
@spec update(
%{:invite => Invite.t(), :current_user => User.t(), optional(any) => any},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{invite: invite} = assigns, socket) do
{:ok, socket |> assign(assigns) |> assign(:changeset, Invites.change_invite(invite))}
end
@impl true
def handle_event(
"validate",
%{"invite" => invite_params},
%{assigns: %{invite: invite}} = socket
) do
{:noreply, socket |> assign(:changeset, invite |> Invites.change_invite(invite_params))}
end
def handle_event("save", %{"invite" => invite_params}, %{assigns: %{action: action}} = socket) do
save_invite(socket, action, invite_params)
end
defp save_invite(
%{assigns: %{current_user: current_user, invite: invite, return_to: return_to}} = socket,
:edit,
invite_params
) do
socket =
case invite |> Invites.update_invite(invite_params, current_user) do
{:ok, %{name: invite_name}} ->
2022-05-05 21:47:22 -04:00
prompt =
dgettext("prompts", "%{invite_name} updated successfully", invite_name: invite_name)
2022-02-25 22:13:35 -05:00
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset)
end
{:noreply, socket}
end
defp save_invite(
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
:new,
invite_params
) do
socket =
case current_user |> Invites.create_invite(invite_params) do
{:ok, %{name: invite_name}} ->
2022-05-05 21:47:22 -04:00
prompt =
dgettext("prompts", "%{invite_name} created successfully", invite_name: invite_name)
2022-02-25 22:13:35 -05:00
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset)
end
{:noreply, socket}
end
end