use strict context boundaries and remove all n+1 queries

This commit is contained in:
2023-03-18 21:06:00 -04:00
parent 0b7146ba32
commit a54cf8b87d
84 changed files with 4345 additions and 3090 deletions

View File

@ -44,7 +44,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
@impl true
def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do
{:noreply, socket |> assign_changeset(ammo_group_params)}
{:noreply, socket |> assign_changeset(ammo_group_params, :validate)}
end
def handle_event(
@ -56,6 +56,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
end
# HTML Helpers
@spec container_options([Container.t()]) :: [{String.t(), Container.id()}]
defp container_options(containers) do
containers |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
@ -70,35 +71,28 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
defp assign_changeset(
%{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket,
ammo_group_params
ammo_group_params,
changeset_action \\ nil
) do
changeset_action =
cond do
action in [:new, :clone] -> :insert
action == :edit -> :update
default_action =
case action do
create when create in [:new, :clone] -> :insert
:edit -> :update
end
changeset =
cond do
action in [:new, :clone] ->
ammo_type =
if ammo_group_params |> Map.has_key?("ammo_type_id"),
do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user),
else: nil
container =
if ammo_group_params |> Map.has_key?("container_id"),
do: ammo_group_params |> Map.get("container_id") |> Containers.get_container!(user),
else: nil
case default_action do
:insert ->
ammo_type = maybe_get_ammo_type(ammo_group_params, user)
container = maybe_get_container(ammo_group_params, user)
ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params)
action == :edit ->
:update ->
ammo_group |> AmmoGroup.update_changeset(ammo_group_params, user)
end
changeset =
case changeset |> Changeset.apply_action(changeset_action) do
case changeset |> Changeset.apply_action(changeset_action || default_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
@ -106,6 +100,20 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
socket |> assign(:changeset, changeset)
end
defp maybe_get_container(%{"container_id" => container_id}, user)
when is_binary(container_id) do
container_id |> Containers.get_container!(user)
end
defp maybe_get_container(_params_not_found, _user), do: nil
defp maybe_get_ammo_type(%{"ammo_type_id" => ammo_type_id}, user)
when is_binary(ammo_type_id) do
ammo_type_id |> Ammo.get_ammo_type!(user)
end
defp maybe_get_ammo_type(_params_not_found, _user), do: nil
defp save_ammo_group(
%{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} =
socket,
@ -146,27 +154,26 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
multiplier: multiplier
)
{:error, changeset} =
changeset
|> Changeset.add_error(:multiplier, error_msg)
|> Changeset.apply_action(:insert)
socket |> assign(:changeset, changeset)
save_multiplier_error(socket, changeset, error_msg)
:error ->
error_msg = dgettext("errors", "Could not parse number of copies")
{:error, changeset} =
changeset
|> Changeset.add_error(:multiplier, error_msg)
|> Changeset.apply_action(:insert)
socket |> assign(:changeset, changeset)
save_multiplier_error(socket, changeset, error_msg)
end
{:noreply, socket}
end
@spec save_multiplier_error(Socket.t(), Changeset.t(), String.t()) :: Socket.t()
defp save_multiplier_error(socket, changeset, error_msg) do
{:error, changeset} =
changeset
|> Changeset.add_error(:multiplier, error_msg)
|> Changeset.apply_action(:insert)
socket |> assign(:changeset, changeset)
end
defp create_multiple(
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
ammo_group_params,

View File

@ -104,7 +104,7 @@
</.link>
</div>
</:range>
<:container :let={%{container: %{name: container_name} = container} = ammo_group}>
<:container :let={{ammo_group, %{name: container_name} = container}}>
<div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center">
<.link
navigate={Routes.container_show_path(Endpoint, :show, container)}
@ -117,18 +117,18 @@
patch={Routes.ammo_group_index_path(Endpoint, :move, ammo_group)}
class="mx-2 my-1 text-sm btn btn-primary"
>
<%= gettext("Move ammo") %>
<%= dgettext("actions", "Move ammo") %>
</.link>
</div>
</:container>
<:actions :let={ammo_group}>
<:actions :let={%{count: ammo_group_count} = ammo_group}>
<div class="py-2 px-4 h-full space-x-4 flex justify-center items-center">
<.link
navigate={Routes.ammo_group_show_path(Endpoint, :show, ammo_group)}
class="text-primary-600 link"
aria-label={
gettext("View ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group.count
dgettext("actions", "View ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group_count
)
}
>
@ -139,8 +139,8 @@
patch={Routes.ammo_group_index_path(Endpoint, :edit, ammo_group)}
class="text-primary-600 link"
aria-label={
gettext("Edit ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group.count
dgettext("actions", "Edit ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group_count
)
}
>
@ -151,8 +151,8 @@
patch={Routes.ammo_group_index_path(Endpoint, :clone, ammo_group)}
class="text-primary-600 link"
aria-label={
gettext("Clone ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group.count
dgettext("actions", "Clone ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group_count
)
}
>
@ -166,8 +166,8 @@
phx-value-id={ammo_group.id}
data-confirm={dgettext("prompts", "Are you sure you want to delete this ammo?")}
aria-label={
gettext("Delete ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group.count
dgettext("actions", "Delete ammo group of %{ammo_group_count} bullets",
ammo_group_count: ammo_group_count
)
}
>
@ -179,8 +179,8 @@
<% end %>
</div>
<%= cond do %>
<% @live_action in [:new, :edit, :clone] -> %>
<%= case @live_action do %>
<% create when create in [:new, :edit, :clone] -> %>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.AmmoGroupLive.FormComponent}
@ -192,7 +192,7 @@
current_user={@current_user}
/>
</.modal>
<% @live_action == :add_shot_group -> %>
<% :add_shot_group -> %>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
@ -204,7 +204,7 @@
current_user={@current_user}
/>
</.modal>
<% @live_action == :move -> %>
<% :move -> %>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.Components.MoveAmmoGroupComponent}
@ -216,6 +216,5 @@
current_user={@current_user}
/>
</.modal>
<% true -> %>
<%= nil %>
<% _ -> %>
<% end %>

View File

@ -4,7 +4,9 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
"""
use CanneryWeb, :live_view
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup, Repo}
alias Cannery.{ActivityLog, ActivityLog.ShotGroup}
alias Cannery.{Ammo, Ammo.AmmoGroup}
alias Cannery.Containers
alias CanneryWeb.Endpoint
alias Phoenix.LiveView.Socket
@ -81,30 +83,45 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
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)
defp display_ammo_group(
%{assigns: %{current_user: current_user}} = socket,
%AmmoGroup{container_id: container_id} = ammo_group
) do
columns = [
%{label: gettext("Rounds shot"), key: :count},
%{label: gettext("Notes"), key: :notes},
%{label: gettext("Date"), key: :date},
%{label: gettext("Date"), key: :date, type: Date},
%{label: nil, key: :actions, sortable: false}
]
shot_groups = ActivityLog.list_shot_groups_for_ammo_group(ammo_group, current_user)
rows =
ammo_group.shot_groups
shot_groups
|> Enum.map(fn shot_group ->
ammo_group |> get_table_row_for_shot_group(shot_group, columns)
end)
socket |> assign(ammo_group: ammo_group, columns: columns, rows: rows)
socket
|> assign(
ammo_group: ammo_group,
original_count: Ammo.get_original_count(ammo_group, current_user),
percentage_remaining: Ammo.get_percentage_remaining(ammo_group, current_user),
container: container_id && Containers.get_container!(container_id, current_user),
shot_groups: shot_groups,
columns: columns,
rows: rows
)
end
defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id),
do: display_ammo_group(socket, Ammo.get_ammo_group!(id, current_user))
@spec display_currency(float()) :: String.t()
defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2)
@spec get_table_row_for_shot_group(AmmoGroup.t(), ShotGroup.t(), [map()]) :: map()
defp get_table_row_for_shot_group(ammo_group, %{date: date} = shot_group, columns) do
defp get_table_row_for_shot_group(ammo_group, %{id: id, date: date} = shot_group, columns) do
assigns = %{ammo_group: ammo_group, shot_group: shot_group}
columns
@ -112,11 +129,11 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
value =
case key do
:date ->
assigns = %{date: date}
assigns = %{id: id, date: date}
{date,
~H"""
<.date date={@date} />
<.date id={"#{@id}-date"} date={@date} />
"""}
:actions ->

View File

@ -11,12 +11,12 @@
<span class="rounded-lg title text-lg">
<%= gettext("Original count:") %>
<%= Ammo.get_original_count(@ammo_group) %>
<%= @original_count %>
</span>
<span class="rounded-lg title text-lg">
<%= gettext("Percentage left:") %>
<%= gettext("%{percentage}%", percentage: @ammo_group |> Ammo.get_percentage_remaining()) %>
<%= gettext("%{percentage}%", percentage: @percentage_remaining) %>
</span>
<%= if @ammo_group.notes do %>
@ -28,23 +28,19 @@
<span class="rounded-lg title text-lg">
<%= gettext("Purchased on:") %>
<.date date={@ammo_group.purchased_on} />
<.date id={"#{@ammo_group.id}-purchased-on"} date={@ammo_group.purchased_on} />
</span>
<%= if @ammo_group.price_paid do %>
<span class="rounded-lg title text-lg">
<%= gettext("Original cost:") %>
<%= gettext("$%{amount}",
amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2)
) %>
<%= gettext("$%{amount}", amount: display_currency(@ammo_group.price_paid)) %>
</span>
<span class="rounded-lg title text-lg">
<%= gettext("Current value:") %>
<%= gettext("$%{amount}",
amount:
(@ammo_group.price_paid * Ammo.get_percentage_remaining(@ammo_group) / 100)
|> :erlang.float_to_binary(decimals: 2)
amount: display_currency(@ammo_group.price_paid * @percentage_remaining / 100)
) %>
</span>
<% end %>
@ -77,7 +73,7 @@
phx-click="delete"
data-confirm={dgettext("prompts", "Are you sure you want to delete this ammo?")}
aria-label={
gettext("Delete ammo group of %{ammo_group_count} bullets",
dgettext("actions", "Delete ammo group of %{ammo_group_count} bullets",
ammo_group_count: @ammo_group.count
)
}
@ -97,7 +93,7 @@
patch={Routes.ammo_group_show_path(Endpoint, :move, @ammo_group)}
class="btn btn-primary"
>
<%= dgettext("actions", "Move containers") %>
<%= dgettext("actions", "Move ammo") %>
</.link>
<.link
@ -112,18 +108,18 @@
<hr class="mb-4 w-full" />
<div>
<%= if @ammo_group.container do %>
<%= if @container do %>
<h1 class="mb-4 px-4 py-2 text-center rounded-lg title text-xl">
<%= gettext("Stored in") %>
</h1>
<.container_card container={@ammo_group.container} />
<.container_card container={@container} current_user={@current_user} />
<% else %>
<%= gettext("This ammo is not in a container") %>
<% end %>
</div>
<%= unless @ammo_group.shot_groups |> Enum.empty?() do %>
<%= unless @shot_groups |> Enum.empty?() do %>
<hr class="mb-4 w-full" />
<h1 class="mb-4 px-4 py-2 text-center rounded-lg title text-xl">

View File

@ -35,15 +35,18 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
ammo_type_params
) do
changeset_action =
cond do
action in [:new, :clone] -> :insert
action == :edit -> :update
case action do
create when create in [:new, :clone] -> :insert
:edit -> :update
end
changeset =
cond do
action in [:new, :clone] -> ammo_type |> AmmoType.create_changeset(user, ammo_type_params)
action == :edit -> ammo_type |> AmmoType.update_changeset(ammo_type_params)
case action do
create when create in [:new, :clone] ->
ammo_type |> AmmoType.create_changeset(user, ammo_type_params)
:edit ->
ammo_type |> AmmoType.update_changeset(ammo_type_params)
end
changeset =

View File

@ -69,9 +69,7 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
%{name: name} = Ammo.get_ammo_type!(id, current_user) |> Ammo.delete_ammo_type!(current_user)
prompt = dgettext("prompts", "%{name} deleted succesfully", name: name)
{:noreply, socket |> put_flash(:info, prompt) |> list_ammo_types()}
end
@ -84,8 +82,8 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply,
socket |> push_patch(to: Routes.ammo_type_index_path(Endpoint, :search, search_term))}
search_path = Routes.ammo_type_index_path(Endpoint, :search, search_term)
{:noreply, socket |> push_patch(to: search_path)}
end
defp list_ammo_types(%{assigns: %{search: search, current_user: current_user}} = socket) do

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
"""
use CanneryWeb, :live_view
alias Cannery.{Ammo, Ammo.AmmoType}
alias Cannery.{ActivityLog, Ammo, Ammo.AmmoType}
alias CanneryWeb.Endpoint
@fields_list [
@ -91,12 +91,27 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
ammo_type |> Map.get(field) != default_value
end)
ammo_groups = ammo_type |> Ammo.list_ammo_groups_for_type(current_user, show_used)
original_counts = ammo_groups |> Ammo.get_original_counts(current_user)
cprs = ammo_groups |> Ammo.get_cprs(current_user)
historical_packs_count = ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user, true)
last_used_dates = ammo_groups |> ActivityLog.get_last_used_dates(current_user)
socket
|> assign(
page_title: page_title(live_action, ammo_type),
ammo_type: ammo_type,
ammo_groups: ammo_type |> Ammo.list_ammo_groups_for_type(current_user, show_used),
avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user),
ammo_groups: ammo_groups,
original_counts: original_counts,
cprs: cprs,
last_used_dates: last_used_dates,
avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type(current_user),
rounds: ammo_type |> Ammo.get_round_count_for_ammo_type(current_user),
used_rounds: ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user),
historical_round_count: ammo_type |> Ammo.get_historical_count_for_ammo_type(current_user),
packs_count: ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user),
used_packs_count: ammo_type |> Ammo.get_used_ammo_groups_count_for_type(current_user),
historical_packs_count: historical_packs_count,
fields_list: @fields_list,
fields_to_display: fields_to_display
)
@ -110,6 +125,9 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
socket |> display_ammo_type(ammo_type)
end
@spec display_currency(float()) :: String.t()
defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2)
defp page_title(action, %{name: ammo_type_name}) when action in [:show, :table],
do: ammo_type_name

View File

@ -6,8 +6,8 @@
<span
:if={@ammo_type.desc}
class="max-w-2xl w-full px-8 py-4 rounded-lg
text-center title text-lg
border border-primary-600"
text-center title text-lg
border border-primary-600"
>
<%= @ammo_type.desc %>
</span>
@ -71,7 +71,7 @@
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_round_count_for_ammo_type(@current_user) %>
<%= @rounds %>
</span>
<h3 class="title text-lg">
@ -79,7 +79,7 @@
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_used_count_for_ammo_type(@current_user) %>
<%= @used_rounds %>
</span>
<h3 class="title text-lg">
@ -87,7 +87,7 @@
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_historical_count_for_ammo_type(@current_user) %>
<%= @historical_round_count %>
</span>
</div>
@ -99,7 +99,7 @@
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_ammo_groups_count_for_type(@current_user) %>
<%= @packs_count %>
</span>
<h3 class="title text-lg">
@ -107,7 +107,7 @@
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_used_ammo_groups_count_for_type(@current_user) %>
<%= @used_packs_count %>
</span>
<h3 class="title text-lg">
@ -115,7 +115,7 @@
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_ammo_groups_count_for_type(@current_user, true) %>
<%= @historical_packs_count %>
</span>
</div>
@ -127,7 +127,7 @@
</h3>
<span class="text-primary-600">
<.datetime datetime={@ammo_type.inserted_at} />
<.datetime id={"#{@ammo_type.id}-inserted-at"} datetime={@ammo_type.inserted_at} />
</span>
<%= if @avg_cost_per_round do %>
@ -136,9 +136,7 @@
</h3>
<span class="text-primary-600">
<%= gettext("$%{amount}",
amount: @avg_cost_per_round |> :erlang.float_to_binary(decimals: 2)
) %>
<%= gettext("$%{amount}", amount: display_currency(@avg_cost_per_round)) %>
</span>
<% else %>
<h3 class="mx-8 my-4 title text-lg text-primary-600 col-span-2">
@ -177,7 +175,7 @@
ammo_groups={@ammo_groups}
current_user={@current_user}
>
<:container :let={%{container: %{name: container_name} = container}}>
<:container :let={{_ammo_group, %{name: container_name} = container}}>
<.link
navigate={Routes.container_show_path(Endpoint, :show, container)}
class="mx-2 my-1 link"
@ -189,8 +187,12 @@
<% else %>
<div class="flex flex-wrap justify-center items-stretch">
<.ammo_group_card
:for={ammo_group <- @ammo_groups}
:for={%{id: ammo_group_id} = ammo_group <- @ammo_groups}
ammo_group={ammo_group}
original_count={Map.fetch!(@original_counts, ammo_group_id)}
cpr={Map.get(@cprs, ammo_group_id)}
last_used_date={Map.get(@last_used_dates, ammo_group_id)}
current_user={@current_user}
show_container={true}
/>
</div>

View File

@ -4,7 +4,8 @@ defmodule CanneryWeb.ContainerLive.EditTagsComponent do
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, Containers, Containers.Container, Tags, Tags.Tag}
alias Cannery.{Accounts.User, Containers}
alias Cannery.Containers.{Container, Tag}
alias Phoenix.LiveView.Socket
@impl true
@ -22,7 +23,7 @@ defmodule CanneryWeb.ContainerLive.EditTagsComponent do
assigns,
socket
) do
tags = Tags.list_tags(current_user)
tags = Containers.list_tags(current_user)
{:ok, socket |> assign(assigns) |> assign(:tags, tags)}
end

View File

@ -35,17 +35,17 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
container_params
) do
changeset_action =
cond do
action in [:new, :clone] -> :insert
action == :edit -> :update
case action do
create when create in [:new, :clone] -> :insert
:edit -> :update
end
changeset =
cond do
action in [:new, :clone] ->
case action do
create when create in [:new, :clone] ->
container |> Container.create_changeset(user, container_params)
action == :edit ->
:edit ->
container |> Container.update_changeset(container_params)
end

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.ContainerLive.Index do
"""
use CanneryWeb, :live_view
alias Cannery.{Containers, Containers.Container, Repo}
alias Cannery.{Containers, Containers.Container}
alias Ecto.Changeset
@impl true
@ -22,10 +22,7 @@ defmodule CanneryWeb.ContainerLive.Index do
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{name: container_name} =
container =
Containers.get_container!(id, current_user)
|> Repo.preload([:tags, :ammo_groups])
%{name: container_name} = container = Containers.get_container!(id, current_user)
socket
|> assign(page_title: gettext("Edit %{name}", name: container_name), container: container)
@ -61,9 +58,7 @@ defmodule CanneryWeb.ContainerLive.Index do
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit_tags, %{"id" => id}) do
%{name: container_name} =
container =
Containers.get_container!(id, current_user) |> Repo.preload([:tags, :ammo_groups])
%{name: container_name} = container = Containers.get_container!(id, current_user)
page_title = gettext("Edit %{name} tags", name: container_name)
socket |> assign(page_title: page_title, container: container)
@ -119,10 +114,6 @@ defmodule CanneryWeb.ContainerLive.Index do
end
defp display_containers(%{assigns: %{search: search, current_user: current_user}} = socket) do
containers =
Containers.list_containers(search, current_user)
|> Repo.preload([:tags, :ammo_groups])
socket |> assign(:containers, containers)
socket |> assign(:containers, Containers.list_containers(search, current_user))
end
end

View File

@ -108,7 +108,11 @@
</.live_component>
<% else %>
<div class="w-full flex flex-row flex-wrap justify-center items-stretch">
<.container_card :for={container <- @containers} container={container}>
<.container_card
:for={container <- @containers}
container={container}
current_user={@current_user}
>
<:tag_actions>
<div class="mx-4 my-2">
<.link
@ -162,29 +166,30 @@
<% end %>
</div>
<.modal
:if={@live_action in [:new, :edit, :clone]}
return_to={Routes.container_index_path(Endpoint, :index)}
>
<.live_component
module={CanneryWeb.ContainerLive.FormComponent}
id={@container.id || :new}
title={@page_title}
action={@live_action}
container={@container}
return_to={Routes.container_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<.modal :if={@live_action == :edit_tags} return_to={Routes.container_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.ContainerLive.EditTagsComponent}
id={@container.id}
title={@page_title}
action={@live_action}
container={@container}
current_path={Routes.container_index_path(Endpoint, :edit_tags, @container)}
current_user={@current_user}
/>
</.modal>
<%= case @live_action do %>
<% modifying when modifying in [:new, :edit, :clone] -> %>
<.modal return_to={Routes.container_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.ContainerLive.FormComponent}
id={@container.id || :new}
title={@page_title}
action={@live_action}
container={@container}
return_to={Routes.container_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<% :edit_tags -> %>
<.modal return_to={Routes.container_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.ContainerLive.EditTagsComponent}
id={@container.id}
title={@page_title}
action={@live_action}
container={@container}
current_path={Routes.container_index_path(Endpoint, :edit_tags, @container)}
current_user={@current_user}
/>
</.modal>
<% _ -> %>
<% end %>

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.ContainerLive.Show do
"""
use CanneryWeb, :live_view
alias Cannery.{Accounts.User, Ammo, Containers, Containers.Container, Repo, Tags}
alias Cannery.{Accounts.User, ActivityLog, Ammo, Containers, Containers.Container}
alias CanneryWeb.Endpoint
alias Ecto.Changeset
alias Phoenix.LiveView.Socket
@ -30,7 +30,7 @@ defmodule CanneryWeb.ContainerLive.Show do
%{assigns: %{container: container, current_user: current_user}} = socket
) do
socket =
case Tags.get_tag(tag_id, current_user) do
case Containers.get_tag(tag_id, current_user) do
{:ok, tag} ->
_count = Containers.remove_tag!(container, tag, current_user)
@ -42,8 +42,8 @@ defmodule CanneryWeb.ContainerLive.Show do
socket |> put_flash(:info, prompt) |> render_container()
{:error, error_string} ->
socket |> put_flash(:error, error_string)
{:error, :not_found} ->
socket |> put_flash(:error, dgettext("errors", "Tag not found"))
end
{:noreply, socket}
@ -96,12 +96,11 @@ defmodule CanneryWeb.ContainerLive.Show do
id,
current_user
) do
%{name: container_name} =
container =
Containers.get_container!(id, current_user)
|> Repo.preload([:tags], force: true)
%{name: container_name} = container = Containers.get_container!(id, current_user)
ammo_groups = Ammo.list_ammo_groups_for_container(container, current_user, show_used)
original_counts = ammo_groups |> Ammo.get_original_counts(current_user)
cprs = ammo_groups |> Ammo.get_cprs(current_user)
last_used_dates = ammo_groups |> ActivityLog.get_last_used_dates(current_user)
page_title =
case live_action do
@ -110,7 +109,16 @@ defmodule CanneryWeb.ContainerLive.Show do
:edit_tags -> gettext("Edit %{name} tags", name: container_name)
end
socket |> assign(container: container, ammo_groups: ammo_groups, page_title: page_title)
socket
|> assign(
container: container,
round_count: Ammo.get_round_count_for_container!(container, current_user),
ammo_groups: ammo_groups,
original_counts: original_counts,
cprs: cprs,
last_used_dates: last_used_dates,
page_title: page_title
)
end
@spec render_container(Socket.t()) :: Socket.t()

View File

@ -20,21 +20,18 @@
<%= unless @ammo_groups |> Enum.empty?() do %>
<span class="rounded-lg title text-lg">
<%= if @show_used do %>
<%= gettext("Total packs:") %>
<% else %>
<%= gettext("Packs:") %>
<% end %>
<%= gettext("Packs:") %>
<%= @ammo_groups |> Enum.reject(fn %{count: count} -> count in [0, nil] end) |> Enum.count() %>
</span>
<span :if={@show_used} class="rounded-lg title text-lg">
<%= gettext("Total packs:") %>
<%= Enum.count(@ammo_groups) %>
</span>
<span class="rounded-lg title text-lg">
<%= if @show_used do %>
<%= gettext("Total rounds:") %>
<% else %>
<%= gettext("Rounds:") %>
<% end %>
<%= @container |> Containers.get_container_rounds!() %>
<%= gettext("Rounds:") %>
<%= @round_count %>
</span>
<% end %>
@ -130,40 +127,45 @@
</.live_component>
<% else %>
<div class="flex flex-wrap justify-center items-stretch">
<.ammo_group_card :for={ammo_group <- @ammo_groups} ammo_group={ammo_group} />
<.ammo_group_card
:for={%{id: ammo_group_id} = ammo_group <- @ammo_groups}
ammo_group={ammo_group}
original_count={Map.fetch!(@original_counts, ammo_group_id)}
cpr={Map.get(@cprs, ammo_group_id)}
last_used_date={Map.get(@last_used_dates, ammo_group_id)}
current_user={@current_user}
/>
</div>
<% end %>
<% end %>
</div>
</div>
<.modal
:if={@live_action == :edit}
return_to={Routes.container_show_path(Endpoint, :show, @container)}
>
<.live_component
module={CanneryWeb.ContainerLive.FormComponent}
id={@container.id}
title={@page_title}
action={@live_action}
container={@container}
return_to={Routes.container_show_path(Endpoint, :show, @container)}
current_user={@current_user}
/>
</.modal>
<.modal
:if={@live_action == :edit_tags}
return_to={Routes.container_show_path(Endpoint, :show, @container)}
>
<.live_component
module={CanneryWeb.ContainerLive.EditTagsComponent}
id={@container.id}
title={@page_title}
action={@live_action}
container={@container}
return_to={Routes.container_show_path(Endpoint, :show, @container)}
current_path={Routes.container_show_path(Endpoint, :edit_tags, @container)}
current_user={@current_user}
/>
</.modal>
<%= case @live_action do %>
<% :edit -> %>
<.modal return_to={Routes.container_show_path(Endpoint, :show, @container)}>
<.live_component
module={CanneryWeb.ContainerLive.FormComponent}
id={@container.id}
title={@page_title}
action={@live_action}
container={@container}
return_to={Routes.container_show_path(Endpoint, :show, @container)}
current_user={@current_user}
/>
</.modal>
<% :edit_tags -> %>
<.modal return_to={Routes.container_show_path(Endpoint, :show, @container)}>
<.live_component
module={CanneryWeb.ContainerLive.EditTagsComponent}
id={@container.id}
title={@page_title}
action={@live_action}
container={@container}
return_to={Routes.container_show_path(Endpoint, :show, @container)}
current_path={Routes.container_show_path(Endpoint, :edit_tags, @container)}
current_user={@current_user}
/>
</.modal>
<% _ -> %>
<% end %>

View File

@ -12,7 +12,6 @@ defmodule CanneryWeb.HomeLive do
@impl true
def mount(_params, _session, socket) do
admins = Accounts.list_users_by_role(:admin)
socket = socket |> assign(page_title: gettext("Home"), admins: admins, version: @version)
{:ok, socket}
{:ok, socket |> assign(page_title: gettext("Home"), admins: admins, version: @version)}
end
end

View File

@ -17,8 +17,7 @@
<hr class="hr" />
<ul class="flex flex-col space-y-4 text-center">
<li class="flex flex-col justify-center items-center
space-y-2">
<li class="flex flex-col justify-center items-center space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Easy to Use:") %>
</b>
@ -37,8 +36,7 @@
<%= gettext("Your data stays with you, period") %>
</p>
</li>
<li class="flex flex-col justify-center items-center
space-y-2">
<li class="flex flex-col justify-center items-center space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Simple:") %>
</b>
@ -81,9 +79,9 @@
<li class="flex flex-row justify-center space-x-2">
<b><%= gettext("Registration:") %></b>
<p>
<%= case Application.get_env(:cannery, Cannery.Accounts)[:registration] do
"public" -> gettext("Public Signups")
_ -> gettext("Invite Only")
<%= case Accounts.registration_mode() do
:public -> gettext("Public Signups")
:invite_only -> gettext("Invite Only")
end %>
</p>
</li>

View File

@ -29,8 +29,8 @@ defmodule CanneryWeb.InviteLive.Index do
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket
|> assign(page_title: gettext("Edit Invite"), invite: Invites.get_invite!(id, current_user))
invite = Invites.get_invite!(id, current_user)
socket |> assign(page_title: gettext("Edit Invite"), invite: invite)
end
defp apply_action(socket, :new, _params) do
@ -123,8 +123,7 @@ defmodule CanneryWeb.InviteLive.Index do
end
def handle_event("copy_to_clipboard", _params, socket) do
prompt = dgettext("prompts", "Copied to clipboard")
{:noreply, socket |> put_flash(:info, prompt)}
{:noreply, socket |> put_flash(:info, dgettext("prompts", "Copied to clipboard"))}
end
def handle_event(
@ -133,9 +132,7 @@ defmodule CanneryWeb.InviteLive.Index do
%{assigns: %{current_user: current_user}} = socket
) do
%{email: user_email} = Accounts.get_user!(id) |> Accounts.delete_user!(current_user)
prompt = dgettext("prompts", "%{user_email} deleted succesfully", user_email: user_email)
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end
@ -148,7 +145,8 @@ defmodule CanneryWeb.InviteLive.Index do
|> Map.get(:admin, [])
|> Enum.reject(fn %{id: user_id} -> user_id == current_user.id end)
use_counts = invites |> Invites.get_use_counts(current_user)
users = all_users |> Map.get(:user, [])
socket |> assign(invites: invites, admins: admins, users: users)
socket |> assign(invites: invites, use_counts: use_counts, admins: admins, users: users)
end
end

View File

@ -19,7 +19,12 @@
<% end %>
<div class="flex flex-col justify-center items-stretch space-y-4">
<.invite_card :for={invite <- @invites} invite={invite} current_user={@current_user}>
<.invite_card
:for={invite <- @invites}
invite={invite}
current_user={@current_user}
use_count={Map.get(@use_counts, invite.id)}
>
<:code_actions>
<form phx-submit="copy_to_clipboard">
<button

View File

@ -5,8 +5,12 @@ defmodule CanneryWeb.RangeLive.FormComponent do
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup}
alias Ecto.Changeset
alias Phoenix.LiveView.Socket
@impl true
def mount(socket), do: {:ok, socket |> assign(:ammo_group, nil)}
@impl true
@spec update(
%{
@ -19,28 +23,23 @@ defmodule CanneryWeb.RangeLive.FormComponent do
) :: {:ok, Socket.t()}
def update(
%{
shot_group: %ShotGroup{ammo_group_id: ammo_group_id} = shot_group,
shot_group: %ShotGroup{ammo_group_id: ammo_group_id},
current_user: current_user
} = assigns,
socket
) do
changeset = shot_group |> ShotGroup.update_changeset(current_user, %{})
)
when is_binary(ammo_group_id) do
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
{:ok, socket |> assign(assigns) |> assign(ammo_group: ammo_group, changeset: changeset)}
{:ok, socket |> assign(assigns) |> assign(:ammo_group, ammo_group) |> assign_changeset(%{})}
end
def update(%{shot_group: %ShotGroup{}} = assigns, socket) do
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
end
@impl true
def handle_event(
"validate",
%{"shot_group" => shot_group_params},
%{assigns: %{current_user: current_user, shot_group: shot_group}} = socket
) do
changeset =
shot_group
|> ShotGroup.update_changeset(current_user, shot_group_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
def handle_event("validate", %{"shot_group" => shot_group_params}, socket) do
{:noreply, socket |> assign_changeset(shot_group_params, :validate)}
end
def handle_event(
@ -61,4 +60,37 @@ defmodule CanneryWeb.RangeLive.FormComponent do
{:noreply, socket}
end
defp assign_changeset(
%{
assigns: %{
action: live_action,
current_user: user,
ammo_group: ammo_group,
shot_group: shot_group
}
} = socket,
shot_group_params,
action \\ nil
) do
default_action =
case live_action do
:add_shot_group -> :insert
editing when editing in [:edit, :edit_shot_group] -> :update
end
changeset =
case default_action do
:insert -> shot_group |> ShotGroup.create_changeset(user, ammo_group, shot_group_params)
:update -> shot_group |> ShotGroup.update_changeset(user, shot_group_params)
end
changeset =
case changeset |> Changeset.apply_action(action || default_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
socket |> assign(:changeset, changeset)
end
end

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.RangeLive.Index do
"""
use CanneryWeb, :live_view
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo, Repo}
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo}
alias CanneryWeb.Endpoint
alias Phoenix.LiveView.Socket
@ -104,16 +104,19 @@ defmodule CanneryWeb.RangeLive.Index do
@spec display_shot_groups(Socket.t()) :: Socket.t()
defp display_shot_groups(%{assigns: %{search: search, current_user: current_user}} = socket) do
shot_groups =
ActivityLog.list_shot_groups(search, current_user)
|> Repo.preload(ammo_group: :ammo_type)
shot_groups = ActivityLog.list_shot_groups(search, current_user)
ammo_groups = Ammo.list_staged_ammo_groups(current_user)
chart_data = shot_groups |> get_chart_data_for_shot_group()
original_counts = ammo_groups |> Ammo.get_original_counts(current_user)
cprs = ammo_groups |> Ammo.get_cprs(current_user)
last_used_dates = ammo_groups |> ActivityLog.get_last_used_dates(current_user)
socket
|> assign(
ammo_groups: ammo_groups,
original_counts: original_counts,
cprs: cprs,
last_used_dates: last_used_dates,
chart_data: chart_data,
shot_groups: shot_groups
)
@ -122,7 +125,6 @@ defmodule CanneryWeb.RangeLive.Index do
@spec get_chart_data_for_shot_group([ShotGroup.t()]) :: [map()]
defp get_chart_data_for_shot_group(shot_groups) do
shot_groups
|> Repo.preload(ammo_group: :ammo_type)
|> Enum.group_by(fn %{date: date} -> date end, fn %{count: count} -> count end)
|> Enum.map(fn {date, rounds} ->
sum = Enum.sum(rounds)

View File

@ -18,7 +18,14 @@
</.link>
<div class="w-full flex flex-row flex-wrap justify-center items-stretch">
<.ammo_group_card :for={ammo_group <- @ammo_groups} ammo_group={ammo_group}>
<.ammo_group_card
:for={%{id: ammo_group_id} = ammo_group <- @ammo_groups}
ammo_group={ammo_group}
original_count={Map.fetch!(@original_counts, ammo_group_id)}
cpr={Map.get(@cprs, ammo_group_id)}
last_used_date={Map.get(@last_used_dates, ammo_group_id)}
current_user={@current_user}
>
<button
type="button"
class="btn btn-primary"
@ -134,29 +141,30 @@
<% end %>
</div>
<.modal :if={@live_action == :edit} return_to={Routes.range_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.RangeLive.FormComponent}
id={@shot_group.id}
title={@page_title}
action={@live_action}
shot_group={@shot_group}
return_to={Routes.range_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<.modal
:if={@live_action == :add_shot_group}
return_to={Routes.range_index_path(Endpoint, :index)}
>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
id={:new}
title={@page_title}
action={@live_action}
ammo_group={@ammo_group}
return_to={Routes.range_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<%= case @live_action do %>
<% :edit -> %>
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.RangeLive.FormComponent}
id={@shot_group.id}
title={@page_title}
action={@live_action}
shot_group={@shot_group}
return_to={Routes.range_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<% :add_shot_group -> %>
<.modal return_to={Routes.range_index_path(Endpoint, :index)}>
<.live_component
module={CanneryWeb.Components.AddShotGroupComponent}
id={:new}
title={@page_title}
action={@live_action}
ammo_group={@ammo_group}
return_to={Routes.range_index_path(Endpoint, :index)}
current_user={@current_user}
/>
</.modal>
<% _ -> %>
<% end %>

View File

@ -1,11 +1,10 @@
defmodule CanneryWeb.TagLive.FormComponent do
@moduledoc """
Livecomponent that can update or create an Cannery.Tags.Tag
Livecomponent that can update or create an Cannery.Containers.Tag
"""
use CanneryWeb, :live_component
alias Cannery.Tags
alias Cannery.{Accounts.User, Tags.Tag}
alias Cannery.{Accounts.User, Containers, Containers.Tag}
alias Ecto.Changeset
alias Phoenix.LiveView.Socket
@ -56,7 +55,7 @@ defmodule CanneryWeb.TagLive.FormComponent do
tag_params
) do
socket =
case Tags.update_tag(tag, tag_params, current_user) do
case Containers.update_tag(tag, tag_params, current_user) do
{:ok, %{name: tag_name}} ->
prompt = dgettext("prompts", "%{name} updated successfully", name: tag_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
@ -74,7 +73,7 @@ defmodule CanneryWeb.TagLive.FormComponent do
tag_params
) do
socket =
case Tags.create_tag(tag_params, current_user) do
case Containers.create_tag(tag_params, current_user) do
{:ok, %{name: tag_name}} ->
prompt = dgettext("prompts", "%{name} created successfully", name: tag_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)

View File

@ -1,10 +1,10 @@
defmodule CanneryWeb.TagLive.Index do
@moduledoc """
Liveview to show a Cannery.Tags.Tag index
Liveview to show a Cannery.Containers.Tag index
"""
use CanneryWeb, :live_view
alias Cannery.{Tags, Tags.Tag}
alias Cannery.{Containers, Containers.Tag}
alias CanneryWeb.ViewHelpers
@impl true
@ -25,7 +25,7 @@ defmodule CanneryWeb.TagLive.Index do
socket
|> assign(
page_title: gettext("Edit Tag"),
tag: Tags.get_tag!(id, current_user)
tag: Containers.get_tag!(id, current_user)
)
end
@ -59,7 +59,9 @@ defmodule CanneryWeb.TagLive.Index do
@impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
%{name: tag_name} = Tags.get_tag!(id, current_user) |> Tags.delete_tag!(current_user)
%{name: tag_name} =
Containers.get_tag!(id, current_user) |> Containers.delete_tag!(current_user)
prompt = dgettext("prompts", "%{name} deleted succesfully", name: tag_name)
{:noreply, socket |> put_flash(:info, prompt) |> display_tags()}
end
@ -73,6 +75,6 @@ defmodule CanneryWeb.TagLive.Index do
end
defp display_tags(%{assigns: %{search: search, current_user: current_user}} = socket) do
socket |> assign(tags: Tags.list_tags(search, current_user))
socket |> assign(tags: Containers.list_tags(search, current_user))
end
end