move components to Components context

This commit is contained in:
2022-02-11 22:47:33 -05:00
parent 993d583fdd
commit c0b3de75a9
19 changed files with 47 additions and 40 deletions

View File

@ -1,48 +0,0 @@
defmodule CanneryWeb.AmmoGroupLive.AmmoGroupCard do
@moduledoc """
Display card for an ammo group
"""
use CanneryWeb, :component
alias Cannery.Repo
alias CanneryWeb.Endpoint
def ammo_group_card(assigns) do
assigns = assigns |> assign(:ammo_group, assigns.ammo_group |> Repo.preload(:ammo_type))
~H"""
<div
id={"ammo_group-#{@ammo_group.id}"}
class="px-8 py-4 flex flex-col justify-center items-center
border border-gray-400 rounded-lg shadow-lg hover:shadow-md"
>
<%= live_redirect to: Routes.ammo_group_show_path(Endpoint, :show, @ammo_group),
class: "mb-2 link" do %>
<h1 class="title text-xl title-primary-500">
<%= @ammo_group.ammo_type.name %>
</h1>
<% end %>
<div class="flex flex-col justify-center items-center">
<span class="rounded-lg title text-lg">
<%= gettext("Count:") %>
<%= @ammo_group.count %>
</span>
<%= if @ammo_group.notes do %>
<span class="rounded-lg title text-lg">
<%= gettext("Notes:") %>
<%= @ammo_group.notes %>
</span>
<% end %>
<%= if @ammo_group.price_paid do %>
<span class="rounded-lg title text-lg">
<%= gettext("Price paid:") %> $ <%= @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2) %>
</span>
<% end %>
</div>
</div>
"""
end
end

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
"""
use CanneryWeb, :live_view
import CanneryWeb.ContainerLive.ContainerCard
import CanneryWeb.Components.ContainerCard
alias Cannery.{Ammo, Repo}
@impl true

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
"""
use CanneryWeb, :live_view
import CanneryWeb.AmmoGroupLive.AmmoGroupCard
import CanneryWeb.Components.AmmoGroupCard
alias Cannery.Ammo
@impl true

View File

@ -1,52 +0,0 @@
defmodule CanneryWeb.ContainerLive.ContainerCard do
@moduledoc """
Display card for a container
"""
use CanneryWeb, :component
alias CanneryWeb.Endpoint
def container_card(assigns) do
~H"""
<div
id={"container-#{@container.id}"}
class="px-8 py-4 flex flex-col justify-center items-center
border border-gray-400 rounded-lg shadow-lg hover:shadow-md"
>
<div class="mb-4 flex flex-col justify-center items-center">
<%= live_redirect to: Routes.container_show_path(Endpoint, :show, @container),
class: "link" do %>
<h1 class="px-4 py-2 rounded-lg title text-xl">
<%= @container.name %>
</h1>
<% end %>
<%= if @container.desc do %>
<span class="rounded-lg title text-lg">
<%= gettext("Description:") %>
<%= @container.desc %>
</span>
<% end %>
<span class="rounded-lg title text-lg">
<%= gettext("Type:") %>
<%= @container.type %>
</span>
<%= if @container.location do %>
<span class="rounded-lg title text-lg">
<%= gettext("Location:") %>
<%= @container.location %>
</span>
<% end %>
</div>
<%= if assigns |> Map.has_key?(:inner_block) do %>
<div class="flex space-x-4 justify-center items-center">
<%= render_slot(@inner_block) %>
</div>
<% end %>
</div>
"""
end
end

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.ContainerLive.Index do
"""
use CanneryWeb, :live_view
import CanneryWeb.ContainerLive.ContainerCard
import CanneryWeb.Components.ContainerCard
alias Cannery.{Containers, Containers.Container}
alias Ecto.Changeset

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.ContainerLive.Show do
"""
use CanneryWeb, :live_view
import CanneryWeb.AmmoGroupLive.AmmoGroupCard
import CanneryWeb.Components.AmmoGroupCard
alias Cannery.{Containers, Repo}
alias Ecto.Changeset

View File

@ -6,9 +6,10 @@ defmodule CanneryWeb.LiveHelpers do
import Phoenix.LiveView.Helpers
import Phoenix.LiveView, only: [assign_new: 3]
alias Cannery.Accounts
alias CanneryWeb.Components.Modal
@doc """
Renders a component inside the `CanneryWeb.ModalComponent` component.
Renders a component inside the `Modal` component.
The rendered modal receives a `:return_to` option to properly update
the URL when the modal is closed.
@ -23,8 +24,7 @@ defmodule CanneryWeb.LiveHelpers do
"""
def live_modal(component, opts) do
path = Keyword.fetch!(opts, :return_to)
modal_opts = [id: :modal, return_to: path, component: component, opts: opts]
live_component(CanneryWeb.ModalComponent, modal_opts)
live_component(Modal, id: :modal, return_to: path, component: component, opts: opts)
end
def assign_defaults(socket, %{"user_token" => user_token} = _session) do

View File

@ -1,43 +0,0 @@
defmodule CanneryWeb.ModalComponent do
@moduledoc """
Livecomponent that displays a floating modal window
"""
use CanneryWeb, :live_component
@impl true
def render(assigns) do
~H"""
<div
id={@id}
class="fixed z-10 left-0 top-0
w-full h-full overflow-hidden
p-8 flex flex-col justify-center items-center"
style="opacity: 1 !important; background-color: rgba(0,0,0,0.4);"
phx-capture-click="close"
phx-window-keydown="close"
phx-key="escape"
phx-target={"#{@id}"}
phx-page-loading
>
<div class="w-full max-w-3xl max-h-128 relative overflow-y-auto
flex flex-col justify-start items-center
bg-white border-2 rounded-lg">
<%= live_patch to: @return_to,
class:
"absolute top-8 right-10 text-gray-500 hover:text-gray-800 transition-all duration-500 ease-in-out" do %>
<i class="fa-fw fa-lg fas fa-times"></i>
<% end %>
<div class="p-8 flex flex-col space-y-4 justify-start items-center">
<%= live_component(@component, @opts) %>
</div>
</div>
</div>
"""
end
@impl true
def handle_event("close", _, socket) do
{:noreply, push_patch(socket, to: socket.assigns.return_to)}
end
end

View File

@ -4,7 +4,7 @@ defmodule CanneryWeb.TagLive.Index do
"""
use CanneryWeb, :live_view
import CanneryWeb.TagLive.TagCard
import CanneryWeb.Components.TagCard
alias Cannery.Tags
alias Cannery.Tags.Tag

View File

@ -1,40 +0,0 @@
defmodule CanneryWeb.TagLive.TagCard do
@moduledoc """
Display card for a tag
"""
use CanneryWeb, :component
alias CanneryWeb.Endpoint
def tag_card(assigns) do
~H"""
<div
id={"tag-#{@tag.id}"}
class="mx-4 my-2 px-8 py-4 space-x-4 flex justify-center items-center
border border-gray-400 rounded-lg shadow-lg hover:shadow-md"
>
<h1
class="px-4 py-2 rounded-lg title text-xl"
style={"color: #{@tag.text_color}; background-color: #{@tag.bg_color}"}
>
<%= @tag.name %>
</h1>
<%= live_patch to: Routes.tag_index_path(Endpoint, :edit, @tag),
class: "text-primary-500 link" do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
class: "text-primary-500 link",
phx_click: "delete",
phx_value_id: @tag.id,
data: [
confirm: dgettext("prompts", "Are you sure you want to delete %{name}?", name: @tag.name)
] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
</div>
"""
end
end