cannery/lib/cannery_web/live/invite_live/index.ex

156 lines
4.8 KiB
Elixir
Raw Normal View History

2021-09-10 00:23:26 -04:00
defmodule CanneryWeb.InviteLive.Index do
2022-01-22 21:40:29 -05:00
@moduledoc """
2023-02-04 16:46:37 -05:00
Liveview to show a Cannery.Accounts.Invite index
2022-01-22 21:40:29 -05:00
"""
2021-09-10 00:23:26 -04:00
use CanneryWeb, :live_view
import CanneryWeb.Components.{InviteCard, UserCard}
2023-02-04 16:46:37 -05:00
alias Cannery.Accounts
alias Cannery.Accounts.{Invite, Invites}
2023-02-26 14:21:49 -05:00
alias CanneryWeb.HomeLive
2022-02-15 19:50:48 -05:00
alias Phoenix.LiveView.JS
2021-09-10 00:23:26 -04:00
@impl true
2022-05-05 23:26:29 -04:00
def mount(_params, _session, %{assigns: %{current_user: current_user}} = socket) do
2022-02-11 22:37:58 -05:00
socket =
if current_user |> Map.get(:role) == :admin do
socket |> display_invites()
else
prompt = dgettext("errors", "You are not authorized to view this page")
return_to = Routes.live_path(Endpoint, HomeLive)
2023-02-04 16:46:37 -05:00
socket |> put_flash(:error, prompt) |> push_redirect(to: return_to)
2022-02-11 22:37:58 -05:00
end
{:ok, socket}
2021-09-10 00:23:26 -04:00
end
@impl true
2022-02-11 00:34:29 -05:00
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, socket |> apply_action(live_action, params)}
2021-09-10 00:23:26 -04:00
end
2022-02-11 00:34:29 -05:00
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
2021-09-10 00:23:26 -04:00
socket
2022-02-11 00:34:29 -05:00
|> assign(page_title: gettext("Edit Invite"), invite: Invites.get_invite!(id, current_user))
2021-09-10 00:23:26 -04:00
end
defp apply_action(socket, :new, _params) do
2022-02-11 00:34:29 -05:00
socket |> assign(page_title: gettext("New Invite"), invite: %Invite{})
2021-09-10 00:23:26 -04:00
end
defp apply_action(socket, :index, _params) do
2022-02-24 00:52:46 -05:00
socket |> assign(page_title: gettext("Invites"), invite: nil)
2021-09-10 00:23:26 -04:00
end
@impl true
def handle_event(
"delete_invite",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
2022-02-11 00:34:29 -05:00
%{name: invite_name} =
id |> Invites.get_invite!(current_user) |> Invites.delete_invite!(current_user)
2023-02-04 16:46:37 -05:00
prompt = dgettext("prompts", "%{invite_name} deleted succesfully", invite_name: invite_name)
2022-02-11 00:34:29 -05:00
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end
2022-02-11 00:34:29 -05:00
def handle_event(
"set_unlimited",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
socket =
Invites.get_invite!(id, current_user)
2023-02-04 16:46:37 -05:00
|> Invites.update_invite(%{uses_left: nil}, current_user)
2022-02-11 00:34:29 -05:00
|> case do
{:ok, %{name: invite_name}} ->
2023-02-04 16:46:37 -05:00
prompt =
dgettext("prompts", "%{invite_name} updated succesfully", invite_name: invite_name)
2022-02-11 00:34:29 -05:00
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
2022-02-15 19:50:48 -05:00
def handle_event(
"enable_invite",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
2022-02-11 00:34:29 -05:00
socket =
Invites.get_invite!(id, current_user)
2023-02-04 16:46:37 -05:00
|> Invites.update_invite(%{uses_left: nil, disabled_at: nil}, current_user)
2022-02-11 00:34:29 -05:00
|> case do
{:ok, %{name: invite_name}} ->
2023-02-04 16:46:37 -05:00
prompt =
dgettext("prompts", "%{invite_name} enabled succesfully", invite_name: invite_name)
2022-02-11 00:34:29 -05:00
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
2021-09-10 00:23:26 -04:00
2022-02-15 19:50:48 -05:00
def handle_event(
"disable_invite",
%{"id" => id},
%{assigns: %{current_user: current_user}} = socket
) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
2022-02-11 00:34:29 -05:00
socket =
Invites.get_invite!(id, current_user)
2023-02-04 16:46:37 -05:00
|> Invites.update_invite(%{uses_left: 0, disabled_at: now}, current_user)
2022-02-11 00:34:29 -05:00
|> case do
{:ok, %{name: invite_name}} ->
2023-02-04 16:46:37 -05:00
prompt =
dgettext("prompts", "%{invite_name} disabled succesfully", invite_name: invite_name)
2022-02-11 00:34:29 -05:00
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
2021-09-10 00:23:26 -04:00
end
2022-03-28 23:05:12 -04:00
def handle_event("copy_to_clipboard", _params, socket) do
2022-02-15 19:50:48 -05:00
prompt = dgettext("prompts", "Copied to clipboard")
{: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)
2023-02-04 16:46:37 -05:00
prompt = dgettext("prompts", "%{user_email} deleted succesfully", user_email: user_email)
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end
2022-02-11 00:34:29 -05:00
defp display_invites(%{assigns: %{current_user: current_user}} = socket) do
2022-02-15 19:55:48 -05:00
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)
2022-02-15 19:55:48 -05:00
users = all_users |> Map.get(:user, [])
socket |> assign(invites: invites, admins: admins, users: users)
2021-09-10 00:23:26 -04:00
end
end