forked from shibao/cannery
edit and delete shot groups from ammo group page
This commit is contained in:
@ -5,8 +5,9 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
import CanneryWeb.Components.ContainerCard
|
||||
alias Cannery.{Ammo, Repo}
|
||||
alias Cannery.{ActivityLog, Ammo, Ammo.AmmoGroup, Repo}
|
||||
alias CanneryWeb.Endpoint
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
@ -15,14 +16,31 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
|
||||
@impl true
|
||||
def handle_params(
|
||||
%{"id" => id},
|
||||
%{"id" => id, "shot_group_id" => shot_group_id},
|
||||
_url,
|
||||
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
|
||||
) do
|
||||
ammo_group = Ammo.get_ammo_group!(id, current_user) |> Repo.preload([:container, :ammo_type])
|
||||
{:noreply, socket |> assign(page_title: page_title(live_action), ammo_group: ammo_group)}
|
||||
shot_group = ActivityLog.get_shot_group!(shot_group_id, current_user)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(page_title: page_title(live_action), shot_group: shot_group)
|
||||
|> display_ammo_group(id)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _url, %{assigns: %{live_action: live_action}} = socket) do
|
||||
{:noreply, socket |> assign(page_title: page_title(live_action)) |> display_ammo_group(id)}
|
||||
end
|
||||
|
||||
defp page_title(:add_shot_group), do: gettext("Record Shots")
|
||||
defp page_title(:edit_shot_group), do: gettext("Edit Shot Records")
|
||||
defp page_title(:move), do: gettext("Move Ammo group")
|
||||
defp page_title(:show), do: gettext("Show Ammo group")
|
||||
defp page_title(:edit), do: gettext("Edit Ammo group")
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete",
|
||||
@ -46,11 +64,29 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
{:ok, ammo_group} =
|
||||
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
|
||||
|
||||
{:noreply, socket |> assign(ammo_group: ammo_group)}
|
||||
{:noreply, socket |> display_ammo_group(ammo_group)}
|
||||
end
|
||||
|
||||
defp page_title(:add_shot_group), do: gettext("Add Shot group")
|
||||
defp page_title(:move), do: gettext("Move Ammo group")
|
||||
defp page_title(:show), do: gettext("Show Ammo group")
|
||||
defp page_title(:edit), do: gettext("Edit Ammo group")
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete_shot_group",
|
||||
%{"id" => id},
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
||||
) do
|
||||
{:ok, _} =
|
||||
ActivityLog.get_shot_group!(id, current_user)
|
||||
|> ActivityLog.delete_shot_group(current_user)
|
||||
|
||||
prompt = dgettext("prompts", "Shot records deleted succesfully")
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_group(ammo_group)}
|
||||
end
|
||||
|
||||
@spec display_ammo_group(Socket.t(), AmmoGroup.t() | AmmoGroup.id()) :: Socket.t()
|
||||
defp display_ammo_group(socket, %AmmoGroup{} = ammo_group) do
|
||||
ammo_group = ammo_group |> Repo.preload([:container, :ammo_type, :shot_groups], force: true)
|
||||
socket |> assign(:ammo_group, ammo_group)
|
||||
end
|
||||
|
||||
defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id),
|
||||
do: display_ammo_group(socket, Ammo.get_ammo_group!(id, current_user))
|
||||
end
|
||||
|
@ -124,6 +124,8 @@
|
||||
<th class="p-2">
|
||||
<%= gettext("Date") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="shot_groups">
|
||||
@ -132,12 +134,35 @@
|
||||
<td class="p-2">
|
||||
<%= shot_group.count %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= shot_group.notes %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= shot_group.date |> display_date() %>
|
||||
</td>
|
||||
|
||||
<td class="p-2 w-full h-full space-x-2 flex justify-center items-center">
|
||||
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
||||
<%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, shot_group),
|
||||
class: "text-primary-600 link",
|
||||
data: [qa: "edit-#{shot_group.id}"] do %>
|
||||
<i class="fa-fw fa-lg fas fa-edit"></i>
|
||||
<% end %>
|
||||
|
||||
<%= link to: "#",
|
||||
class: "text-primary-600 link",
|
||||
phx_click: "delete_shot_group",
|
||||
phx_value_id: shot_group.id,
|
||||
data: [
|
||||
confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"),
|
||||
qa: "delete-#{shot_group.id}"
|
||||
] do %>
|
||||
<i class="fa-fw fa-lg fas fa-trash"></i>
|
||||
<% end %>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
@ -159,6 +184,18 @@
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% :edit_shot_group -> %>
|
||||
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.RangeLive.FormComponent}
|
||||
id={@shot_group.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
shot_group={@shot_group}
|
||||
return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% :add_shot_group -> %>
|
||||
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
|
||||
<.live_component
|
||||
|
@ -25,7 +25,7 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
%{"id" => id}
|
||||
) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Record shots"))
|
||||
|> assign(:page_title, gettext("Record Shots"))
|
||||
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
|
||||
end
|
||||
|
||||
|
@ -80,6 +80,7 @@ defmodule CanneryWeb.Router do
|
||||
live "/ammo_groups/:id/show/edit", AmmoGroupLive.Show, :edit
|
||||
live "/ammo_groups/:id/show/add_shot_group", AmmoGroupLive.Show, :add_shot_group
|
||||
live "/ammo_groups/:id/show/move", AmmoGroupLive.Show, :move
|
||||
live "/ammo_groups/:id/show/:shot_group_id/edit", AmmoGroupLive.Show, :edit_shot_group
|
||||
|
||||
live "/range", RangeLive.Index, :index
|
||||
live "/range/:id/edit", RangeLive.Index, :edit
|
||||
|
Reference in New Issue
Block a user