implemented shift template delete modal

This commit is contained in:
Adam Piontek 2021-03-09 20:55:59 -05:00
parent 9322e98fef
commit 5c4a4ea793
7 changed files with 102 additions and 61 deletions

View file

@ -22,7 +22,7 @@ defmodule Shift73k.ShiftTemplates do
end end
def list_shift_templates_by_user_id(user_id) do def list_shift_templates_by_user_id(user_id) do
(from s in ShiftTemplate, where: s.user_id == ^user_id) from(s in ShiftTemplate, where: s.user_id == ^user_id)
|> Repo.all() |> Repo.all()
end end
@ -42,6 +42,8 @@ defmodule Shift73k.ShiftTemplates do
""" """
def get_shift_template!(id), do: Repo.get!(ShiftTemplate, id) def get_shift_template!(id), do: Repo.get!(ShiftTemplate, id)
def get_shift_template(id), do: Repo.get(ShiftTemplate, id)
@doc """ @doc """
Creates a shift_template. Creates a shift_template.

View file

@ -0,0 +1,44 @@
defmodule Shift73kWeb.ShiftTemplateLive.DeleteComponent do
use Shift73kWeb, :live_component
alias Shift73k.ShiftTemplates
@impl true
def update(assigns, socket) do
socket
|> assign(assigns)
|> live_okreply()
end
@impl true
def handle_event("cancel", _, socket) do
{:noreply, push_event(socket, "modal-please-hide", %{})}
end
@impl true
def handle_event("confirm", %{"id" => id, "subject" => subject}, socket) do
id
|> ShiftTemplates.get_shift_template()
|> ShiftTemplates.delete_shift_template()
|> case do
{:ok, _} ->
flash = {:info, "Shift template deleted successfully: \"#{subject}\""}
send(self(), {:put_flash_message, flash})
socket
|> push_event("modal-please-hide", %{})
|> live_noreply()
{:error, _} ->
flash =
{:error,
"Some error trying to delete shift template \"#{subject}\". Possibly already deleted? Reloading list..."}
send(self(), {:put_flash_message, flash})
socket
|> push_event("modal-please-hide", %{})
|> live_noreply()
end
end
end

View file

@ -0,0 +1,23 @@
<div class="modal-body">
Are you sure you want to delete "<%= @delete_shift_template.subject %>
(<%= @delete_shift_template.start_time |> Calendar.strftime("%I:%M%P") %>
&mdash;
<%=
@delete_shift_template.start_time
|> Time.add((60 * 60 * @delete_shift_template.length_hours) + ((@delete_shift_template.length_minutes || 0) * 60))
|> Calendar.strftime("%I:%M%P")
%>)"?
</div>
<div class="modal-footer">
<%= link "Cancel", to: "#", class: "btn me-2", phx_click: "cancel", phx_target: @myself %>
<%= link "Confirm Delete", to: "#",
class: "btn btn-danger",
phx_click: "confirm",
phx_target: @myself,
phx_value_id: @delete_shift_template.id,
phx_value_subject: @delete_shift_template.subject %>
</div>

View file

@ -19,7 +19,6 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
live_action = socket.assigns.live_action live_action = socket.assigns.live_action
shift_template = shift_template_from_params(params) shift_template = shift_template_from_params(params)
if Roles.can?(current_user, shift_template, live_action) do if Roles.can?(current_user, shift_template, live_action) do
socket socket
|> assign_shift_templates() |> assign_shift_templates()
@ -66,15 +65,19 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
defp shift_template_from_params(_params), do: %ShiftTemplate{} defp shift_template_from_params(_params), do: %ShiftTemplate{}
@impl true @impl true
def handle_event("delete", %{"id" => id}, socket) do def handle_event("delete-modal", %{"id" => id}, socket) do
shift_template = ShiftTemplates.get_shift_template!(id) {:noreply, assign(socket, :delete_shift_template, ShiftTemplates.get_shift_template!(id))}
{:ok, _} = ShiftTemplates.delete_shift_template(shift_template)
{:noreply, assign_shift_templates(socket)}
end 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_shift_templates(socket)}
# end
@impl true @impl true
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
socket |> copy_flash() |> push_patch(to: to) |> live_noreply() socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
@ -84,5 +87,4 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
socket |> put_flash(flash_type, msg) |> live_noreply() socket |> put_flash(flash_type, msg) |> live_noreply()
end end
end end

View file

@ -7,6 +7,14 @@
current_user: @current_user %> current_user: @current_user %>
<% end %> <% end %>
<%= if @delete_shift_template do %>
<%= live_modal @socket, Shift73kWeb.ShiftTemplateLive.DeleteComponent,
id: @delete_shift_template.id,
title: "Delete Shift Template",
delete_shift_template: @delete_shift_template,
current_user: @current_user %>
<% end %>
<div class="row justify-content-start justify-content-sm-center"> <div class="row justify-content-start justify-content-sm-center">
<div class="col-md-12 col-lg-10 col-xl-9"> <div class="col-md-12 col-lg-10 col-xl-9">
@ -83,22 +91,20 @@
</tbody> </tbody>
</table> </table>
<div class="text-end"> <%= if Roles.can?(@current_user, shift, :edit) do %>
<%= live_patch to: Routes.shift_template_index_path(@socket, :edit, shift), class: "btn btn-primary btn-sm text-nowrap" do %>
<%= if Roles.can?(@current_user, shift, :delete) do %> <%= icon_div @socket, "bi-pencil", [class: "icon baseline", style: "margin-right:0.125rem;"] %>
<%= link to: "#", phx_click: "delete", phx_value_id: shift.id, data: [confirm: "Are you sure?"], class: "btn btn-outline-danger btn-sm text-nowrap" do %> Edit
<%= icon_div @socket, "bi-trash", [class: "icon baseline", style: "margin-right:0.125rem;"] %>
Delete
<% end %>
<% end %> <% end %>
<% end %>
<%= if Roles.can?(@current_user, shift, :delete) do %>
<button class="btn btn-outline-danger btn-sm text-nowrap" phx-click="delete-modal" phx-value-id="<%= shift.id %>">
<%= icon_div @socket, "bi-trash", [class: "icon baseline", style: "margin-right:0.125rem;"] %>
Delete
</button>
<% end %>
<%= if Roles.can?(@current_user, shift, :edit) do %>
<%= live_patch to: Routes.shift_template_index_path(@socket, :edit, shift), class: "btn btn-primary btn-sm text-nowrap" do %>
<%= icon_div @socket, "bi-pencil", [class: "icon baseline", style: "margin-right:0.125rem;"] %>
Edit
<% end %>
<% end %>
</div>
</div> </div>
</div> </div>
@ -108,41 +114,5 @@
</div> </div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Subject</th>
<th>Description</th>
<th>Location</th>
<th>Timezone</th>
<th>Start time</th>
<th>Length</th>
<th></th>
</tr>
</thead>
<tbody id="shift_templates">
<%= for shift <- @shift_templates do %>
<tr id="shift_template-<%= shift.id %>">
<td><%= shift.subject %></td>
<td><%= shift.description %></td>
<td><%= shift.location %></td>
<td><%= shift.timezone %></td>
<td><%= shift.start_time |> Calendar.strftime("%I:%M %p") %></td>
<td><%= shift.length_hours %>h <%= shift.length_minutes || 0 %>m</td>
<td>
<span><%= live_patch "Edit", to: Routes.shift_template_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>
</div>
</div> </div>
</div> </div>

View file

@ -97,7 +97,7 @@ defmodule Shift73kWeb.UserManagementLive.Index do
@impl true @impl true
def handle_event("delete-modal", %{"id" => id}, socket) do def handle_event("delete-modal", %{"id" => id}, socket) do
{:noreply, assign(socket, :delete_user, Accounts.get_user(id))} {:noreply, assign(socket, :delete_user, Accounts.get_user!(id))}
end end
@impl true @impl true

View file

@ -94,7 +94,7 @@
</dl> </dl>
<%= if Roles.can?(@current_user, user, :edit) do %> <%= if Roles.can?(@current_user, user, :edit) do %>
<%= live_patch to: Routes.user_management_index_path(@socket, :edit, user.id, Enum.into(@query, [])), class: "btn btn-outline-primary btn-sm text-nowrap" do %> <%= live_patch to: Routes.user_management_index_path(@socket, :edit, user.id, Enum.into(@query, [])), class: "btn btn-primary btn-sm text-nowrap" do %>
<%= icon_div @socket, "bi-pencil", [class: "icon baseline", style: "margin-right:0.125rem;"] %> <%= icon_div @socket, "bi-pencil", [class: "icon baseline", style: "margin-right:0.125rem;"] %>
Edit Edit
<% end %> <% end %>