Skip to content

Commit

Permalink
fix: Fixing tests on ongoing timers and refactoring to Timer.ex. #195
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Nov 14, 2022
1 parent 0e5a49e commit 6536a5f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 deletions.
64 changes: 64 additions & 0 deletions lib/app/timer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 1 addition & 60 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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",
%{
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/app_web/live/app_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 6536a5f

Please sign in to comment.