initial liveview/context/schema generation for Shifts

This commit is contained in:
Adam Piontek 2021-03-11 17:16:11 -05:00
parent c864ff9563
commit 290475c101
15 changed files with 648 additions and 6 deletions

View file

@ -0,0 +1,55 @@
defmodule Shift73kWeb.ShiftLive.FormComponent do
use Shift73kWeb, :live_component
alias Shift73k.Shifts
@impl true
def update(%{shift: shift} = assigns, socket) do
changeset = Shifts.change_shift(shift)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"shift" => shift_params}, socket) do
changeset =
socket.assigns.shift
|> Shifts.change_shift(shift_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"shift" => shift_params}, socket) do
save_shift(socket, socket.assigns.action, shift_params)
end
defp save_shift(socket, :edit, shift_params) do
case Shifts.update_shift(socket.assigns.shift, shift_params) do
{:ok, _shift} ->
{:noreply,
socket
|> put_flash(:info, "Shift updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_shift(socket, :new, shift_params) do
case Shifts.create_shift(shift_params) do
{:ok, _shift} ->
{:noreply,
socket
|> put_flash(:info, "Shift created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View file

@ -0,0 +1,42 @@
<h2><%= @title %></h2>
<%= f = form_for @changeset, "#",
id: "shift-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>
<%= label f, :subject %>
<%= text_input f, :subject %>
<%= error_tag f, :subject %>
<%= label f, :location %>
<%= text_input f, :location %>
<%= error_tag f, :location %>
<%= label f, :description %>
<%= textarea f, :description %>
<%= error_tag f, :description %>
<%= label f, :time_zone %>
<%= text_input f, :time_zone %>
<%= error_tag f, :time_zone %>
<%= label f, :date %>
<%= date_select f, :date %>
<%= error_tag f, :date %>
<%= label f, :time_zone %>
<%= text_input f, :time_zone %>
<%= error_tag f, :time_zone %>
<%= label f, :time_start %>
<%= time_select f, :time_start %>
<%= error_tag f, :time_start %>
<%= label f, :time_end %>
<%= time_select f, :time_end %>
<%= error_tag f, :time_end %>
<%= submit "Save", phx_disable_with: "Saving..." %>
</form>

View file

@ -0,0 +1,46 @@
defmodule Shift73kWeb.ShiftLive.Index do
use Shift73kWeb, :live_view
alias Shift73k.Shifts
alias Shift73k.Shifts.Shift
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :shifts, list_shifts())}
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 Shift")
|> assign(:shift, Shifts.get_shift!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Shift")
|> assign(:shift, %Shift{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Shifts")
|> assign(:shift, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
shift = Shifts.get_shift!(id)
{:ok, _} = Shifts.delete_shift(shift)
{:noreply, assign(socket, :shifts, list_shifts())}
end
defp list_shifts do
Shifts.list_shifts()
end
end

View file

@ -0,0 +1,49 @@
<h1>Listing Shifts</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal @socket, Shift73kWeb.ShiftLive.FormComponent,
id: @shift.id || :new,
title: @page_title,
action: @live_action,
shift: @shift,
return_to: Routes.shift_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Subject</th>
<th>Location</th>
<th>Description</th>
<th>Time zone</th>
<th>Date</th>
<th>Time zone</th>
<th>Time start</th>
<th>Time end</th>
<th></th>
</tr>
</thead>
<tbody id="shifts">
<%= for shift <- @shifts do %>
<tr id="shift-<%= shift.id %>">
<td><%= shift.subject %></td>
<td><%= shift.location %></td>
<td><%= shift.description %></td>
<td><%= shift.time_zone %></td>
<td><%= shift.date %></td>
<td><%= shift.time_zone %></td>
<td><%= shift.time_start %></td>
<td><%= shift.time_end %></td>
<td>
<span><%= live_redirect "Show", to: Routes.shift_show_path(@socket, :show, shift) %></span>
<span><%= live_patch "Edit", to: Routes.shift_index_path(@socket, :edit, shift) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: shift.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Shift", to: Routes.shift_index_path(@socket, :new) %></span>

View file

@ -0,0 +1,21 @@
defmodule Shift73kWeb.ShiftLive.Show do
use Shift73kWeb, :live_view
alias Shift73k.Shifts
@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(:shift, Shifts.get_shift!(id))}
end
defp page_title(:show), do: "Show Shift"
defp page_title(:edit), do: "Edit Shift"
end

View file

@ -0,0 +1,57 @@
<h1>Show Shift</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal @socket, Shift73kWeb.ShiftLive.FormComponent,
id: @shift.id,
title: @page_title,
action: @live_action,
shift: @shift,
return_to: Routes.shift_show_path(@socket, :show, @shift) %>
<% end %>
<ul>
<li>
<strong>Subject:</strong>
<%= @shift.subject %>
</li>
<li>
<strong>Location:</strong>
<%= @shift.location %>
</li>
<li>
<strong>Description:</strong>
<%= @shift.description %>
</li>
<li>
<strong>Time zone:</strong>
<%= @shift.time_zone %>
</li>
<li>
<strong>Date:</strong>
<%= @shift.date %>
</li>
<li>
<strong>Time zone:</strong>
<%= @shift.time_zone %>
</li>
<li>
<strong>Time start:</strong>
<%= @shift.time_start %>
</li>
<li>
<strong>Time end:</strong>
<%= @shift.time_end %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.shift_show_path(@socket, :edit, @shift), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.shift_index_path(@socket, :index) %></span>

View file

@ -91,10 +91,17 @@ defmodule Shift73kWeb.Router do
scope "/", Shift73kWeb do
pipe_through([:browser, :require_authenticated_user, :user])
live "/my_shifts", ShiftTemplateLive.Index, :index
live "/my_shifts/new", ShiftTemplateLive.Index, :new
live "/my_shifts/:id/edit", ShiftTemplateLive.Index, :edit
live "/my_shifts/:id/clone", ShiftTemplateLive.Index, :clone
live "/templates", ShiftTemplateLive.Index, :index
live "/templates/new", ShiftTemplateLive.Index, :new
live "/templates/:id/edit", ShiftTemplateLive.Index, :edit
live "/templates/:id/clone", ShiftTemplateLive.Index, :clone
live "/shifts", ShiftLive.Index, :index
live "/shifts/new", ShiftLive.Index, :new
live "/shifts/:id/edit", ShiftLive.Index, :edit
live "/shifts/:id", ShiftLive.Show, :show
live "/shifts/:id/show/edit", ShiftLive.Show, :edit
end
# scope "/", Shift73kWeb do