style ammo type show page

This commit is contained in:
shibao 2022-01-31 23:20:57 -05:00
parent b75c0f7b1b
commit 9ca69ae3e4
4 changed files with 126 additions and 90 deletions

View File

@ -4,9 +4,9 @@ defmodule Cannery.Ammo do
"""
import Ecto.Query, warn: false
alias Cannery.Repo
alias Cannery.Ammo.AmmoType
alias Cannery.{Accounts.User, Repo}
alias Cannery.Ammo.{AmmoGroup, AmmoType}
alias Ecto.Changeset
@doc """
Returns the list of ammo_types.
@ -17,9 +17,8 @@ defmodule Cannery.Ammo do
[%AmmoType{}, ...]
"""
def list_ammo_types do
Repo.all(AmmoType)
end
@spec list_ammo_types() :: [AmmoType.t()]
def list_ammo_types, do: Repo.all(AmmoType)
@doc """
Gets a single ammo_type.
@ -35,6 +34,7 @@ defmodule Cannery.Ammo do
** (Ecto.NoResultsError)
"""
@spec get_ammo_type!(AmmoType.id()) :: AmmoType.t()
def get_ammo_type!(id), do: Repo.get!(AmmoType, id)
@doc """
@ -49,11 +49,9 @@ defmodule Cannery.Ammo do
{:error, %Ecto.Changeset{}}
"""
def create_ammo_type(attrs \\ %{}) do
%AmmoType{}
|> AmmoType.changeset(attrs)
|> Repo.insert()
end
@spec create_ammo_type(attrs :: map()) :: {:ok, AmmoType.t()} | {:error, Changeset.t()}
def create_ammo_type(attrs \\ %{}),
do: %AmmoType{} |> AmmoType.changeset(attrs) |> Repo.insert()
@doc """
Updates a ammo_type.
@ -67,11 +65,10 @@ defmodule Cannery.Ammo do
{:error, %Ecto.Changeset{}}
"""
def update_ammo_type(%AmmoType{} = ammo_type, attrs) do
ammo_type
|> AmmoType.changeset(attrs)
|> Repo.update()
end
@spec update_ammo_type(AmmoType.t(), attrs :: map()) ::
{:ok, AmmoType.t()} | {:error, Changeset.t()}
def update_ammo_type(%AmmoType{} = ammo_type, attrs),
do: ammo_type |> AmmoType.changeset(attrs) |> Repo.update()
@doc """
Deletes a ammo_type.
@ -85,9 +82,20 @@ defmodule Cannery.Ammo do
{:error, %Ecto.Changeset{}}
"""
def delete_ammo_type(%AmmoType{} = ammo_type) do
Repo.delete(ammo_type)
end
@spec delete_ammo_type(AmmoType.t()) :: {:ok, AmmoType.t()} | {:error, Changeset.t()}
def delete_ammo_type(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete()
@doc """
Deletes a ammo_type.
## Examples
iex> delete_ammo_type(ammo_type)
%AmmoType{}
"""
@spec delete_ammo_type!(AmmoType.t()) :: AmmoType.t()
def delete_ammo_type!(%AmmoType{} = ammo_type), do: ammo_type |> Repo.delete!()
@doc """
Returns an `%Ecto.Changeset{}` for tracking ammo_type changes.
@ -98,23 +106,28 @@ defmodule Cannery.Ammo do
%Ecto.Changeset{data: %AmmoType{}}
"""
def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}) do
AmmoType.changeset(ammo_type, attrs)
end
alias Cannery.Ammo.AmmoGroup
@spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type()) :: Changeset.t()
@spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type(), attrs :: map()) :: Changeset.t()
def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}),
do: AmmoType.changeset(ammo_type, attrs)
@doc """
Returns the list of ammo_groups.
## Examples
iex> list_ammo_groups()
iex> list_ammo_groups(%User{id: 123})
[%AmmoGroup{}, ...]
iex> list_ammo_groups(123)
[%AmmoGroup{}, ...]
"""
def list_ammo_groups do
Repo.all(AmmoGroup)
@spec list_ammo_groups(User.t() | User.id()) :: [AmmoGroup.t()]
def list_ammo_groups(%{id: user_id}), do: list_ammo_groups(user_id)
def list_ammo_groups(user_id) do
Repo.all(from am in AmmoGroup, where: am.user_id == ^user_id)
end
@doc """
@ -131,6 +144,7 @@ defmodule Cannery.Ammo do
** (Ecto.NoResultsError)
"""
@spec get_ammo_group!(AmmoGroup.id()) :: AmmoGroup.t()
def get_ammo_group!(id), do: Repo.get!(AmmoGroup, id)
@doc """
@ -145,11 +159,9 @@ defmodule Cannery.Ammo do
{:error, %Ecto.Changeset{}}
"""
def create_ammo_group(attrs \\ %{}) do
%AmmoGroup{}
|> AmmoGroup.changeset(attrs)
|> Repo.insert()
end
@spec create_ammo_group(attrs :: map()) :: {:ok, AmmoGroup.t()} | {:error, Changeset.t()}
def create_ammo_group(attrs \\ %{}),
do: %AmmoGroup{} |> AmmoGroup.changeset(attrs) |> Repo.insert()
@doc """
Updates a ammo_group.
@ -163,11 +175,10 @@ defmodule Cannery.Ammo do
{:error, %Ecto.Changeset{}}
"""
def update_ammo_group(%AmmoGroup{} = ammo_group, attrs) do
ammo_group
|> AmmoGroup.changeset(attrs)
|> Repo.update()
end
@spec update_ammo_group(AmmoGroup.t(), attrs :: map()) ::
{:ok, AmmoGroup.t()} | {:error, Changeset.t()}
def update_ammo_group(%AmmoGroup{} = ammo_group, attrs),
do: ammo_group |> AmmoGroup.changeset(attrs) |> Repo.update()
@doc """
Deletes a ammo_group.
@ -181,9 +192,20 @@ defmodule Cannery.Ammo do
{:error, %Ecto.Changeset{}}
"""
def delete_ammo_group(%AmmoGroup{} = ammo_group) do
Repo.delete(ammo_group)
end
@spec delete_ammo_group(AmmoGroup.t()) :: {:ok, AmmoGroup.t()} | {:error, Changeset.t()}
def delete_ammo_group(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete()
@doc """
Deletes a ammo_group.
## Examples
iex> delete_ammo_group!(ammo_group)
%AmmoGroup{}
"""
@spec delete_ammo_group!(AmmoGroup.t()) :: AmmoGroup.t()
def delete_ammo_group!(%AmmoGroup{} = ammo_group), do: ammo_group |> Repo.delete!()
@doc """
Returns an `%Ecto.Changeset{}` for tracking ammo_group changes.
@ -194,7 +216,8 @@ defmodule Cannery.Ammo do
%Ecto.Changeset{data: %AmmoGroup{}}
"""
def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}) do
AmmoGroup.changeset(ammo_group, attrs)
end
@spec change_ammo_group(AmmoGroup.t()) :: Changeset.t(AmmoGroup.t())
@spec change_ammo_group(AmmoGroup.t(), attrs :: map()) :: Changeset.t(AmmoGroup.t())
def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}),
do: AmmoGroup.changeset(ammo_group, attrs)
end

View File

@ -20,6 +20,12 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
|> assign(:ammo_type, Ammo.get_ammo_type!(id))}
end
@impl true
def handle_event("delete", _, socket) do
socket.assigns.ammo_type |> Ammo.delete_ammo_type!()
{:noreply, socket |> push_redirect(to: Routes.ammo_type_index_path(socket, :index))}
end
defp page_title(:show), do: "Show Ammo type"
defp page_title(:edit), do: "Edit Ammo type"
end

View File

@ -0,0 +1,54 @@
<div class="mx-auto space-y-4 max-w-3xl flex flex-col justify-center items-center">
<h1 class="title text-2xl title-primary-500">
<%= @ammo_type.name %>
</h1>
<div class="flex space-x-4 justify-center items-center text-primary-500">
<%= live_redirect "Back", to: Routes.ammo_type_index_path(@socket, :index), class: "link" %>
<%= live_patch "Edit", to: Routes.ammo_type_show_path(@socket, :edit, @ammo_type), class: "button" %>
<%= link("Delete",
to: "#",
class: "link",
phx_click: "delete",
data: [confirm: "Are you sure you want to delete #{@ammo_type.name}?"]
) %>
</div>
<hr class="w-full">
<ul class="text-center">
<li>
<strong>Desc:</strong>
<%= @ammo_type.desc %>
</li>
<li>
<strong>Case material:</strong>
<%= @ammo_type.case_material %>
</li>
<li>
<strong>Bullet type:</strong>
<%= @ammo_type.bullet_type %>
</li>
<li>
<strong>Grain:</strong>
<%= @ammo_type.grain %>
</li>
<li>
<strong>Manufacturer:</strong>
<%= @ammo_type.manufacturer %>
</li>
</ul>
</div>
<%= if @live_action in [:edit] do %>
<%= live_modal CanneryWeb.AmmoTypeLive.FormComponent,
id: @ammo_type.id,
title: @page_title,
action: @live_action,
ammo_type: @ammo_type,
return_to: Routes.ammo_type_show_path(@socket, :show, @ammo_type) %>
<% end %>

View File

@ -1,47 +0,0 @@
<h1>Show Ammo type</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal CanneryWeb.AmmoTypeLive.FormComponent,
id: @ammo_type.id,
title: @page_title,
action: @live_action,
ammo_type: @ammo_type,
return_to: Routes.ammo_type_show_path(@socket, :show, @ammo_type) %>
<% end %>
<ul>
<li>
<strong>Name:</strong>
<%= @ammo_type.name %>
</li>
<li>
<strong>Desc:</strong>
<%= @ammo_type.desc %>
</li>
<li>
<strong>Case material:</strong>
<%= @ammo_type.case_material %>
</li>
<li>
<strong>Bullet type:</strong>
<%= @ammo_type.bullet_type %>
</li>
<li>
<strong>Weight:</strong>
<%= @ammo_type.weight %>
</li>
<li>
<strong>Manufacturer:</strong>
<%= @ammo_type.manufacturer %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.ammo_type_show_path(@socket, :edit, @ammo_type), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.ammo_type_index_path(@socket, :index) %></span>