memEx/lib/memex_web/components/contexts_table_component.ex

123 lines
3.1 KiB
Elixir
Raw Normal View History

2022-11-24 12:44:34 -05:00
defmodule MemexWeb.Components.ContextsTableComponent do
@moduledoc """
A component that displays a list of contexts
"""
use MemexWeb, :live_component
alias Ecto.UUID
2022-11-27 21:18:35 -05:00
alias Memex.{Accounts.User, Contexts.Context}
2022-11-24 12:44:34 -05:00
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
required(:contexts) => [Context.t()],
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, contexts: _contexts, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_contexts()
{:ok, socket}
end
defp display_contexts(
%{
assigns: %{
contexts: contexts,
current_user: current_user,
actions: actions
}
} = socket
) do
columns =
if actions == [] or current_user |> is_nil() do
[]
else
2023-03-19 15:28:53 -04:00
[%{label: gettext("actions"), key: :actions, sortable: false}]
2022-11-24 12:44:34 -05:00
end
columns = [
2022-11-26 14:51:18 -05:00
%{label: gettext("slug"), key: :slug},
2022-11-24 12:44:34 -05:00
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
| columns
]
rows =
contexts
|> Enum.map(fn context ->
context
|> get_row_data_for_context(%{
columns: columns,
current_user: current_user,
actions: actions
})
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div class="w-full">
<.live_component
module={MemexWeb.Components.TableComponent}
id={@id}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_context(Context.t(), additional_data :: map()) :: map()
defp get_row_data_for_context(context, %{columns: columns} = additional_data) do
columns
|> Map.new(fn %{key: key} ->
{key, get_value_for_key(key, context, additional_data)}
end)
end
@spec get_value_for_key(atom(), Context.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
2023-04-13 23:29:29 -04:00
defp get_value_for_key(:slug, %{slug: slug} = assigns, _additional_data) do
2022-11-26 14:51:18 -05:00
slug_block = ~H"""
2023-04-13 23:29:29 -04:00
<.link navigate={~p"/context/#{@slug}"} class="link">
2022-11-26 14:51:18 -05:00
<%= @slug %>
2022-11-24 12:44:34 -05:00
</.link>
"""
2022-11-26 14:51:18 -05:00
{slug, slug_block}
2022-11-24 12:44:34 -05:00
end
2023-04-13 23:29:29 -04:00
defp get_value_for_key(:tags, assigns, _additional_data) do
2022-12-15 22:33:10 -05:00
~H"""
<div class="flex flex-wrap justify-center space-x-1">
2023-04-13 23:29:29 -04:00
<.link :for={tag <- @tags} patch={~p"/contexts/#{tag}"} class="link">
2023-02-04 11:29:06 -05:00
<%= tag %>
</.link>
2022-12-15 22:33:10 -05:00
</div>
"""
2022-11-24 12:44:34 -05:00
end
defp get_value_for_key(:actions, context, %{actions: actions}) do
assigns = %{actions: actions, context: context}
~H"""
<div class="flex justify-center items-center space-x-4">
<%= render_slot(@actions, @context) %>
</div>
"""
end
defp get_value_for_key(key, context, _additional_data), do: context |> Map.get(key)
end