Skip to content

Commit

Permalink
feat: store latest workout in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullathedruid committed Dec 6, 2024
1 parent 823a67f commit 49b82f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/gym_live_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ defmodule GymLiveWeb do
those modules here.
"""

def static_paths, do: ~w(assets fonts images favicon.ico favicon.png robots.txt manifest.json sw.js js)
def static_paths,
do: ~w(assets fonts images favicon.ico favicon.png robots.txt manifest.json sw.js js)

def router do
quote do
Expand Down
32 changes: 31 additions & 1 deletion lib/gym_live_web/live/workouts/edit_workout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ defmodule GymLiveWeb.Live.Workouts.EditWorkout do

def render(assigns) do
~H"""
<div :if={@workout} class="flex flex-col min-h-screen bg-gray-50">
<div
:if={@workout}
class="flex flex-col min-h-screen bg-gray-50"
id="container"
phx-hook="WorkoutForm"
>
<.modal id="cancel-modal">
<p>
Are you sure you want to abandon your workout? Your workout data will be lost.
Expand Down Expand Up @@ -185,9 +190,34 @@ defmodule GymLiveWeb.Live.Workouts.EditWorkout do
|> Map.put(:action, :insert)
|> to_form()

{:noreply,
assign(socket, form: form)
|> push_event("persist_form", %{
exercise: params["exercise"],
weight: params["weight"],
reps: params["reps"],
workout_id: socket.assigns.workout.id
})}
end

def handle_event(
"restore_form",
params = %{"workout_id" => workout_id},
socket = %{assigns: %{workout: %Workout{id: workout_id}}}
) do
form =
%Set{}
|> Training.change_set(params)
|> Map.put(:action, :insert)
|> to_form()

{:noreply, assign(socket, form: form)}
end

def handle_event("restore_form", _params, socket) do
{:noreply, socket}
end

def handle_event("save_set", %{"set" => params}, socket) do
case Training.create_set(socket.assigns.workout.id, params) do
{:ok, set} ->
Expand Down

0 comments on commit 49b82f5

Please sign in to comment.