New list: orders
This commit is contained in:
60
lib/osuuspuutarha_web/live/live_helpers.ex
Normal file
60
lib/osuuspuutarha_web/live/live_helpers.ex
Normal file
@@ -0,0 +1,60 @@
|
||||
defmodule OsuuspuutarhaWeb.LiveHelpers do
|
||||
import Phoenix.LiveView
|
||||
import Phoenix.LiveView.Helpers
|
||||
|
||||
alias Phoenix.LiveView.JS
|
||||
|
||||
@doc """
|
||||
Renders a live component inside a modal.
|
||||
|
||||
The rendered modal receives a `:return_to` option to properly update
|
||||
the URL when the modal is closed.
|
||||
|
||||
## Examples
|
||||
|
||||
<.modal return_to={Routes.order_index_path(@socket, :index)}>
|
||||
<.live_component
|
||||
module={OsuuspuutarhaWeb.OrderLive.FormComponent}
|
||||
id={@order.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
return_to={Routes.order_index_path(@socket, :index)}
|
||||
order: @order
|
||||
/>
|
||||
</.modal>
|
||||
"""
|
||||
def modal(assigns) do
|
||||
assigns = assign_new(assigns, :return_to, fn -> nil end)
|
||||
|
||||
~H"""
|
||||
<div id="modal" class="phx-modal fade-in" phx-remove={hide_modal()}>
|
||||
<div
|
||||
id="modal-content"
|
||||
class="phx-modal-content fade-in-scale"
|
||||
phx-click-away={JS.dispatch("click", to: "#close")}
|
||||
phx-window-keydown={JS.dispatch("click", to: "#close")}
|
||||
phx-key="escape"
|
||||
>
|
||||
<%= if @return_to do %>
|
||||
<%= live_patch "✖",
|
||||
to: @return_to,
|
||||
id: "close",
|
||||
class: "phx-modal-close",
|
||||
phx_click: hide_modal()
|
||||
%>
|
||||
<% else %>
|
||||
<a id="close" href="#" class="phx-modal-close" phx-click={hide_modal()}>✖</a>
|
||||
<% end %>
|
||||
|
||||
<%= render_slot(@inner_block) %>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp hide_modal(js \\ %JS{}) do
|
||||
js
|
||||
|> JS.hide(to: "#modal", transition: "fade-out")
|
||||
|> JS.hide(to: "#modal-content", transition: "fade-out-scale")
|
||||
end
|
||||
end
|
||||
55
lib/osuuspuutarha_web/live/order_live/form_component.ex
Normal file
55
lib/osuuspuutarha_web/live/order_live/form_component.ex
Normal file
@@ -0,0 +1,55 @@
|
||||
defmodule OsuuspuutarhaWeb.OrderLive.FormComponent do
|
||||
use OsuuspuutarhaWeb, :live_component
|
||||
|
||||
alias Osuuspuutarha.Orders
|
||||
|
||||
@impl true
|
||||
def update(%{order: order} = assigns, socket) do
|
||||
changeset = Orders.change_order(order)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"order" => order_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.order
|
||||
|> Orders.change_order(order_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"order" => order_params}, socket) do
|
||||
save_order(socket, socket.assigns.action, order_params)
|
||||
end
|
||||
|
||||
defp save_order(socket, :edit, order_params) do
|
||||
case Orders.update_order(socket.assigns.order, order_params) do
|
||||
{:ok, _order} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Muutokset tallennettu")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_order(socket, :new, order_params) do
|
||||
case Orders.create_order(order_params) do
|
||||
{:ok, _order} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Tilauksesi on tallennettu")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,60 @@
|
||||
<div>
|
||||
<h2><%= @title %></h2>
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="order-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save">
|
||||
|
||||
<%= label f, :order_type, "Valitse tilausmuoto" %>
|
||||
<%= select f, :order_type, Ecto.Enum.values(Osuuspuutarha.Orders.Order, :order_type), prompt: "Ei valintaa" %>
|
||||
<%= error_tag f, :order_type %>
|
||||
|
||||
<%= label f, :location, "Valitse jakopaikka" %>
|
||||
<%= select f, :location, Ecto.Enum.values(Osuuspuutarha.Orders.Order, :location), prompt: "Ei valintaa" %>
|
||||
<%= error_tag f, :location %>
|
||||
|
||||
<%= label f, :fname, "Etunimi" %>
|
||||
<%= text_input f, :fname %>
|
||||
<%= error_tag f, :fname %>
|
||||
|
||||
<%= label f, :lname, "Sukunimi" %>
|
||||
<%= text_input f, :lname %>
|
||||
<%= error_tag f, :lname %>
|
||||
|
||||
<%= label f, :address, "Katuosoite" %>
|
||||
<%= text_input f, :address %>
|
||||
<%= error_tag f, :address %>
|
||||
|
||||
<%= label f, :pcode, "Postinumero" %>
|
||||
<%= text_input f, :pcode %>
|
||||
<%= error_tag f, :pcode %>
|
||||
|
||||
<%= label f, :city, "Postitoimipaikka" %>
|
||||
<%= text_input f, :city %>
|
||||
<%= error_tag f, :city %>
|
||||
|
||||
<%= label f, :phone, "Puhelinnumero" %>
|
||||
<%= text_input f, :phone %>
|
||||
<%= error_tag f, :phone %>
|
||||
|
||||
<%= label f, :email, "Sähköpostiosoite" %>
|
||||
<%= text_input f, :email %>
|
||||
<%= error_tag f, :email %>
|
||||
|
||||
<%= label f, :is_member, "Olen jo Livonsaaren Osuuspuutarhan jäsen" %>
|
||||
<%= checkbox f, :is_member %>
|
||||
<%= error_tag f, :is_member %>
|
||||
|
||||
<%= label f, :split_invoice, "Haluan maksaa laskun kahdessa erässä" %>
|
||||
<%= checkbox f, :split_invoice %>
|
||||
<%= error_tag f, :split_invoice %>
|
||||
|
||||
<div>
|
||||
<%= submit "Tallenna", phx_disable_with: "Tallennetaan..." %>
|
||||
</div>
|
||||
</.form>
|
||||
</div>
|
||||
46
lib/osuuspuutarha_web/live/order_live/index.ex
Normal file
46
lib/osuuspuutarha_web/live/order_live/index.ex
Normal file
@@ -0,0 +1,46 @@
|
||||
defmodule OsuuspuutarhaWeb.OrderLive.Index do
|
||||
use OsuuspuutarhaWeb, :live_view
|
||||
|
||||
alias Osuuspuutarha.Orders
|
||||
alias Osuuspuutarha.Orders.Order
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :orders, list_orders())}
|
||||
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, "Muokkaa tilausta")
|
||||
|> assign(:order, Orders.get_order!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Satolaatikko tilaus")
|
||||
|> assign(:order, %Order{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Tilaukset")
|
||||
|> assign(:order, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
order = Orders.get_order!(id)
|
||||
{:ok, _} = Orders.delete_order(order)
|
||||
|
||||
{:noreply, assign(socket, :orders, list_orders())}
|
||||
end
|
||||
|
||||
defp list_orders do
|
||||
Orders.list_orders()
|
||||
end
|
||||
end
|
||||
61
lib/osuuspuutarha_web/live/order_live/index.html.heex
Normal file
61
lib/osuuspuutarha_web/live/order_live/index.html.heex
Normal file
@@ -0,0 +1,61 @@
|
||||
<h1>Listing Orders</h1>
|
||||
|
||||
<%= if @live_action in [:new, :edit] do %>
|
||||
<.modal return_to={Routes.order_index_path(@socket, :index)}>
|
||||
<.live_component
|
||||
module={OsuuspuutarhaWeb.OrderLive.FormComponent}
|
||||
id={@order.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
order={@order}
|
||||
return_to={Routes.order_index_path(@socket, :index)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Tilausmuoto</th>
|
||||
<th>Jakopaikka</th>
|
||||
<th>Etunimi</th>
|
||||
<th>Sukunimi</th>
|
||||
<th>Osoite</th>
|
||||
<th>Postinum.</th>
|
||||
<th>T. paikka</th>
|
||||
<th>Puh.</th>
|
||||
<th>Säpö</th>
|
||||
<th>Jäsen?</th>
|
||||
<th>Kaksi laskua?</th>
|
||||
<th>Parilliset viikot</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="orders">
|
||||
<%= for order <- @orders do %>
|
||||
<tr id={"order-#{order.id}"}>
|
||||
<td><%= order.order_type %></td>
|
||||
<td><%= order.location %></td>
|
||||
<td><%= order.fname %></td>
|
||||
<td><%= order.lname %></td>
|
||||
<td><%= order.address %></td>
|
||||
<td><%= order.pcode %></td>
|
||||
<td><%= order.city %></td>
|
||||
<td><%= order.phone %></td>
|
||||
<td><%= order.email %></td>
|
||||
<td><%= order.is_member %></td>
|
||||
<td><%= order.split_invoice %></td>
|
||||
<td><%= order.even_weeks %></td>
|
||||
|
||||
<td>
|
||||
<span><%= live_redirect "Näytä", to: Routes.order_show_path(@socket, :show, order) %></span>
|
||||
<span><%= live_patch "Muokkaa", to: Routes.order_index_path(@socket, :edit, order) %></span>
|
||||
<span><%= link "Poista", to: "#", phx_click: "delete", phx_value_id: order.id, data: [confirm: "Oletko varma?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= live_patch "Lisää tilaus", to: Routes.order_index_path(@socket, :new) %></span>
|
||||
21
lib/osuuspuutarha_web/live/order_live/show.ex
Normal file
21
lib/osuuspuutarha_web/live/order_live/show.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule OsuuspuutarhaWeb.OrderLive.Show do
|
||||
use OsuuspuutarhaWeb, :live_view
|
||||
|
||||
alias Osuuspuutarha.Orders
|
||||
|
||||
@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(:order, Orders.get_order!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Tilaukset tiedot"
|
||||
defp page_title(:edit), do: "Muokkaa tilausta"
|
||||
end
|
||||
81
lib/osuuspuutarha_web/live/order_live/show.html.heex
Normal file
81
lib/osuuspuutarha_web/live/order_live/show.html.heex
Normal file
@@ -0,0 +1,81 @@
|
||||
<h1>Tilauksen tiedot</h1>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.modal return_to={Routes.order_show_path(@socket, :show, @order)}>
|
||||
<.live_component
|
||||
module={OsuuspuutarhaWeb.OrderLive.FormComponent}
|
||||
id={@order.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
order={@order}
|
||||
return_to={Routes.order_show_path(@socket, :show, @order)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Tilausmuoto:</strong>
|
||||
<%= @order.order_type %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Jakopaikka:</strong>
|
||||
<%= @order.location %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Etunimi:</strong>
|
||||
<%= @order.fname %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Sukunimi:</strong>
|
||||
<%= @order.lname %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Osoite:</strong>
|
||||
<%= @order.address %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Postinumero:</strong>
|
||||
<%= @order.pcode %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Postitoimipaikka:</strong>
|
||||
<%= @order.city %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Puhelinnumero:</strong>
|
||||
<%= @order.phone %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Sähköpostiosoite:</strong>
|
||||
<%= @order.email %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Jäsen?</strong>
|
||||
<%= @order.is_member %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Kahdella laskulla:</strong>
|
||||
<%= @order.split_invoice %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Parilliset viikot:</strong>
|
||||
<%= @order.even_weeks %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= live_patch "Muokkaa", to: Routes.order_show_path(@socket, :edit, @order), class: "button" %></span> |
|
||||
<span><%= live_redirect "Takaisin", to: Routes.order_index_path(@socket, :index) %></span>
|
||||
@@ -18,6 +18,13 @@ defmodule OsuuspuutarhaWeb.Router do
|
||||
pipe_through :browser
|
||||
|
||||
get "/", PageController, :index
|
||||
|
||||
live "/orders", OrderLive.Index, :index
|
||||
live "/orders/new", OrderLive.Index, :new
|
||||
live "/orders/:id/edit", OrderLive.Index, :edit
|
||||
|
||||
live "/orders/:id", OrderLive.Show, :show
|
||||
live "/orders/:id/show/edit", OrderLive.Show, :edit
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
||||
Reference in New Issue
Block a user