diff --git a/lib/shift73k/shift_templates.ex b/lib/shift73k/shift_templates.ex index c3079102..b5f7139a 100644 --- a/lib/shift73k/shift_templates.ex +++ b/lib/shift73k/shift_templates.ex @@ -22,7 +22,7 @@ defmodule Shift73k.ShiftTemplates do end 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() 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) + @doc """ Creates a shift_template. diff --git a/lib/shift73k_web/live/shift_template_live/delete_component.ex b/lib/shift73k_web/live/shift_template_live/delete_component.ex new file mode 100644 index 00000000..47f62f73 --- /dev/null +++ b/lib/shift73k_web/live/shift_template_live/delete_component.ex @@ -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 diff --git a/lib/shift73k_web/live/shift_template_live/delete_component.html.leex b/lib/shift73k_web/live/shift_template_live/delete_component.html.leex new file mode 100644 index 00000000..8047f7c8 --- /dev/null +++ b/lib/shift73k_web/live/shift_template_live/delete_component.html.leex @@ -0,0 +1,23 @@ + + diff --git a/lib/shift73k_web/live/shift_template_live/index.ex b/lib/shift73k_web/live/shift_template_live/index.ex index 65efc1cc..467afd4c 100644 --- a/lib/shift73k_web/live/shift_template_live/index.ex +++ b/lib/shift73k_web/live/shift_template_live/index.ex @@ -19,7 +19,6 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do 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() @@ -66,15 +65,19 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do 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_shift_templates(socket)} + def handle_event("delete-modal", %{"id" => id}, socket) do + {:noreply, assign(socket, :delete_shift_template, ShiftTemplates.get_shift_template!(id))} 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 def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do 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 socket |> put_flash(flash_type, msg) |> live_noreply() end - end diff --git a/lib/shift73k_web/live/shift_template_live/index.html.leex b/lib/shift73k_web/live/shift_template_live/index.html.leex index a88400e2..22c4a554 100644 --- a/lib/shift73k_web/live/shift_template_live/index.html.leex +++ b/lib/shift73k_web/live/shift_template_live/index.html.leex @@ -7,6 +7,14 @@ current_user: @current_user %> <% 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 %> +
@@ -83,22 +91,20 @@ -
- - <%= if Roles.can?(@current_user, shift, :delete) do %> - <%= 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 %> - <%= icon_div @socket, "bi-trash", [class: "icon baseline", style: "margin-right:0.125rem;"] %> - Delete - <% 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 %> + + <%= if Roles.can?(@current_user, shift, :delete) do %> + + <% 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 %> -
@@ -108,41 +114,5 @@ - - -
- - - - - - - - - - - - - - - <%= for shift <- @shift_templates do %> - - - - - - - - - - - <% end %> - -
SubjectDescriptionLocationTimezoneStart timeLength
<%= shift.subject %><%= shift.description %><%= shift.location %><%= shift.timezone %><%= shift.start_time |> Calendar.strftime("%I:%M %p") %><%= shift.length_hours %>h <%= shift.length_minutes || 0 %>m - <%= live_patch "Edit", to: Routes.shift_template_index_path(@socket, :edit, shift) %> - <%= link "Delete", to: "#", phx_click: "delete", phx_value_id: shift.id, data: [confirm: "Are you sure?"] %> -
-
- diff --git a/lib/shift73k_web/live/user_management/index.ex b/lib/shift73k_web/live/user_management/index.ex index 1b92e9b4..782c7cc4 100644 --- a/lib/shift73k_web/live/user_management/index.ex +++ b/lib/shift73k_web/live/user_management/index.ex @@ -97,7 +97,7 @@ defmodule Shift73kWeb.UserManagementLive.Index do @impl true 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 @impl true diff --git a/lib/shift73k_web/live/user_management/index.html.leex b/lib/shift73k_web/live/user_management/index.html.leex index 8861fc1a..714346b2 100644 --- a/lib/shift73k_web/live/user_management/index.html.leex +++ b/lib/shift73k_web/live/user_management/index.html.leex @@ -94,7 +94,7 @@ <%= 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;"] %> Edit <% end %>