defmodule MemexWeb.Components.NotesTableComponent do @moduledoc """ A component that displays a list of notes """ use MemexWeb, :live_component alias Ecto.UUID alias Memex.{Accounts.User, Notes, Notes.Note} alias MemexWeb.Endpoint 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 = [ %{label: gettext("title"), key: :title}, %{label: gettext("content"), key: :content}, %{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"""