Skip to content

Commit

Permalink
fix: Adding documentation and simplifying controller pattern matching…
Browse files Browse the repository at this point in the history
… on error. #195
  • Loading branch information
LuchoTurtle committed Nov 14, 2022
1 parent 6536a5f commit 71d2f4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
22 changes: 15 additions & 7 deletions lib/app/timer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ defmodule App.Timer do
|> Repo.update()
end

@doc """
Updates a timer object inside a list with timer changesets.
This function is only useful for form validations, since it replaces the errored changeset
according to the index that is passed, alongside the list and the fields to update the timer.
It returns {:ok, []} in case the update is successful.
Otherwise, it returns {:error, updated_list}, where `error_term` is the error that occurred and `updated_list` being the updated item changeset list with the error.
"""
def update_timer_inside_changeset_list(
timer_id,
timer_start,
Expand Down Expand Up @@ -128,7 +136,7 @@ defmodule App.Timer do
:update
)

{:error_invalid_start, updated_changeset_timers_list}
{:error, updated_changeset_timers_list}

:error_not_after_others ->
updated_changeset_timers_list =
Expand All @@ -141,7 +149,7 @@ defmodule App.Timer do
:update
)

{:error_not_after_others, updated_changeset_timers_list}
{:error, updated_changeset_timers_list}
end
end
def update_timer_inside_changeset_list(
Expand Down Expand Up @@ -217,7 +225,7 @@ defmodule App.Timer do
:update
)

{:error_invalid_start, updated_changeset_timers_list}
{:error, updated_changeset_timers_list}

:error_invalid_stop ->
updated_changeset_timers_list =
Expand All @@ -230,7 +238,7 @@ defmodule App.Timer do
:update
)

{:error_invalid_stop, updated_changeset_timers_list}
{:error, updated_changeset_timers_list}

:error_overlap ->
updated_changeset_timers_list =
Expand All @@ -243,7 +251,7 @@ defmodule App.Timer do
:update
)

{:error_overlap, updated_changeset_timers_list}
{:error, updated_changeset_timers_list}

:error_start_equal_stop ->
updated_changeset_timers_list =
Expand All @@ -256,7 +264,7 @@ defmodule App.Timer do
:update
)

{:error_start_equal_stop, updated_changeset_timers_list}
{:error, updated_changeset_timers_list}

:error_start_greater_than_stop ->
updated_changeset_timers_list =
Expand All @@ -269,7 +277,7 @@ defmodule App.Timer do
:update
)

{:error_start_greater_than_stop, updated_changeset_timers_list}
{:error, updated_changeset_timers_list}
end
end

Expand Down
8 changes: 1 addition & 7 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,7 @@ defmodule AppWeb.AppLive do

case Timer.update_timer_inside_changeset_list(id, timer_start, timer_stop, index, timer_changeset_list) do
{:ok, _list} -> {:noreply, assign(socket, editing: nil, editing_timers: [])}

{:error_invalid_start, updated_list} -> {:noreply, assign(socket, editing_timers: updated_list)}
{:error_invalid_stop, updated_list} -> {:noreply, assign(socket, editing_timers: updated_list)}
{: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)}
{:error, updated_errored_list} -> {:noreply, assign(socket, editing_timers: updated_errored_list)}
end
end

Expand Down

0 comments on commit 71d2f4e

Please sign in to comment.