Autogen Yield liveviews
This commit is contained in:
55
lib/osuuspuutarha_web/live/yield_live/form_component.ex
Normal file
55
lib/osuuspuutarha_web/live/yield_live/form_component.ex
Normal file
@@ -0,0 +1,55 @@
|
||||
defmodule OsuuspuutarhaWeb.YieldLive.FormComponent do
|
||||
use OsuuspuutarhaWeb, :live_component
|
||||
|
||||
alias Osuuspuutarha.Harvest
|
||||
|
||||
@impl true
|
||||
def update(%{yield: yield} = assigns, socket) do
|
||||
changeset = Harvest.change_yield(yield)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"yield" => yield_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.yield
|
||||
|> Harvest.change_yield(yield_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"yield" => yield_params}, socket) do
|
||||
save_yield(socket, socket.assigns.action, yield_params)
|
||||
end
|
||||
|
||||
defp save_yield(socket, :edit, yield_params) do
|
||||
case Harvest.update_yield(socket.assigns.yield, yield_params) do
|
||||
{:ok, _yield} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Yield updated successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_yield(socket, :new, yield_params) do
|
||||
case Harvest.create_yield(yield_params) do
|
||||
{:ok, _yield} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Yield created successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
<div>
|
||||
<h2><%= @title %></h2>
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="yield-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save">
|
||||
|
||||
<%= label f, :date %>
|
||||
<%= date_input f, :date %>
|
||||
<%= error_tag f, :date %>
|
||||
|
||||
<%= label f, :plant %>
|
||||
<%= select f, :plant, Ecto.Enum.values(Osuuspuutarha.Harvest.Yield, :plant), prompt: "Choose a value" %>
|
||||
<%= error_tag f, :plant %>
|
||||
|
||||
<%= label f, :amount %>
|
||||
<%= number_input f, :amount, step: "any" %>
|
||||
<%= error_tag f, :amount %>
|
||||
|
||||
<%= label f, :unit %>
|
||||
<%= select f, :unit, Ecto.Enum.values(Osuuspuutarha.Harvest.Yield, :unit), prompt: "Choose a value" %>
|
||||
<%= error_tag f, :unit %>
|
||||
|
||||
<div>
|
||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
||||
</div>
|
||||
</.form>
|
||||
</div>
|
||||
46
lib/osuuspuutarha_web/live/yield_live/index.ex
Normal file
46
lib/osuuspuutarha_web/live/yield_live/index.ex
Normal file
@@ -0,0 +1,46 @@
|
||||
defmodule OsuuspuutarhaWeb.YieldLive.Index do
|
||||
use OsuuspuutarhaWeb, :live_view
|
||||
|
||||
alias Osuuspuutarha.Harvest
|
||||
alias Osuuspuutarha.Harvest.Yield
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :yields, list_yields())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Yield")
|
||||
|> assign(:yield, Harvest.get_yield!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Yield")
|
||||
|> assign(:yield, %Yield{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Yields")
|
||||
|> assign(:yield, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
yield = Harvest.get_yield!(id)
|
||||
{:ok, _} = Harvest.delete_yield(yield)
|
||||
|
||||
{:noreply, assign(socket, :yields, list_yields())}
|
||||
end
|
||||
|
||||
defp list_yields do
|
||||
Harvest.list_yields()
|
||||
end
|
||||
end
|
||||
45
lib/osuuspuutarha_web/live/yield_live/index.html.heex
Normal file
45
lib/osuuspuutarha_web/live/yield_live/index.html.heex
Normal file
@@ -0,0 +1,45 @@
|
||||
<h1>Listing Yields</h1>
|
||||
|
||||
<%= if @live_action in [:new, :edit] do %>
|
||||
<.modal return_to={Routes.yield_index_path(@socket, :index)}>
|
||||
<.live_component
|
||||
module={OsuuspuutarhaWeb.YieldLive.FormComponent}
|
||||
id={@yield.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
yield={@yield}
|
||||
return_to={Routes.yield_index_path(@socket, :index)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Plant</th>
|
||||
<th>Amount</th>
|
||||
<th>Unit</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="yields">
|
||||
<%= for yield <- @yields do %>
|
||||
<tr id={"yield-#{yield.id}"}>
|
||||
<td><%= yield.date %></td>
|
||||
<td><%= yield.plant %></td>
|
||||
<td><%= yield.amount %></td>
|
||||
<td><%= yield.unit %></td>
|
||||
|
||||
<td>
|
||||
<span><%= live_redirect "Show", to: Routes.yield_show_path(@socket, :show, yield) %></span>
|
||||
<span><%= live_patch "Edit", to: Routes.yield_index_path(@socket, :edit, yield) %></span>
|
||||
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: yield.id, data: [confirm: "Are you sure?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= live_patch "New Yield", to: Routes.yield_index_path(@socket, :new) %></span>
|
||||
21
lib/osuuspuutarha_web/live/yield_live/show.ex
Normal file
21
lib/osuuspuutarha_web/live/yield_live/show.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule OsuuspuutarhaWeb.YieldLive.Show do
|
||||
use OsuuspuutarhaWeb, :live_view
|
||||
|
||||
alias Osuuspuutarha.Harvest
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:yield, Harvest.get_yield!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Yield"
|
||||
defp page_title(:edit), do: "Edit Yield"
|
||||
end
|
||||
41
lib/osuuspuutarha_web/live/yield_live/show.html.heex
Normal file
41
lib/osuuspuutarha_web/live/yield_live/show.html.heex
Normal file
@@ -0,0 +1,41 @@
|
||||
<h1>Show Yield</h1>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.modal return_to={Routes.yield_show_path(@socket, :show, @yield)}>
|
||||
<.live_component
|
||||
module={OsuuspuutarhaWeb.YieldLive.FormComponent}
|
||||
id={@yield.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
yield={@yield}
|
||||
return_to={Routes.yield_show_path(@socket, :show, @yield)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Date:</strong>
|
||||
<%= @yield.date %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Plant:</strong>
|
||||
<%= @yield.plant %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Amount:</strong>
|
||||
<%= @yield.amount %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Unit:</strong>
|
||||
<%= @yield.unit %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= live_patch "Edit", to: Routes.yield_show_path(@socket, :edit, @yield), class: "button" %></span> |
|
||||
<span><%= live_redirect "Back", to: Routes.yield_index_path(@socket, :index) %></span>
|
||||
@@ -45,6 +45,13 @@ defmodule OsuuspuutarhaWeb.Router do
|
||||
|
||||
live "/tilaukset/:id", OrderLive.Show, :show
|
||||
live "/tilaukset/:id/nayta/muokkaa", OrderLive.Show, :edit
|
||||
|
||||
live "/korjuut", YieldLive.Index, :index
|
||||
live "/korjuut/uusi", YieldLive.Index, :new
|
||||
live "/korjuut/:id/muokkaa", YieldLive.Index, :edit
|
||||
|
||||
live "/korjuut/:id", YieldLive.Show, :show
|
||||
live "/korjuut/:id/nayta/muokkaa", YieldLive.Show, :edit
|
||||
end
|
||||
|
||||
scope "/lataukset", as: :exports, alias: OsuuspuutarhaWeb.Exports do
|
||||
|
||||
Reference in New Issue
Block a user