|
| 1 | +defmodule GalliumWeb.BackOffice.TablesLive.Index do |
| 2 | + @moduledoc """ |
| 3 | + LiveView showing seating arrangement: one rectangle per table with avatars |
| 4 | + for each person around it. |
| 5 | + """ |
| 6 | + use GalliumWeb, :live_view |
| 7 | + |
| 8 | + import GalliumWeb.BackOffice.Components.BackofficeLayout |
| 9 | + |
| 10 | + alias Gallium.Ticketing |
| 11 | + |
| 12 | + attr :name, :string, required: true |
| 13 | + attr :people, :list, required: true |
| 14 | + |
| 15 | + def table_card(assigns) do |
| 16 | + {top, bottom} = split_people(assigns.people) |
| 17 | + assigns = assigns |> assign(:top, top) |> assign(:bottom, bottom) |
| 18 | + |
| 19 | + ~H""" |
| 20 | + <div class="bg-white rounded-xl border border-gray-200 shadow-sm px-6 py-8"> |
| 21 | + <div class="flex flex-col items-center gap-3"> |
| 22 | + <div class="flex justify-center gap-3 min-h-10"> |
| 23 | + <.avatar :for={person <- @top} person={person} /> |
| 24 | + </div> |
| 25 | +
|
| 26 | + <div class="w-full max-w-md h-28 rounded-lg bg-olive text-white flex items-center justify-center shadow-inner"> |
| 27 | + <span class="font-amarante text-2xl uppercase tracking-widest">{@name}</span> |
| 28 | + </div> |
| 29 | +
|
| 30 | + <div class="flex justify-center gap-3 min-h-10"> |
| 31 | + <.avatar :for={person <- @bottom} person={person} /> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + """ |
| 36 | + end |
| 37 | + |
| 38 | + attr :person, :map, required: true |
| 39 | + |
| 40 | + defp avatar(assigns) do |
| 41 | + ~H""" |
| 42 | + <div class="relative group"> |
| 43 | + <div |
| 44 | + class="w-10 h-10 rounded-full bg-olive-100 text-olive-700 border border-olive-200 flex items-center justify-center font-amarante text-sm font-bold cursor-default select-none" |
| 45 | + title={@person.full_name} |
| 46 | + > |
| 47 | + {@person.initials} |
| 48 | + </div> |
| 49 | + <span class="pointer-events-none absolute left-1/2 -translate-x-1/2 -top-9 whitespace-nowrap rounded bg-gray-900 text-white text-xs px-2 py-1 opacity-0 group-hover:opacity-100 transition-opacity font-cormorant z-10"> |
| 50 | + {@person.full_name} |
| 51 | + </span> |
| 52 | + </div> |
| 53 | + """ |
| 54 | + end |
| 55 | + |
| 56 | + defp split_people(people) do |
| 57 | + count = length(people) |
| 58 | + half = div(count + 1, 2) |
| 59 | + Enum.split(people, half) |
| 60 | + end |
| 61 | + |
| 62 | + def mount(_params, _session, socket) do |
| 63 | + tables = |
| 64 | + Ticketing.list_tables_with_attendees() |
| 65 | + |> Enum.map(fn {name, people} -> |
| 66 | + %{ |
| 67 | + name: name, |
| 68 | + people: Enum.map(people, fn p -> Map.put(p, :initials, initials(p.full_name)) end) |
| 69 | + } |
| 70 | + end) |
| 71 | + |
| 72 | + socket = |
| 73 | + socket |
| 74 | + |> assign(:current_page, :tables) |
| 75 | + |> assign(:sidebar_open, false) |
| 76 | + |> assign(:tables, tables) |
| 77 | + |
| 78 | + {:ok, socket} |
| 79 | + end |
| 80 | + |
| 81 | + def handle_event("toggle_sidebar", _params, socket) do |
| 82 | + {:noreply, assign(socket, :sidebar_open, !socket.assigns.sidebar_open)} |
| 83 | + end |
| 84 | + |
| 85 | + defp initials(nil), do: "?" |
| 86 | + |
| 87 | + defp initials(full_name) do |
| 88 | + parts = |
| 89 | + full_name |
| 90 | + |> String.split(~r/\s+/, trim: true) |
| 91 | + |> Enum.reject(&(&1 == "")) |
| 92 | + |
| 93 | + case parts do |
| 94 | + [] -> |
| 95 | + "?" |
| 96 | + |
| 97 | + [one] -> |
| 98 | + String.upcase(String.slice(one, 0, 1)) |
| 99 | + |
| 100 | + list -> |
| 101 | + String.upcase(String.slice(List.first(list), 0, 1) <> String.slice(List.last(list), 0, 1)) |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments