@@ -1,55 +0,0 @@
|
||||
defmodule OsuuspuutarhaWeb.CalendarLive.FormComponent do
|
||||
use OsuuspuutarhaWeb, :live_component
|
||||
|
||||
alias Osuuspuutarha.Season
|
||||
|
||||
@impl true
|
||||
def update(%{calendar: calendar} = assigns, socket) do
|
||||
changeset = Season.change_calendar(calendar)
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"calendar" => calendar_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.calendar
|
||||
|> Season.change_calendar(calendar_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"calendar" => calendar_params}, socket) do
|
||||
save_calendar(socket, socket.assigns.action, calendar_params)
|
||||
end
|
||||
|
||||
defp save_calendar(socket, :edit, calendar_params) do
|
||||
case Season.update_calendar(socket.assigns.calendar, calendar_params) do
|
||||
{:ok, _calendar} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Calendar updated successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_calendar(socket, :new, calendar_params) do
|
||||
case Season.create_calendar(calendar_params) do
|
||||
{:ok, _calendar} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Calendar created successfully")
|
||||
|> push_redirect(to: socket.assigns.return_to)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,24 +0,0 @@
|
||||
<div>
|
||||
<h2><%= @title %></h2>
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="calendar-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save">
|
||||
|
||||
<%= label f, :pickup_date %>
|
||||
<%= date_select f, :pickup_date %>
|
||||
<%= error_tag f, :pickup_date %>
|
||||
|
||||
<%= label f, :is_picked_up %>
|
||||
<%= checkbox f, :is_picked_up %>
|
||||
<%= error_tag f, :is_picked_up %>
|
||||
|
||||
<div>
|
||||
<%= submit "Save", phx_disable_with: "Saving..." %>
|
||||
</div>
|
||||
</.form>
|
||||
</div>
|
||||
@@ -1,46 +0,0 @@
|
||||
defmodule OsuuspuutarhaWeb.CalendarLive.Index do
|
||||
use OsuuspuutarhaWeb, :live_view
|
||||
|
||||
alias Osuuspuutarha.Season
|
||||
alias Osuuspuutarha.Season.Calendar
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, assign(socket, :calendars, list_calendars())}
|
||||
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 Calendar")
|
||||
|> assign(:calendar, Season.get_calendar!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Calendar")
|
||||
|> assign(:calendar, %Calendar{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Calendars")
|
||||
|> assign(:calendar, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
calendar = Season.get_calendar!(id)
|
||||
{:ok, _} = Season.delete_calendar(calendar)
|
||||
|
||||
{:noreply, assign(socket, :calendars, list_calendars())}
|
||||
end
|
||||
|
||||
defp list_calendars do
|
||||
Season.list_calendars()
|
||||
end
|
||||
end
|
||||
@@ -1,41 +0,0 @@
|
||||
<h1>Listing Calendars</h1>
|
||||
|
||||
<%= if @live_action in [:new, :edit] do %>
|
||||
<.modal return_to={Routes.calendar_index_path(@socket, :index)}>
|
||||
<.live_component
|
||||
module={OsuuspuutarhaWeb.CalendarLive.FormComponent}
|
||||
id={@calendar.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
calendar={@calendar}
|
||||
return_to={Routes.calendar_index_path(@socket, :index)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Pickup date</th>
|
||||
<th>Is picked up</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="calendars">
|
||||
<%= for calendar <- @calendars do %>
|
||||
<tr id={"calendar-#{calendar.id}"}>
|
||||
<td><%= calendar.pickup_date %></td>
|
||||
<td><%= calendar.is_picked_up %></td>
|
||||
|
||||
<td>
|
||||
<span><%= live_redirect "Show", to: Routes.calendar_show_path(@socket, :show, calendar) %></span>
|
||||
<span><%= live_patch "Edit", to: Routes.calendar_index_path(@socket, :edit, calendar) %></span>
|
||||
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: calendar.id, data: [confirm: "Are you sure?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= live_patch "New Calendar", to: Routes.calendar_index_path(@socket, :new) %></span>
|
||||
@@ -1,21 +0,0 @@
|
||||
defmodule OsuuspuutarhaWeb.CalendarLive.Show do
|
||||
use OsuuspuutarhaWeb, :live_view
|
||||
|
||||
alias Osuuspuutarha.Season
|
||||
|
||||
@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(:calendar, Season.get_calendar!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Calendar"
|
||||
defp page_title(:edit), do: "Edit Calendar"
|
||||
end
|
||||
@@ -1,31 +0,0 @@
|
||||
<h1>Show Calendar</h1>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.modal return_to={Routes.calendar_show_path(@socket, :show, @calendar)}>
|
||||
<.live_component
|
||||
module={OsuuspuutarhaWeb.CalendarLive.FormComponent}
|
||||
id={@calendar.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
calendar={@calendar}
|
||||
return_to={Routes.calendar_show_path(@socket, :show, @calendar)}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Pickup date:</strong>
|
||||
<%= @calendar.pickup_date %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Is picked up:</strong>
|
||||
<%= @calendar.is_picked_up %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= live_patch "Edit", to: Routes.calendar_show_path(@socket, :edit, @calendar), class: "button" %></span> |
|
||||
<span><%= live_redirect "Back", to: Routes.calendar_index_path(@socket, :index) %></span>
|
||||
@@ -32,13 +32,6 @@ defmodule OsuuspuutarhaWeb.Router do
|
||||
|
||||
live "/ilmoittautuminen", OrderLive.Registration, :index
|
||||
live "/ilmoittautuminen/uusi", OrderLive.Registration, :new
|
||||
|
||||
live "/calendars", CalendarLive.Index, :index
|
||||
live "/calendars/new", CalendarLive.Index, :new
|
||||
live "/calendars/:id/edit", CalendarLive.Index, :edit
|
||||
|
||||
live "/calendars/:id", CalendarLive.Show, :show
|
||||
live "/calendars/:id/show/edit", CalendarLive.Show, :edit
|
||||
end
|
||||
|
||||
scope "/hallinta", OsuuspuutarhaWeb do
|
||||
|
||||
Reference in New Issue
Block a user