initial shift template live view progress

This commit is contained in:
Adam Piontek 2021-03-06 20:16:56 -05:00
commit 57c8b5b322
9 changed files with 98 additions and 39 deletions
lib/shift73k_web/live/shift_template_live

View file

@ -3,15 +3,36 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
alias Shift73k.ShiftTemplates
alias Shift73k.ShiftTemplates.ShiftTemplate
alias Shift73kWeb.Roles
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :shift_templates, list_shift_templates())}
def mount(_params, session, socket) do
# {:ok, assign(socket, :shift_templates, list_shift_templates())}
socket
|> assign_defaults(session)
|> live_okreply()
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
current_user = socket.assigns.current_user
live_action = socket.assigns.live_action
shift_template = shift_template_from_params(params)
if Roles.can?(current_user, shift_template, live_action) do
socket
|> assign_shift_templates()
|> assign(:modal_return_to, Routes.shift_template_index_path(socket, :index))
|> assign(:delete_shift_template, nil)
|> apply_action(socket.assigns.live_action, params)
|> live_noreply()
else
socket
|> put_flash(:error, "Unauthorised")
|> redirect(to: "/")
|> live_noreply()
end
end
defp apply_action(socket, :edit, %{"id" => id}) do
@ -32,15 +53,36 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
|> assign(:shift_template, nil)
end
defp assign_shift_templates(socket) do
%User{id: uid} = socket.assigns.current_user
user_shifts = ShiftTemplates.list_shift_templates_by_user_id(uid)
assign(socket, :shift_templates, user_shifts)
end
defp shift_template_from_params(params)
defp shift_template_from_params(%{"id" => id}),
do: ShiftTemplates.get_shift_template!(id)
defp shift_template_from_params(_params), do: %ShiftTemplate{}
@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())}
{:noreply, assign_shift_templates(socket)}
end
defp list_shift_templates do
ShiftTemplates.list_shift_templates()
@impl true
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
end
@impl true
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
socket |> put_flash(flash_type, msg) |> live_noreply()
end
end

View file

@ -1,15 +1,24 @@
<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) %>
current_user: @current_user %>
<% end %>
<table>
<div class="d-flex justify-content-between d-flex align-items-center">
<h2>
<%= icon_div @socket, "bi-clock-history", [class: "icon baseline"] %>
My Shift Templates
</h2>
<%= live_patch "New Shift Template", to: Routes.shift_template_index_path(@socket, :new), class: "btn btn-primary" %>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Label</th>
@ -18,8 +27,7 @@
<th>Location</th>
<th>Timezone</th>
<th>Start time</th>
<th>Length hours</th>
<th>Length minutes</th>
<th>Length</th>
<th></th>
</tr>
@ -32,9 +40,8 @@
<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><%= shift_template.start_time |> Calendar.strftime("%I:%M %p") %></td>
<td><%= shift_template.length_hours %>h <%= shift_template.length_minutes || 0 %>m</td>
<td>
<span><%= live_redirect "Show", to: Routes.shift_template_show_path(@socket, :show, shift_template) %></span>
@ -45,5 +52,4 @@
<% end %>
</tbody>
</table>
<span><%= live_patch "New Shift template", to: Routes.shift_template_index_path(@socket, :new) %></span>
</div>