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

91 lines
2.7 KiB
Elixir
Raw Permalink 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 """
2023-02-04 17:22:06 -05:00
Livecomponent that can update or create an Memex.Accounts.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
2023-02-04 17:22:06 -05:00
alias Memex.Accounts.{Invite, Invites, User}
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()}
2023-02-04 17:22:06 -05:00
def update(%{invite: _invite} = assigns, socket) do
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
2022-02-25 22:13:35 -05:00
end
@impl true
2023-02-04 17:22:06 -05:00
def handle_event("validate", %{"invite" => invite_params}, socket) do
2023-03-18 23:03:15 -04:00
{:noreply, socket |> assign_changeset(invite_params, :validate)}
2022-02-25 22:13:35 -05:00
end
def handle_event("save", %{"invite" => invite_params}, %{assigns: %{action: action}} = socket) do
save_invite(socket, action, invite_params)
end
2023-02-04 17:22:06 -05:00
defp assign_changeset(
%{assigns: %{action: action, current_user: user, invite: invite}} = socket,
2023-03-18 23:03:15 -04:00
invite_params,
changeset_action \\ nil
2023-02-04 17:22:06 -05:00
) do
2023-03-18 23:03:15 -04:00
default_action =
2023-02-04 17:22:06 -05:00
case action do
:new -> :insert
:edit -> :update
end
changeset =
2023-03-18 23:03:15 -04:00
case default_action do
:insert -> Invite.create_changeset(user, "example_token", invite_params)
:update -> invite |> Invite.update_changeset(invite_params)
2023-02-04 17:22:06 -05:00
end
changeset =
2023-03-18 23:03:15 -04:00
case changeset |> Changeset.apply_action(changeset_action || default_action) do
2023-02-04 17:22:06 -05:00
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
socket |> assign(:changeset, changeset)
end
2022-02-25 22:13:35 -05:00
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}} ->
2023-02-04 17:22:06 -05:00
prompt = dgettext("prompts", "%{name} updated successfully", name: invite_name)
2022-11-16 21:11:02 -05:00
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
2022-02-25 22:13:35 -05:00
{: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}} ->
2023-02-04 17:22:06 -05:00
prompt = dgettext("prompts", "%{name} created successfully", name: invite_name)
2022-11-16 21:11:02 -05:00
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
2022-02-25 22:13:35 -05:00
{:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset)
end
{:noreply, socket}
end
end