defmodule BeetRoundServerWeb.BiddingLive.Index do use BeetRoundServerWeb, :live_view alias BeetRoundServer.Biddings alias BeetRoundServer.BiddingRounds.BiddingRoundFacade @impl true def render(assigns) do ~H""" <.header> {@current_scope.user.email} <:actions> <.button variant="primary" navigate={~p"/biddings/new"}> <.icon name="hero-plus" /> Neues Gebot <%= if @bidding_round == 0 do %>

Keine Bietrunde aktiv. Aktuell kein Bieten möglich!

<% else %>

Aktive Bietrunde: {@bidding_round} - Es kann geboten werden!

<% end %>
<%= if @current_bidding do %>

Aktuelles Gebot:

<.list> <:item title="Bietrunde">{@current_bidding.bidding_round} <:item title="monatl. Betrag">{@current_bidding.amount} € <:item title="Depot Wunsch 1">{@current_bidding.depot_wish_one} <:item title="Depot Wunsch 2">{@current_bidding.depot_wish_two} <% else %>

Noch kein Gebot abgegeben

<% end %>
""" end @impl true def mount(_params, _session, socket) do if connected?(socket) do Biddings.subscribe_biddings(socket.assigns.current_scope) end current_round = BiddingRoundFacade.get_current_round() current_bidding = Biddings.get_most_recent_bidding(socket.assigns.current_scope) {:ok, socket |> assign(:page_title, "Aktuelles Gebot") |> assign(bidding_round: current_round) |> assign(current_bidding: current_bidding) |> stream(:biddings, list_biddings(socket.assigns.current_scope))} end @impl true def handle_event("delete", %{"id" => id}, socket) do bidding = Biddings.get_bidding!(socket.assigns.current_scope, id) {:ok, _} = Biddings.delete_bidding(socket.assigns.current_scope, bidding) {:noreply, stream_delete(socket, :biddings, bidding)} end @impl true def handle_info({type, %BeetRoundServer.Biddings.Bidding{}}, socket) when type in [:created, :updated, :deleted] do {:noreply, stream(socket, :biddings, list_biddings(socket.assigns.current_scope), reset: true)} end defp list_biddings(current_scope) do Biddings.list_biddings(current_scope) end end