2023-03-29 22:54:55 -04:00
|
|
|
defmodule CanneryWeb.PackLive.Index do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
2023-03-29 22:54:55 -04:00
|
|
|
Liveview to show a Cannery.Ammo.Pack index
|
2022-01-22 21:40:29 -05:00
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use CanneryWeb, :live_view
|
2023-03-29 22:54:55 -04:00
|
|
|
alias Cannery.{Ammo, Ammo.Pack, Containers}
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-12-03 18:46:44 -05:00
|
|
|
def mount(%{"search" => search}, _session, socket) do
|
2023-03-28 23:08:40 -04:00
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(class: :all, show_used: false, search: search)
|
2023-03-29 22:54:55 -04:00
|
|
|
|> display_packs()
|
2023-03-28 23:08:40 -04:00
|
|
|
|
|
|
|
{:ok, socket}
|
2022-12-03 18:46:44 -05:00
|
|
|
end
|
|
|
|
|
2022-05-05 23:26:29 -04:00
|
|
|
def mount(_params, _session, socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:ok, socket |> assign(class: :all, show_used: false, search: nil) |> display_packs()}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-02-10 00:57:56 -05:00
|
|
|
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, apply_action(socket, live_action, params) |> display_packs()}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-23 20:34:07 -05:00
|
|
|
defp apply_action(
|
|
|
|
%{assigns: %{current_user: current_user}} = socket,
|
2023-03-30 20:43:30 -04:00
|
|
|
:add_shot_record,
|
2022-02-23 20:34:07 -05:00
|
|
|
%{"id" => id}
|
|
|
|
) do
|
2022-02-17 21:19:01 -05:00
|
|
|
socket
|
2022-12-03 18:46:44 -05:00
|
|
|
|> assign(
|
|
|
|
page_title: gettext("Record shots"),
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: Ammo.get_pack!(id, current_user)
|
2022-12-03 18:46:44 -05:00
|
|
|
)
|
2022-02-17 21:19:01 -05:00
|
|
|
end
|
|
|
|
|
2022-02-15 18:20:12 -05:00
|
|
|
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :move, %{"id" => id}) do
|
|
|
|
socket
|
2022-12-03 18:46:44 -05:00
|
|
|
|> assign(
|
|
|
|
page_title: gettext("Move ammo"),
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: Ammo.get_pack!(id, current_user)
|
2022-12-03 18:46:44 -05:00
|
|
|
)
|
2022-02-15 18:20:12 -05:00
|
|
|
end
|
|
|
|
|
2022-02-10 00:57:56 -05:00
|
|
|
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
2021-09-02 23:31:14 -04:00
|
|
|
socket
|
2022-12-03 18:46:44 -05:00
|
|
|
|> assign(
|
|
|
|
page_title: gettext("Edit ammo"),
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: Ammo.get_pack!(id, current_user)
|
2022-12-03 18:46:44 -05:00
|
|
|
)
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-11-10 18:21:29 -05:00
|
|
|
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
|
|
|
|
socket
|
2022-12-03 18:46:44 -05:00
|
|
|
|> assign(
|
|
|
|
page_title: dgettext("actions", "Add Ammo"),
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: %{Ammo.get_pack!(id, current_user) | id: nil}
|
2022-12-03 18:46:44 -05:00
|
|
|
)
|
2022-11-10 18:21:29 -05:00
|
|
|
end
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
defp apply_action(socket, :new, _params) do
|
|
|
|
socket
|
2022-12-03 18:46:44 -05:00
|
|
|
|> assign(
|
|
|
|
page_title: dgettext("actions", "Add Ammo"),
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: %Pack{}
|
2022-12-03 18:46:44 -05:00
|
|
|
)
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :index, _params) do
|
2022-11-10 21:45:50 -05:00
|
|
|
socket
|
2022-12-03 18:46:44 -05:00
|
|
|
|> assign(
|
|
|
|
page_title: gettext("Ammo"),
|
|
|
|
search: nil,
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: nil
|
2022-12-03 18:46:44 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :search, %{"search" => search}) do
|
|
|
|
socket
|
|
|
|
|> assign(
|
|
|
|
page_title: gettext("Ammo"),
|
|
|
|
search: search,
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: nil
|
2022-12-03 18:46:44 -05:00
|
|
|
)
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-02-10 00:57:56 -05:00
|
|
|
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
Ammo.get_pack!(id, current_user) |> Ammo.delete_pack!(current_user)
|
2022-02-10 00:57:56 -05:00
|
|
|
|
2022-11-19 15:04:56 -05:00
|
|
|
prompt = dgettext("prompts", "Ammo deleted succesfully")
|
2022-02-10 00:57:56 -05:00
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> put_flash(:info, prompt) |> display_packs()}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-15 17:33:45 -05:00
|
|
|
def handle_event(
|
|
|
|
"toggle_staged",
|
2023-03-29 22:54:55 -04:00
|
|
|
%{"pack_id" => id},
|
2022-02-15 17:33:45 -05:00
|
|
|
%{assigns: %{current_user: current_user}} = socket
|
|
|
|
) do
|
2023-03-29 22:54:55 -04:00
|
|
|
pack = Ammo.get_pack!(id, current_user)
|
2022-02-15 17:33:45 -05:00
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
{:ok, _pack} = pack |> Ammo.update_pack(%{"staged" => !pack.staged}, current_user)
|
2022-02-15 17:33:45 -05:00
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> display_packs()}
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
|
|
|
|
2022-11-07 01:36:28 -05:00
|
|
|
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> assign(:show_used, !show_used) |> display_packs()}
|
2022-11-07 00:47:22 -05:00
|
|
|
end
|
|
|
|
|
2022-12-03 18:46:44 -05:00
|
|
|
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
|
2023-04-14 23:34:11 -04:00
|
|
|
{:noreply, socket |> push_patch(to: ~p"/ammo")}
|
2022-12-03 18:46:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
|
2023-04-14 23:34:11 -04:00
|
|
|
{:noreply, socket |> push_patch(to: ~p"/ammo/search/#{search_term}")}
|
2022-12-03 18:46:44 -05:00
|
|
|
end
|
|
|
|
|
2023-03-30 21:53:52 -04:00
|
|
|
def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> assign(:class, :rifle) |> display_packs()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2023-03-30 21:53:52 -04:00
|
|
|
def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> assign(:class, :shotgun) |> display_packs()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2023-03-30 21:53:52 -04:00
|
|
|
def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> assign(:class, :pistol) |> display_packs()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2023-03-30 21:53:52 -04:00
|
|
|
def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do
|
2023-03-29 22:54:55 -04:00
|
|
|
{:noreply, socket |> assign(:class, :all) |> display_packs()}
|
2023-03-23 22:07:25 -04:00
|
|
|
end
|
|
|
|
|
2023-03-29 22:54:55 -04:00
|
|
|
defp display_packs(
|
2023-03-23 22:07:25 -04:00
|
|
|
%{
|
|
|
|
assigns: %{
|
2023-03-28 23:08:40 -04:00
|
|
|
class: class,
|
2023-03-23 22:07:25 -04:00
|
|
|
search: search,
|
|
|
|
current_user: current_user,
|
|
|
|
show_used: show_used
|
|
|
|
}
|
|
|
|
} = socket
|
2022-11-07 00:47:22 -05:00
|
|
|
) do
|
2023-03-29 23:49:45 -04:00
|
|
|
# get total number of packs to determine whether to display onboarding
|
2023-03-23 22:07:25 -04:00
|
|
|
# prompts
|
2023-06-04 20:53:57 -04:00
|
|
|
packs_count = Ammo.get_packs_count(current_user, show_used: true)
|
2023-06-04 00:00:51 -04:00
|
|
|
packs = Ammo.list_packs(current_user, search: search, class: class, show_used: show_used)
|
2023-03-30 21:53:52 -04:00
|
|
|
types_count = Ammo.get_types_count!(current_user)
|
2022-07-04 21:41:07 -04:00
|
|
|
containers_count = Containers.get_containers_count!(current_user)
|
2022-03-04 20:00:09 -05:00
|
|
|
|
|
|
|
socket
|
2022-07-04 21:41:07 -04:00
|
|
|
|> assign(
|
2023-03-29 22:54:55 -04:00
|
|
|
packs: packs,
|
2023-03-30 21:53:52 -04:00
|
|
|
types_count: types_count,
|
2023-03-23 22:07:25 -04:00
|
|
|
containers_count: containers_count,
|
2023-03-29 22:54:55 -04:00
|
|
|
packs_count: packs_count
|
2022-07-04 21:41:07 -04:00
|
|
|
)
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
end
|