diff --git a/lib/app/timer.ex b/lib/app/timer.ex index a76151ac..27ab2244 100644 --- a/lib/app/timer.ex +++ b/lib/app/timer.ex @@ -80,6 +80,70 @@ defmodule App.Timer do |> Repo.update() end + def update_timer_inside_changeset_list( + timer_id, + timer_start, + timer_stop, + index, + timer_changeset_list + ) when timer_stop == "" or timer_stop == nil do + + # Getting the changeset to change in case there's an error + changeset_obj = Enum.at(timer_changeset_list, index) + + try do + + # Parsing the dates + {start_op, start} = + Timex.parse(timer_start, "%Y-%m-%dT%H:%M:%S", :strftime) + + # Error guards when parsing the date + if start_op === :error do + throw(:error_invalid_start) + end + + # Getting a list of the other timers (the rest we aren't updating) + other_timers_list = List.delete_at(timer_changeset_list, index) + + # Latest timer end + max_end = + other_timers_list |> Enum.map(fn chs -> chs.data.stop end) |> Enum.max() + + case NaiveDateTime.compare(start, max_end) do + :gt -> + update_timer(%{id: timer_id, start: start, stop: nil}) + {:ok, []} + + _ -> throw(:error_not_after_others) + end + catch + :error_invalid_start -> + updated_changeset_timers_list = + Timer.error_timer_changeset( + timer_changeset_list, + changeset_obj, + index, + :id, + "Start field has an invalid date format.", + :update + ) + + {:error_invalid_start, updated_changeset_timers_list} + + :error_not_after_others -> + updated_changeset_timers_list = + Timer.error_timer_changeset( + timer_changeset_list, + changeset_obj, + index, + :id, + "When editing an ongoing timer, make sure it's after all the others.", + :update + ) + + {:error_not_after_others, updated_changeset_timers_list} + end + end def update_timer_inside_changeset_list( timer_id, timer_start, diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index 88e6a0dd..ef4c7893 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -121,66 +121,6 @@ defmodule AppWeb.AppLive do end @impl true - def handle_event( - "update-item-timer", - %{ - "timer_id" => id, - "index" => index, - "timer_start" => timer_start, - "timer_stop" => timer_stop - }, - socket - ) - when timer_stop == "" do - timer_changeset_list = socket.assigns.editing_timers - index = String.to_integer(index) - changeset_obj = Enum.at(timer_changeset_list, index) - - try do - start = - Timex.parse!(timer_start, "%Y-%m-%dT%H:%M:%S", :strftime) - - other_timers_list = List.delete_at(socket.assigns.editing_timers, index) - - max_end = - other_timers_list |> Enum.map(fn chs -> chs.data.stop end) |> Enum.max() - - case NaiveDateTime.compare(start, max_end) do - :gt -> - Timer.update_timer(%{id: id, start: start, stop: nil}) - {:noreply, assign(socket, editing: nil, editing_timers: [])} - - _ -> - updated_changeset_timers_list = - Timer.error_timer_changeset( - timer_changeset_list, - changeset_obj, - index, - :id, - "When editing an ongoing timer, make sure it's after all the others.", - :update - ) - - {:noreply, - assign(socket, editing_timers: updated_changeset_timers_list)} - end - rescue - _e -> - updated_changeset_timers_list = - Timer.error_timer_changeset( - timer_changeset_list, - changeset_obj, - index, - :id, - "Date format invalid on either start or stop.", - :update - ) - - {:noreply, - assign(socket, editing_timers: updated_changeset_timers_list)} - end - end - def handle_event( "update-item-timer", %{ @@ -203,6 +143,7 @@ defmodule AppWeb.AppLive do {:error_start_greater_than_stop, updated_list} -> {:noreply, assign(socket, editing_timers: updated_list)} {:error_start_equal_stop, updated_list} -> {:noreply, assign(socket, editing_timers: updated_list)} {:error_overlap, updated_list} -> {:noreply, assign(socket, editing_timers: updated_list)} + {:error_not_after_others, updated_list} -> {:noreply, assign(socket, editing_timers: updated_list)} end end diff --git a/test/app_web/live/app_live_test.exs b/test/app_web/live/app_live_test.exs index 4c2aa039..a88abc8d 100644 --- a/test/app_web/live/app_live_test.exs +++ b/test/app_web/live/app_live_test.exs @@ -356,7 +356,7 @@ defmodule AppWeb.AppLiveTest do "timer_stop" => "" }) - assert error_format_view =~ "Date format invalid on either start or stop." + assert error_format_view =~ "Start field has an invalid date format." # Update successful ----------- ten_seconds_after_string =