Files
BeetRoundServer/lib/beet_round_server_web/live/bidding_live/index.ex

79 lines
2.4 KiB
Elixir

defmodule BeetRoundServerWeb.BiddingLive.Index do
use BeetRoundServerWeb, :live_view
alias BeetRoundServer.Biddings
alias BeetRoundServer.BiddingRounds.BiddingRoundFacade
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope}>
<.header>
{@current_scope.user.email}
<:actions>
<.button variant="primary" navigate={~p"/biddings/new"}>
<.icon name="hero-plus" /> Neues Gebot
</.button>
</:actions>
</.header>
<%= if @bidding_round == 0 do %>
<p>Keine Bietrunde aktiv. Aktuell kein Bieten möglich!</p>
<% else %>
<p>Aktive Bietrunde: {@bidding_round} - Es kann geboten werden!</p>
<% end %>
<br />
<%= if @current_bidding do %>
<p><b>Aktuelles Gebot:</b></p>
<.list>
<:item title="Bietrunde">{@current_bidding.bidding_round}</:item>
<:item title="monatl. Betrag">{@current_bidding.amount} €</:item>
<:item title="Depot Wunsch 1">{@current_bidding.depot_wish_one}</:item>
<:item title="Depot Wunsch 2">{@current_bidding.depot_wish_two}</:item>
</.list>
<% else %>
<p>Noch kein Gebot abgegeben</p>
<% end %>
</Layouts.app>
"""
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