Commit 06feacd

mo khan <mo@mokhan.ca>
2016-06-05 19:35:54
draw buttons and update status after entering reps completed.
1 parent e2c6d40
Changed files (4)
app/assets/javascripts/templates/training_session_view.ractive
@@ -1,5 +1,5 @@
 <div class="row">
-  {{#each exercises}}
+  {{#each json.exercises}}
   <div class="panel small-12 columns">
     <div class="row">
       <div class="small-6 columns">
@@ -14,8 +14,8 @@
 
     <div class="row">
       <div class="small-12 columns">
-      {{#each completed_sets}}
-        <button class="button small round secondary">{{.}}</button>
+      {{#each reps}}
+        <button on-click="completeSet" class="button small round secondary {{status}}">{{completed}}</button>
       {{/each}}
       </div>
     </div>
app/assets/javascripts/views/training_session_view.js.coffee
@@ -1,2 +1,25 @@
-Stronglifters.TrainingSessionView = Ractive.extend
+class Stronglifters.TrainingSessionView extends Ractive
   template: RactiveTemplates["templates/training_session_view"]
+
+  oninit: ->
+    @on 'completeSet', (event) -> @completeSet(event)
+    @observe '*.exercises.*.reps.*', (newValue, oldValue, keypath) ->
+      @updateStatus(newValue, oldValue, keypath)
+
+  completeSet: (event) ->
+    console.log(event)
+    if @get("#{event.keypath}.completed") == 0
+      @set("#{event.keypath}.completed", @get("#{event.keypath}.target"))
+    else
+      @subtract("#{event.keypath}.completed")
+
+  updateStatus: (newValue, oldValue, keyPath) ->
+    console.log([newValue, oldValue, keyPath])
+    if @get("#{keyPath}.completed") == 0
+      @set("#{keyPath}.status", "secondary")
+      return
+
+    if @get("#{keyPath}.target") == @get("#{keyPath}.completed")
+      @set("#{keyPath}.status", "success")
+    else
+      @set("#{keyPath}.status", "alert")
app/views/training_sessions/_edit.json.jbuilder
@@ -6,8 +6,11 @@ end
 json.exercises training_session.workout.exercise_workouts do |exercise|
   json.name exercise.name
   json.sets exercise.sets
-  json.completed_sets training_session.progress_for(exercise).try(:sets) || exercise.sets.times.map { |x| 0 }
   json.repetitions exercise.repetitions
+  json.reps training_session.progress_for(exercise).try(:sets) || exercise.sets.times.map { |x| 0 } do |completed_reps|
+    json.target exercise.repetitions
+    json.completed completed_reps
+  end
   json.target_weight current_user.next_weight_for(exercise.exercise)
 end
 json.count training_session.exercise_sessions.count
app/views/training_sessions/edit.html.erb
@@ -1,5 +1,6 @@
 <div class="row">
   <div class="large-12 columns">
+    <%= render partial: 'edit.json.jbuilder', locals: { training_session: @training_session } %>
     <div id="training-session-view">
     </div>
   </div>
@@ -11,6 +12,9 @@ var json = <%= raw render partial: 'edit.json.jbuilder', locals: { training_sess
 var model = new Stronglifters.TrainingSession(json);
 window.currentView = new Stronglifters.TrainingSessionView({
   el: 'training-session-view',
-  data: json
+  data: {
+    json: json,
+    model: model
+  }
 })
 </script>