diff --git a/lib/shift73k/shifts.ex b/lib/shift73k/shifts.ex index 6e92438f..1ccc8672 100644 --- a/lib/shift73k/shifts.ex +++ b/lib/shift73k/shifts.ex @@ -86,8 +86,12 @@ defmodule Shift73k.Shifts do ** (Ecto.NoResultsError) """ + def get_shift!(nil), do: nil def get_shift!(id), do: Repo.get!(Shift, id) + def get_shift(nil), do: nil + def get_shift(id), do: Repo.get(Shift, id) + @doc """ Creates a shift. diff --git a/lib/shift73k_web/live/shift_live/delete_component.ex b/lib/shift73k_web/live/shift_live/delete_component.ex new file mode 100644 index 00000000..05c41f0c --- /dev/null +++ b/lib/shift73k_web/live/shift_live/delete_component.ex @@ -0,0 +1,46 @@ +defmodule Shift73kWeb.ShiftLive.DeleteComponent do + use Shift73kWeb, :live_component + + alias Shift73k.Shifts + + @impl true + def update(assigns, socket) do + socket + |> assign(assigns) + |> live_okreply() + end + + @impl true + def handle_event("confirm", %{"id" => id, "subject" => subject, "datetime" => datetime}, socket) do + shift = Shifts.get_shift(id) + + if (shift) do + shift + |> Shifts.delete_shift() + |> case do + {:ok, _} -> + flash = {:info, "Shift deleted successfully: \"#{subject}\""} + send(self(), {:put_flash_message, flash}) + + socket + |> push_event("modal-please-hide", %{}) + |> live_noreply() + + {:error, _} -> + handle_error(socket, subject, datetime) + end + end + end + + defp handle_error(socket, subject, datetime) do + flash = + {:error, + "Some error trying to delete shift \"#{subject} (#{datetime})\". Possibly already deleted? Reloading list..."} + + send(self(), {:put_flash_message, flash}) + + socket + |> push_event("modal-please-hide", %{}) + |> live_noreply() + end +end diff --git a/lib/shift73k_web/live/shift_live/delete_component.html.heex b/lib/shift73k_web/live/shift_live/delete_component.html.heex new file mode 100644 index 00000000..45b5d332 --- /dev/null +++ b/lib/shift73k_web/live/shift_live/delete_component.html.heex @@ -0,0 +1,22 @@ +
+ + + + +
diff --git a/lib/shift73k_web/live/shift_live/index.ex b/lib/shift73k_web/live/shift_live/index.ex index 36372328..19fe63d8 100644 --- a/lib/shift73k_web/live/shift_live/index.ex +++ b/lib/shift73k_web/live/shift_live/index.ex @@ -22,6 +22,7 @@ defmodule Shift73kWeb.ShiftLive.Index do socket |> init_today(Date.utc_today()) |> update_agenda() + |> assign_modal_close_handlers() |> assign(:delete_shift, nil) |> apply_action(socket.assigns.live_action, params) |> live_noreply() @@ -33,6 +34,11 @@ defmodule Shift73kWeb.ShiftLive.Index do end end + defp assign_modal_close_handlers(socket) do + to = Routes.shift_index_path(socket, :index) + assign(socket, modal_return_to: to, modal_close_action: :return) + end + defp apply_action(socket, :index, _params) do socket |> assign(:page_title, "My Shifts") @@ -76,6 +82,14 @@ defmodule Shift73kWeb.ShiftLive.Index do |> assign_known_shifts() end + @impl true + def handle_event("delete-modal", %{"id" => id}, socket) do + socket + |> assign(:modal_close_action, :delete_shift) + |> assign(:delete_shift, Shifts.get_shift!(id)) + |> live_noreply() + end + @impl true def handle_event("delete", %{"id" => id}, socket) do shift = Shifts.get_shift!(id) @@ -94,6 +108,28 @@ defmodule Shift73kWeb.ShiftLive.Index do |> live_noreply() end + @impl true + def handle_info({:close_modal, _}, %{assigns: %{modal_close_action: :return}} = socket) do + socket + |> copy_flash() + |> push_patch(to: socket.assigns.modal_return_to) + |> live_noreply() + end + + @impl true + def handle_info({:close_modal, _}, %{assigns: %{modal_close_action: assign_key}} = socket) do + socket + |> assign(assign_key, nil) + |> assign_modal_close_handlers() + |> assign_known_shifts() + |> 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 + defp new_nav_cursor("now", _cursor_date), do: Date.utc_today() defp new_nav_cursor(nav, cursor_date) do diff --git a/lib/shift73k_web/live/shift_live/index.html.leex b/lib/shift73k_web/live/shift_live/index.html.heex similarity index 65% rename from lib/shift73k_web/live/shift_live/index.html.leex rename to lib/shift73k_web/live/shift_live/index.html.heex index 5e0191e4..bc9cdf08 100644 --- a/lib/shift73k_web/live/shift_live/index.html.leex +++ b/lib/shift73k_web/live/shift_live/index.html.heex @@ -1,40 +1,42 @@ +<%= if @delete_shift do %> + <%= live_modal @socket, Shift73kWeb.ShiftLive.DeleteComponent, + id: @delete_shift.id, + title: "Delete Shift Template", + delete_shift: @delete_shift %> +<% end %> + +

- <%= icon_div @socket, "bi-card-list", [class: "icon baseline"] %> - My Shifts + My Shifts

- - -
- <%# month navigation %>

<%= Calendar.strftime(@cursor_date, "%B %Y") %>

-
- <%= for day <- Enum.to_list(@date_range) do %> <%= if Date.day_of_week(day, @current_user.week_start_at) == 1 do %>
@@ -45,24 +47,21 @@ <% day_shifts = Enum.filter(@shifts, fn s -> s.date == day end) %> <%= if !Enum.empty?(day_shifts) do %> - <%= for shift <- day_shifts do %> - -
+
- <%= icon_div @socket, "bi-tag", [class: "icon baseline text-muted me-1"] %> + <%= shift.subject %>
-
- <%= icon_div @socket, "bi-hourglass", [class: "icon baseline text-muted"] %> + Hours: @@ -79,7 +78,7 @@
- <%= icon_div @socket, "bi-geo", [class: "icon baseline text-muted"] %> + Location: @@ -92,7 +91,7 @@
- <%= icon_div @socket, "bi-justify-left", [class: "icon baseline text-muted"] %> + Description: @@ -106,51 +105,22 @@
- <%#= if Roles.can?(@current_user, template, :edit) do %> - <%#= live_patch to: Routes.shift_template_index_path(@socket, :edit, template), class: "btn btn-primary btn-sm text-nowrap" do %> - <%#= icon_div @socket, "bi-pencil", [class: "icon baseline"] %> - <%# Edit %> - <%# end %> - <%# end %> - - <%#= if Roles.can?(@current_user, template, :delete) do %> - <%# %> - <%# end %> - - <%= button 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"] %> - Delete - <% end %> - - +
- - - <% end %> - - - - - <% else %>

Nothing scheduled

<% end %> <% end %> -
-