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

101 lines
3.3 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 """
Liveview to show a Cannery.Invites.Invite index
"""
2021-09-10 00:23:26 -04:00
use CanneryWeb, :live_view
2022-01-22 21:40:29 -05:00
alias Cannery.Invites
alias Cannery.Invites.Invite
2021-09-10 00:23:26 -04:00
@impl true
def mount(_params, session, socket) do
{:ok, socket |> assign_defaults(session) |> display_invites()}
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-11 00:34:29 -05:00
socket |> assign(page_title: gettext("Listing Invites"), invite: nil)
2021-09-10 00:23:26 -04:00
end
@impl true
2022-02-11 00:34:29 -05:00
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
%{name: invite_name} =
id |> Invites.get_invite!(current_user) |> Invites.delete_invite!(current_user)
prompt = dgettext("prompts", "%{name} deleted succesfully", name: invite_name)
{: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)
|> Invites.update_invite(%{"uses_left" => nil}, current_user)
|> case do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} updated succesfully", name: invite_name)
socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} ->
socket |> put_flash(:error, changeset |> changeset_errors())
end
{:noreply, socket}
end
2022-02-11 00:34:29 -05:00
def handle_event("enable", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
socket =
Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{"uses_left" => nil, "disabled_at" => nil}, current_user)
|> case do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} enabled succesfully", name: invite_name)
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-11 00:34:29 -05:00
def handle_event("disable", %{"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)
|> Invites.update_invite(%{"uses_left" => 0, "disabled_at" => now}, current_user)
|> case do
{:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} disabled succesfully", name: invite_name)
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-02-11 00:34:29 -05:00
defp display_invites(%{assigns: %{current_user: current_user}} = socket) do
socket |> assign(:invites, Invites.list_invites(current_user))
2021-09-10 00:23:26 -04:00
end
end