defmodule CanneryWeb.Components.ShotRecordTableComponent do @moduledoc """ A component that displays a list of shot records """ use CanneryWeb, :live_component alias Cannery.{Accounts.User, ActivityLog.ShotRecord, Ammo, ComparableDate} alias Ecto.UUID alias Phoenix.LiveView.{Rendered, Socket} @impl true @spec update( %{ required(:id) => UUID.t(), required(:current_user) => User.t(), optional(:shot_records) => [ShotRecord.t()], optional(:actions) => Rendered.t(), optional(any()) => any() }, Socket.t() ) :: {:ok, Socket.t()} def update( %{id: _id, shot_records: _shot_records, current_user: _current_user} = assigns, socket ) do socket = socket |> assign(assigns) |> assign_new(:actions, fn -> [] end) |> display_shot_records() {:ok, socket} end defp display_shot_records( %{ assigns: %{ shot_records: shot_records, current_user: current_user, actions: actions } } = socket ) do columns = [ %{label: gettext("Ammo"), key: :name}, %{label: gettext("Rounds shot"), key: :count}, %{label: gettext("Notes"), key: :notes}, %{label: gettext("Date"), key: :date, type: ComparableDate}, %{label: gettext("Actions"), key: :actions, sortable: false} ] packs = shot_records |> Enum.map(fn %{pack_id: pack_id} -> pack_id end) |> Ammo.get_packs(current_user) extra_data = %{current_user: current_user, actions: actions, packs: packs} rows = shot_records |> Enum.map(fn shot_record -> shot_record |> get_row_data_for_shot_record(columns, extra_data) end) socket |> assign( columns: columns, rows: rows ) end @impl true def render(assigns) do ~H"""