cannery/lib/cannery_web/live/ammo_group_live/index.ex
shibao e9ef44eb53 - harden ammo context
- add user_id to ammo types
2022-02-10 00:58:05 -05:00

49 lines
1.5 KiB
Elixir

defmodule CanneryWeb.AmmoGroupLive.Index do
@moduledoc """
Liveview to show a Cannery.Ammo.AmmoGroup index
"""
use CanneryWeb, :live_view
alias Cannery.Ammo
alias Cannery.Ammo.AmmoGroup
@impl true
def mount(_params, session, socket) do
{:ok, socket |> assign_defaults(session) |> display_ammo_groups()}
end
@impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, gettext("Edit Ammo group"))
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, dgettext("actions", "Add Ammo"))
|> assign(:ammo_group, %AmmoGroup{})
end
defp apply_action(socket, :index, _params) do
socket |> assign(:page_title, gettext("Listing Ammo groups")) |> assign(:ammo_group, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
Ammo.get_ammo_group!(id, current_user) |> Ammo.delete_ammo_group!(current_user)
prompt = dgettext("prompts", "Ammo group deleted succesfully")
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_groups()}
end
defp display_ammo_groups(%{assigns: %{current_user: current_user}} = socket) do
socket |> assign(:ammo_groups, Ammo.list_ammo_groups(current_user))
end
end