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

50 lines
1.3 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.AmmoGroupLive.Index do
2022-01-22 21:40:29 -05:00
@moduledoc """
Liveview to show a Cannery.Ammo.AmmoGroup index
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_view
alias Cannery.Ammo
alias Cannery.Ammo.AmmoGroup
@impl true
2021-09-02 23:31:16 -04:00
def mount(_params, session, socket) do
2022-02-01 00:14:14 -05:00
{:ok, socket |> assign_defaults(session) |> display_ammo_groups()}
2021-09-02 23:31:14 -04:00
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Ammo group")
|> assign(:ammo_group, Ammo.get_ammo_group!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Ammo group")
|> assign(:ammo_group, %AmmoGroup{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Ammo groups")
|> assign(:ammo_group, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
2022-02-01 00:14:14 -05:00
Ammo.get_ammo_group!(id) |> Ammo.delete_ammo_group!()
{:noreply, socket |> display_ammo_groups()}
2021-09-02 23:31:14 -04:00
end
2022-02-01 00:14:14 -05:00
defp display_ammo_groups(%{assigns: %{current_user: current_user}} = socket) do
ammo_groups = Ammo.list_ammo_groups(current_user)
socket |> assign(:ammo_groups, ammo_groups)
2021-09-02 23:31:14 -04:00
end
end