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

144 lines
4.4 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.InviteLive.Index do
2022-02-25 22:13:35 -05:00
@moduledoc """
2023-02-04 17:22:06 -05:00
Liveview to show a Memex.Accounts.Invite index
2022-02-25 22:13:35 -05:00
"""
2022-07-25 19:31:54 -04:00
use MemexWeb, :live_view
2023-02-04 17:22:06 -05:00
alias Memex.Accounts
alias Memex.Accounts.{Invite, Invites}
2022-02-25 22:13:35 -05:00
alias Phoenix.LiveView.JS
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> display_invites()}
2022-02-25 22:13:35 -05:00
end
@impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, socket |> apply_action(live_action, params)}
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket
2022-11-17 22:30:01 -05:00
|> assign(page_title: gettext("edit invite"), invite: Invites.get_invite!(id, current_user))
2022-02-25 22:13:35 -05:00
end
defp apply_action(socket, :new, _params) do
2022-11-17 22:30:01 -05:00
socket |> assign(page_title: gettext("new invite"), invite: %Invite{})
2022-02-25 22:13:35 -05:00
end
defp apply_action(socket, :index, _params) do
2022-11-17 22:30:01 -05:00
socket |> assign(page_title: gettext("invites"), invite: nil)
2022-02-25 22:13:35 -05:00
end
@impl true
def handle_event(
"delete_invite",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
%{name: invite_name} =
id |> Invites.get_invite!(current_user) |> Invites.delete_invite!(current_user)
2022-05-05 21:47:22 -04:00
prompt = dgettext("prompts", "%{invite_name} deleted succesfully", invite_name: invite_name)
2022-02-25 22:13:35 -05:00
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end
def handle_event(
"set_unlimited",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
socket =
Invites.get_invite!(id, current_user)
2023-02-04 17:22:06 -05:00
|> Invites.update_invite(%{uses_left: nil}, current_user)
2022-02-25 22:13:35 -05:00
|> case do
{:ok, %{name: invite_name}} ->
2022-05-05 21:47:22 -04:00
prompt =
dgettext("prompts", "%{invite_name} updated succesfully", invite_name: invite_name)
2022-02-25 22:13:35 -05:00
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
def handle_event(
"enable_invite",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
socket =
Invites.get_invite!(id, current_user)
2023-02-04 17:22:06 -05:00
|> Invites.update_invite(%{uses_left: nil, disabled_at: nil}, current_user)
2022-02-25 22:13:35 -05:00
|> case do
{:ok, %{name: invite_name}} ->
2022-05-05 21:47:22 -04:00
prompt =
dgettext("prompts", "%{invite_name} enabled succesfully", invite_name: invite_name)
2022-02-25 22:13:35 -05:00
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
def handle_event(
"disable_invite",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
socket =
Invites.get_invite!(id, current_user)
2023-02-04 17:22:06 -05:00
|> Invites.update_invite(%{uses_left: 0, disabled_at: now}, current_user)
2022-02-25 22:13:35 -05:00
|> case do
{:ok, %{name: invite_name}} ->
2022-05-05 21:47:22 -04:00
prompt =
dgettext("prompts", "%{invite_name} disabled succesfully", invite_name: invite_name)
2022-02-25 22:13:35 -05:00
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
2023-02-04 17:36:27 -05:00
def handle_event("copy_to_clipboard", _params, socket) do
2023-02-04 17:22:06 -05:00
prompt = dgettext("prompts", "copied to clipboard")
2022-02-25 22:13:35 -05:00
{:noreply, socket |> put_flash(:info, prompt)}
end
def handle_event(
"delete_user",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
%{email: user_email} = Accounts.get_user!(id) |> Accounts.delete_user!(current_user)
2022-05-05 21:47:22 -04:00
prompt = dgettext("prompts", "%{user_email} deleted succesfully", user_email: user_email)
2022-02-25 22:13:35 -05:00
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end
defp display_invites(%{assigns: %{current_user: current_user}} = socket) do
invites = Invites.list_invites(current_user)
all_users = Accounts.list_all_users_by_role(current_user)
admins =
all_users
|> Map.get(:admin, [])
|> Enum.reject(fn %{id: user_id} -> user_id == current_user.id end)
2023-03-18 13:46:26 -04:00
use_counts = invites |> Invites.get_use_counts(current_user)
2022-02-25 22:13:35 -05:00
users = all_users |> Map.get(:user, [])
2023-03-18 13:46:26 -04:00
socket |> assign(invites: invites, use_counts: use_counts, admins: admins, users: users)
2022-02-25 22:13:35 -05:00
end
end