Skip to content

Commit

Permalink
feat: group by weight
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullathedruid committed Jun 3, 2024
1 parent 0ef5463 commit 493a336
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/gym_live_web/live/app/view_workout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ defmodule GymLiveWeb.ViewWorkout do
{:ok, socket}
end

defp group_by_weight(sets), do: group_by_weight(sets, [])

defp group_by_weight([], acc), do: Enum.reverse(acc)

defp group_by_weight([hd | tl], []) do
group_by_weight(tl, [{hd.weight, [hd]}])
end

defp group_by_weight([hd | tl], [{prev_weight, prev_sets} | rest] = acc) do
if hd.weight == prev_weight do
group_by_weight(tl, [{hd.weight, prev_sets ++ [hd]} | rest])
else
group_by_weight(tl, [{hd.weight, [hd]} | acc])
end
end

def render(assigns) do
~H"""
<div class="mx-auto px-3 py-3">
Expand All @@ -35,7 +51,7 @@ defmodule GymLiveWeb.ViewWorkout do
<%= Exercises.get_exercise_name(exercise) %>
</div>
<div
:for={{weight, sets_per_weight} <- Enum.group_by(sets, & &1.weight)}
:for={{weight, sets_per_weight} <- group_by_weight(sets) |> dbg()}
class="flex flex-row gap-3 justify-center"
>
<div class="">
Expand Down Expand Up @@ -87,6 +103,7 @@ defmodule GymLiveWeb.ViewWorkout do
{:noreply, socket}
end

# deprecated
def calculate_scores(sets) do
Enum.reduce(sets, %{}, fn %Set{reps: reps, weight: weight, exercise: exercise}, acc ->
if exercise in [:squat, :press, :bench_press, :deadlift] do
Expand Down

0 comments on commit 493a336

Please sign in to comment.