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 @@ +
- <%= 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 @@ |
Nothing scheduled
<% end %> <% end %> -