2022-11-17 22:38:52 -05:00
|
|
|
defmodule MemexWeb.Components.NotesTableComponent do
|
|
|
|
@moduledoc """
|
|
|
|
A component that displays a list of notes
|
|
|
|
"""
|
|
|
|
use MemexWeb, :live_component
|
|
|
|
alias Ecto.UUID
|
2022-11-27 21:18:35 -05:00
|
|
|
alias Memex.{Accounts.User, Notes.Note}
|
2022-11-17 22:38:52 -05:00
|
|
|
alias Phoenix.LiveView.{Rendered, Socket}
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
@spec update(
|
|
|
|
%{
|
|
|
|
required(:id) => UUID.t(),
|
|
|
|
required(:current_user) => User.t(),
|
|
|
|
required(:notes) => [Note.t()],
|
|
|
|
optional(any()) => any()
|
|
|
|
},
|
|
|
|
Socket.t()
|
|
|
|
) :: {:ok, Socket.t()}
|
|
|
|
def update(%{id: _id, notes: _notes, current_user: _current_user} = assigns, socket) do
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(assigns)
|
|
|
|
|> assign_new(:actions, fn -> [] end)
|
|
|
|
|> display_notes()
|
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp display_notes(
|
|
|
|
%{
|
|
|
|
assigns: %{
|
|
|
|
notes: notes,
|
|
|
|
current_user: current_user,
|
|
|
|
actions: actions
|
|
|
|
}
|
|
|
|
} = socket
|
|
|
|
) do
|
|
|
|
columns =
|
|
|
|
if actions == [] or current_user |> is_nil() do
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
[%{label: nil, key: :actions, sortable: false}]
|
|
|
|
end
|
|
|
|
|
|
|
|
columns = [
|
2022-11-26 14:51:18 -05:00
|
|
|
%{label: gettext("slug"), key: :slug},
|
2022-11-17 22:38:52 -05:00
|
|
|
%{label: gettext("tags"), key: :tags},
|
|
|
|
%{label: gettext("visibility"), key: :visibility}
|
|
|
|
| columns
|
|
|
|
]
|
|
|
|
|
|
|
|
rows =
|
|
|
|
notes
|
|
|
|
|> Enum.map(fn note ->
|
|
|
|
note
|
|
|
|
|> get_row_data_for_note(%{
|
|
|
|
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_note(Note.t(), additional_data :: map()) :: map()
|
|
|
|
defp get_row_data_for_note(note, %{columns: columns} = additional_data) do
|
|
|
|
columns
|
|
|
|
|> Map.new(fn %{key: key} ->
|
|
|
|
{key, get_value_for_key(key, note, additional_data)}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_value_for_key(atom(), Note.t(), additional_data :: map()) ::
|
|
|
|
any() | {any(), Rendered.t()}
|
2022-11-26 14:51:18 -05:00
|
|
|
defp get_value_for_key(:slug, %{slug: slug}, _additional_data) do
|
|
|
|
assigns = %{slug: slug}
|
2022-11-17 22:38:52 -05:00
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
slug_block = ~H"""
|
2022-11-17 22:38:52 -05:00
|
|
|
<.link
|
2022-11-26 14:51:18 -05:00
|
|
|
navigate={Routes.note_show_path(Endpoint, :show, @slug)}
|
2022-11-17 22:38:52 -05:00
|
|
|
class="link"
|
2022-11-26 14:51:18 -05:00
|
|
|
data-qa={"note-show-#{@slug}"}
|
2022-11-17 22:38:52 -05:00
|
|
|
>
|
2022-11-26 14:51:18 -05:00
|
|
|
<%= @slug %>
|
2022-11-17 22:38:52 -05:00
|
|
|
</.link>
|
|
|
|
"""
|
|
|
|
|
2022-11-26 14:51:18 -05:00
|
|
|
{slug, slug_block}
|
2022-11-17 22:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defp get_value_for_key(:tags, %{tags: tags}, _additional_data) do
|
2022-12-15 22:33:10 -05:00
|
|
|
assigns = %{tags: tags}
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
<div class="flex flex-wrap justify-center space-x-1">
|
2023-02-04 11:29:06 -05:00
|
|
|
<.link :for={tag <- @tags} patch={Routes.note_index_path(Endpoint, :search, tag)} class="link">
|
|
|
|
<%= tag %>
|
|
|
|
</.link>
|
2022-12-15 22:33:10 -05:00
|
|
|
</div>
|
|
|
|
"""
|
2022-11-17 22:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
defp get_value_for_key(:actions, note, %{actions: actions}) do
|
|
|
|
assigns = %{actions: actions, note: note}
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
<div class="flex justify-center items-center space-x-4">
|
|
|
|
<%= render_slot(@actions, @note) %>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_value_for_key(key, note, _additional_data), do: note |> Map.get(key)
|
|
|
|
end
|