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
|
2021-09-10 22:22:18 -04:00
|
|
|
{:ok, socket |> assign_defaults(session) |> display_invites()}
|
2021-09-10 00:23:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_params(params, _url, socket) do
|
2021-09-10 22:22:18 -04:00
|
|
|
{:noreply, socket |> apply_action(socket.assigns.live_action, params)}
|
2021-09-10 00:23:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :edit, %{"id" => id}) do
|
|
|
|
socket
|
2021-09-10 22:22:18 -04:00
|
|
|
|> assign(page_title: "Edit Invite", invite: Invites.get_invite!(id))
|
2021-09-10 00:23:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :new, _params) do
|
|
|
|
socket
|
2021-09-10 22:22:18 -04:00
|
|
|
|> assign(page_title: "New Invite", invite: %Invite{})
|
2021-09-10 00:23:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :index, _params) do
|
|
|
|
socket
|
2021-09-10 22:22:18 -04:00
|
|
|
|> assign(page_title: "Listing Invites", invite: nil)
|
2021-09-10 00:23:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("delete", %{"id" => id}, socket) do
|
|
|
|
invite = Invites.get_invite!(id)
|
|
|
|
{:ok, _} = Invites.delete_invite(invite)
|
2021-09-10 22:22:18 -04:00
|
|
|
{:noreply, socket |> display_invites()}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("set_unlimited", %{"id" => id}, socket) do
|
|
|
|
id |> Invites.get_invite!() |> Invites.update_invite(%{"uses_left" => nil})
|
|
|
|
{:noreply, socket |> display_invites()}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("enable", %{"id" => id}, socket) do
|
|
|
|
attrs = %{"uses_left" => nil, "disabled_at" => nil}
|
|
|
|
id |> Invites.get_invite!() |> Invites.update_invite(attrs)
|
|
|
|
{:noreply, socket |> display_invites()}
|
|
|
|
end
|
2021-09-10 00:23:26 -04:00
|
|
|
|
2021-09-10 22:22:18 -04:00
|
|
|
def handle_event("disable", %{"id" => id}, socket) do
|
|
|
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
|
|
|
attrs = %{"uses_left" => 0, "disabled_at" => now}
|
|
|
|
id |> Invites.get_invite!() |> Invites.update_invite(attrs)
|
|
|
|
{:noreply, socket |> display_invites()}
|
2021-09-10 00:23:26 -04:00
|
|
|
end
|
|
|
|
|
2021-09-10 22:22:18 -04:00
|
|
|
# redisplays invites to socket
|
|
|
|
defp display_invites(socket) do
|
|
|
|
invites = Invites.list_invites()
|
|
|
|
socket |> assign(:invites, invites)
|
2021-09-10 00:23:26 -04:00
|
|
|
end
|
|
|
|
end
|