initial shift template migration & schema working with user field/assoc for default shift template

This commit is contained in:
Adam Piontek 2021-03-06 13:48:13 -05:00
parent 61db634c36
commit a151bfcee7
16 changed files with 665 additions and 4 deletions

View file

@ -0,0 +1,55 @@
defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
use Shift73kWeb, :live_component
alias Shift73k.ShiftTemplates
@impl true
def update(%{shift_template: shift_template} = assigns, socket) do
changeset = ShiftTemplates.change_shift_template(shift_template)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"shift_template" => shift_template_params}, socket) do
changeset =
socket.assigns.shift_template
|> ShiftTemplates.change_shift_template(shift_template_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"shift_template" => shift_template_params}, socket) do
save_shift_template(socket, socket.assigns.action, shift_template_params)
end
defp save_shift_template(socket, :edit, shift_template_params) do
case ShiftTemplates.update_shift_template(socket.assigns.shift_template, shift_template_params) do
{:ok, _shift_template} ->
{:noreply,
socket
|> put_flash(:info, "Shift template updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_shift_template(socket, :new, shift_template_params) do
case ShiftTemplates.create_shift_template(shift_template_params) do
{:ok, _shift_template} ->
{:noreply,
socket
|> put_flash(:info, "Shift template 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_template-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save" %>
<%= label f, :label %>
<%= text_input f, :label %>
<%= error_tag f, :label %>
<%= label f, :subject %>
<%= text_input f, :subject %>
<%= error_tag f, :subject %>
<%= label f, :description %>
<%= text_input f, :description %>
<%= error_tag f, :description %>
<%= label f, :location %>
<%= text_input f, :location %>
<%= error_tag f, :location %>
<%= label f, :timezone %>
<%= text_input f, :timezone %>
<%= error_tag f, :timezone %>
<%= label f, :start_time %>
<%= time_select f, :start_time %>
<%= error_tag f, :start_time %>
<%= label f, :length_hours %>
<%= number_input f, :length_hours %>
<%= error_tag f, :length_hours %>
<%= label f, :length_minutes %>
<%= number_input f, :length_minutes %>
<%= error_tag f, :length_minutes %>
<%= submit "Save", phx_disable_with: "Saving..." %>
</form>

View file

@ -0,0 +1,46 @@
defmodule Shift73kWeb.ShiftTemplateLive.Index do
use Shift73kWeb, :live_view
alias Shift73k.ShiftTemplates
alias Shift73k.ShiftTemplates.ShiftTemplate
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :shift_templates, list_shift_templates())}
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 template")
|> assign(:shift_template, ShiftTemplates.get_shift_template!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Shift template")
|> assign(:shift_template, %ShiftTemplate{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Shift templates")
|> assign(:shift_template, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
shift_template = ShiftTemplates.get_shift_template!(id)
{:ok, _} = ShiftTemplates.delete_shift_template(shift_template)
{:noreply, assign(socket, :shift_templates, list_shift_templates())}
end
defp list_shift_templates do
ShiftTemplates.list_shift_templates()
end
end

View file

@ -0,0 +1,49 @@
<h1>Listing Shift templates</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal @socket, Shift73kWeb.ShiftTemplateLive.FormComponent,
id: @shift_template.id || :new,
title: @page_title,
action: @live_action,
shift_template: @shift_template,
return_to: Routes.shift_template_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Label</th>
<th>Subject</th>
<th>Description</th>
<th>Location</th>
<th>Timezone</th>
<th>Start time</th>
<th>Length hours</th>
<th>Length minutes</th>
<th></th>
</tr>
</thead>
<tbody id="shift_templates">
<%= for shift_template <- @shift_templates do %>
<tr id="shift_template-<%= shift_template.id %>">
<td><%= shift_template.label %></td>
<td><%= shift_template.subject %></td>
<td><%= shift_template.description %></td>
<td><%= shift_template.location %></td>
<td><%= shift_template.timezone %></td>
<td><%= shift_template.start_time %></td>
<td><%= shift_template.length_hours %></td>
<td><%= shift_template.length_minutes %></td>
<td>
<span><%= live_redirect "Show", to: Routes.shift_template_show_path(@socket, :show, shift_template) %></span>
<span><%= live_patch "Edit", to: Routes.shift_template_index_path(@socket, :edit, shift_template) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: shift_template.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Shift template", to: Routes.shift_template_index_path(@socket, :new) %></span>

View file

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

View file

@ -0,0 +1,57 @@
<h1>Show Shift template</h1>
<%= if @live_action in [:edit] do %>
<%= live_modal @socket, Shift73kWeb.ShiftTemplateLive.FormComponent,
id: @shift_template.id,
title: @page_title,
action: @live_action,
shift_template: @shift_template,
return_to: Routes.shift_template_show_path(@socket, :show, @shift_template) %>
<% end %>
<ul>
<li>
<strong>Label:</strong>
<%= @shift_template.label %>
</li>
<li>
<strong>Subject:</strong>
<%= @shift_template.subject %>
</li>
<li>
<strong>Description:</strong>
<%= @shift_template.description %>
</li>
<li>
<strong>Location:</strong>
<%= @shift_template.location %>
</li>
<li>
<strong>Timezone:</strong>
<%= @shift_template.timezone %>
</li>
<li>
<strong>Start time:</strong>
<%= @shift_template.start_time %>
</li>
<li>
<strong>Length hours:</strong>
<%= @shift_template.length_hours %>
</li>
<li>
<strong>Length minutes:</strong>
<%= @shift_template.length_minutes %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.shift_template_show_path(@socket, :edit, @shift_template), class: "button" %></span>
<span><%= live_redirect "Back", to: Routes.shift_template_index_path(@socket, :index) %></span>

View file

@ -88,9 +88,16 @@ defmodule Shift73kWeb.Router do
get("/users/confirm/:token", UserConfirmationController, :confirm)
end
# scope "/", Shift73kWeb do
# pipe_through([:browser, :require_authenticated_user, :user])
# end
scope "/", Shift73kWeb do
pipe_through([:browser, :require_authenticated_user, :user])
live "/shift_templates", ShiftTemplateLive.Index, :index
live "/shift_templates/new", ShiftTemplateLive.Index, :new
live "/shift_templates/:id/edit", ShiftTemplateLive.Index, :edit
live "/shift_templates/:id", ShiftTemplateLive.Show, :show
live "/shift_templates/:id/show/edit", ShiftTemplateLive.Show, :edit
end
# scope "/", Shift73kWeb do
# pipe_through([:browser, :require_authenticated_user, :admin])