After "mix phx.gen.live Biddings Bidding biddings bidding_round:integer amount:integer depot_wish_one:string depot_wish_two:string".
This commit is contained in:
101
lib/beet_round_server_web/live/bidding_live/form.ex
Normal file
101
lib/beet_round_server_web/live/bidding_live/form.ex
Normal file
@ -0,0 +1,101 @@
|
||||
defmodule BeetRoundServerWeb.BiddingLive.Form do
|
||||
use BeetRoundServerWeb, :live_view
|
||||
|
||||
alias BeetRoundServer.Biddings
|
||||
alias BeetRoundServer.Biddings.Bidding
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
||||
<.header>
|
||||
{@page_title}
|
||||
<:subtitle>Use this form to manage bidding records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.form for={@form} id="bidding-form" phx-change="validate" phx-submit="save">
|
||||
<.input field={@form[:bidding_round]} type="number" label="Bidding round" />
|
||||
<.input field={@form[:amount]} type="number" label="Amount" />
|
||||
<.input field={@form[:depot_wish_one]} type="text" label="Depot wish one" />
|
||||
<.input field={@form[:depot_wish_two]} type="text" label="Depot wish two" />
|
||||
<footer>
|
||||
<.button phx-disable-with="Saving..." variant="primary">Save Bidding</.button>
|
||||
<.button navigate={return_path(@current_scope, @return_to, @bidding)}>Cancel</.button>
|
||||
</footer>
|
||||
</.form>
|
||||
</Layouts.app>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(params, _session, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:return_to, return_to(params["return_to"]))
|
||||
|> apply_action(socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp return_to("show"), do: "show"
|
||||
defp return_to(_), do: "index"
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
bidding = Biddings.get_bidding!(socket.assigns.current_scope, id)
|
||||
|
||||
socket
|
||||
|> assign(:page_title, "Edit Bidding")
|
||||
|> assign(:bidding, bidding)
|
||||
|> assign(:form, to_form(Biddings.change_bidding(socket.assigns.current_scope, bidding)))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
bidding = %Bidding{user_id: socket.assigns.current_scope.user.id}
|
||||
|
||||
socket
|
||||
|> assign(:page_title, "New Bidding")
|
||||
|> assign(:bidding, bidding)
|
||||
|> assign(:form, to_form(Biddings.change_bidding(socket.assigns.current_scope, bidding)))
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"bidding" => bidding_params}, socket) do
|
||||
changeset = Biddings.change_bidding(socket.assigns.current_scope, socket.assigns.bidding, bidding_params)
|
||||
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"bidding" => bidding_params}, socket) do
|
||||
save_bidding(socket, socket.assigns.live_action, bidding_params)
|
||||
end
|
||||
|
||||
defp save_bidding(socket, :edit, bidding_params) do
|
||||
case Biddings.update_bidding(socket.assigns.current_scope, socket.assigns.bidding, bidding_params) do
|
||||
{:ok, bidding} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Bidding updated successfully")
|
||||
|> push_navigate(
|
||||
to: return_path(socket.assigns.current_scope, socket.assigns.return_to, bidding)
|
||||
)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_bidding(socket, :new, bidding_params) do
|
||||
case Biddings.create_bidding(socket.assigns.current_scope, bidding_params) do
|
||||
{:ok, bidding} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Bidding created successfully")
|
||||
|> push_navigate(
|
||||
to: return_path(socket.assigns.current_scope, socket.assigns.return_to, bidding)
|
||||
)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp return_path(_scope, "index", _bidding), do: ~p"/biddings"
|
||||
defp return_path(_scope, "show", bidding), do: ~p"/biddings/#{bidding}"
|
||||
end
|
||||
76
lib/beet_round_server_web/live/bidding_live/index.ex
Normal file
76
lib/beet_round_server_web/live/bidding_live/index.ex
Normal file
@ -0,0 +1,76 @@
|
||||
defmodule BeetRoundServerWeb.BiddingLive.Index do
|
||||
use BeetRoundServerWeb, :live_view
|
||||
|
||||
alias BeetRoundServer.Biddings
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
||||
<.header>
|
||||
Listing Biddings
|
||||
<:actions>
|
||||
<.button variant="primary" navigate={~p"/biddings/new"}>
|
||||
<.icon name="hero-plus" /> New Bidding
|
||||
</.button>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table
|
||||
id="biddings"
|
||||
rows={@streams.biddings}
|
||||
row_click={fn {_id, bidding} -> JS.navigate(~p"/biddings/#{bidding}") end}
|
||||
>
|
||||
<:col :let={{_id, bidding}} label="Bidding round">{bidding.bidding_round}</:col>
|
||||
<:col :let={{_id, bidding}} label="Amount">{bidding.amount}</:col>
|
||||
<:col :let={{_id, bidding}} label="Depot wish one">{bidding.depot_wish_one}</:col>
|
||||
<:col :let={{_id, bidding}} label="Depot wish two">{bidding.depot_wish_two}</:col>
|
||||
<:action :let={{_id, bidding}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/biddings/#{bidding}"}>Show</.link>
|
||||
</div>
|
||||
<.link navigate={~p"/biddings/#{bidding}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={{id, bidding}}>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: bidding.id}) |> hide("##{id}")}
|
||||
data-confirm="Are you sure?"
|
||||
>
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
</Layouts.app>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
if connected?(socket) do
|
||||
Biddings.subscribe_biddings(socket.assigns.current_scope)
|
||||
end
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:page_title, "Listing Biddings")
|
||||
|> 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
|
||||
67
lib/beet_round_server_web/live/bidding_live/show.ex
Normal file
67
lib/beet_round_server_web/live/bidding_live/show.ex
Normal file
@ -0,0 +1,67 @@
|
||||
defmodule BeetRoundServerWeb.BiddingLive.Show do
|
||||
use BeetRoundServerWeb, :live_view
|
||||
|
||||
alias BeetRoundServer.Biddings
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<Layouts.app flash={@flash} current_scope={@current_scope}>
|
||||
<.header>
|
||||
Bidding {@bidding.id}
|
||||
<:subtitle>This is a bidding record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.button navigate={~p"/biddings"}>
|
||||
<.icon name="hero-arrow-left" />
|
||||
</.button>
|
||||
<.button variant="primary" navigate={~p"/biddings/#{@bidding}/edit?return_to=show"}>
|
||||
<.icon name="hero-pencil-square" /> Edit bidding
|
||||
</.button>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Bidding round">{@bidding.bidding_round}</:item>
|
||||
<:item title="Amount">{@bidding.amount}</:item>
|
||||
<:item title="Depot wish one">{@bidding.depot_wish_one}</:item>
|
||||
<:item title="Depot wish two">{@bidding.depot_wish_two}</:item>
|
||||
</.list>
|
||||
</Layouts.app>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(%{"id" => id}, _session, socket) do
|
||||
if connected?(socket) do
|
||||
Biddings.subscribe_biddings(socket.assigns.current_scope)
|
||||
end
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:page_title, "Show Bidding")
|
||||
|> assign(:bidding, Biddings.get_bidding!(socket.assigns.current_scope, id))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info(
|
||||
{:updated, %BeetRoundServer.Biddings.Bidding{id: id} = bidding},
|
||||
%{assigns: %{bidding: %{id: id}}} = socket
|
||||
) do
|
||||
{:noreply, assign(socket, :bidding, bidding)}
|
||||
end
|
||||
|
||||
def handle_info(
|
||||
{:deleted, %BeetRoundServer.Biddings.Bidding{id: id}},
|
||||
%{assigns: %{bidding: %{id: id}}} = socket
|
||||
) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "The current bidding was deleted.")
|
||||
|> push_navigate(to: ~p"/biddings")}
|
||||
end
|
||||
|
||||
def handle_info({type, %BeetRoundServer.Biddings.Bidding{}}, socket)
|
||||
when type in [:created, :updated, :deleted] do
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user