Skip to content

Commit

Permalink
Add inline edit on workout page (#84)
Browse files Browse the repository at this point in the history
* Add inline edit on workout page

* render different components based on presence of notes

* put edit button back

* Kondo

* Kondo
  • Loading branch information
theianjones committed Nov 29, 2023
1 parent f0f0734 commit f38c6bc
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 121 deletions.
7 changes: 4 additions & 3 deletions src/com/spicy/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[com.spicy.middleware :as mid]
[com.spicy.movements.core :as movements]
[com.spicy.results.core :as results]
[com.spicy.results.transform :as t]
[com.spicy.results.ui :as r]
[com.spicy.settings :as settings]
[com.spicy.sugarwod.core :as sugar.core]
Expand Down Expand Up @@ -52,11 +53,11 @@
[:p "Log a WOD result to see it show up here!"]
[:a.btn {:href "/app/workouts"} "View Workouts"]]
[:div (map (fn [result]
(let [{:keys [workout name date] :as normalized} (r/normalized-result result)]
(let [{:keys [workout name date] :as normalized} (t/normalized-result result)]
[:div.mb-2
[:a.text-lg.font-sans {:href (str "/app/workouts/" (:xt/id workout))} [:h3.font-sans name]]
[:div
[:span.font-bold (w/display-summed-score normalized)]
[:span.font-bold (t/display-summed-score normalized)]
[:span.text-gray-700.ml-2 date]]])) wod-results)])]
[:div.p-4.border-2.border-black.bg-white
[:h2.text-2xl.font-bold "Latest Lifts"]
Expand All @@ -66,7 +67,7 @@
[:a.btn {:href "/app/movements"} "View Movements"]]

[:div (map (fn [result]
(let [{:keys [movement name date sets] :as normalized} (r/normalized-result result)]
(let [{:keys [movement name date sets] :as _normalized} (t/normalized-result result)]
[:div.mb-2
[:a.text-lg.font-sans {:href (str "/app/movements/" (:xt/id movement))} [:h3.font-sans.capitalize name]]
[:div
Expand Down
6 changes: 3 additions & 3 deletions src/com/spicy/calendar.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require
[clojure.string :as string]
[com.biffweb :as biff]
[com.spicy.results.ui :as r]
[com.spicy.results.transform :as t]
[java-time.api :as jt]))


Expand Down Expand Up @@ -114,7 +114,7 @@
(map (comp (fn [{:keys [name href]}]
[:li
[:a.group.flex {:href href}
[:p.flex-auto.truncate.font-medium.font-semibold.text-gray-900.group-hover:text-brand-teal name]]]) r/normalized-result) results)])]))
[:p.flex-auto.truncate.font-medium.font-semibold.text-gray-900.group-hover:text-brand-teal name]]]) t/normalized-result) results)])]))


(defn mobile-day
Expand Down Expand Up @@ -201,7 +201,7 @@
[:div.flex-auto
[:p.font-semibold.text-gray-900 name]]
[:a.ml-6.flex-none.self-center.bg-white.px-3.py-2.font-semibold.text-gray-900.ring-1.ring-inset.ring-black.hover:ring-gray-400 {:href href} "View"
[:span.sr-only (str ", " name)]]]) r/normalized-result) results)]]
[:span.sr-only (str ", " name)]]]) t/normalized-result) results)]]
[:div.px-4.py-10.sm:px-6.lg:hidden
{:id "mobile-day-view"}
[:h2.mb-4 (jt/format "EEE, MMMM dd" date)]
Expand Down
28 changes: 18 additions & 10 deletions src/com/spicy/results/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
[com.spicy.middleware :as mid]
[com.spicy.movements.ui :refer [strength-set-inputs]]
[com.spicy.results.score :refer [scores->tx ->scores]]
[com.spicy.results.ui :refer [result-ui result-form normalized-result]]
[com.spicy.results.transform :as t]
[com.spicy.results.ui :refer [result-ui result-form inline-result-ui]]
[com.spicy.route-helpers :refer [wildcard-override]]
[com.spicy.ui :as ui]
[java-time.api :as jt]
[xtdb.api :as xt]))


(defn workouts-referer?
[{:keys [headers] :as _ctx}]
(let [referer (get headers "referer")]
(re-matches #".*/workouts/.*" referer)))


(defn show
[{:keys [biff/db session path-params]}]
[{:keys [biff/db session path-params] :as ctx}]
(let [result (first (biff/q db '{:find (pull result [* {:result/type [*
{:result/workout [*]}
{:result/movement [*]}
Expand All @@ -23,7 +30,9 @@
:where [[result :xt/id result-id]
[result :result/user user]]}
[(parse-uuid (:id path-params)) (:uid session)]))]
(result-ui result)))
(if (workouts-referer? ctx)
(inline-result-ui result)
(result-ui result))))


(defn update-handler
Expand Down Expand Up @@ -80,7 +89,7 @@
workout? (seq (:result/workout type))
movement? (seq (:result/movement type))]
[:div#edit-result
[:h2.text-2xl.font-bold (:name (normalized-result result))]
[:h2.text-2xl.font-bold (:name (t/normalized-result result))]
(when movement?
(biff/form {:hidden {:sets (count (-> result :result/type :result-set/_parent))
:strength-result-id (:xt/id type)
Expand Down Expand Up @@ -126,11 +135,10 @@
(defn index
[{:keys [biff/db session] :as ctx}]
(let [date-and-results (biff/q db '{:find [date (pull result
[*
{:result/type [*
{:result/workout [*]}
{:result/movement [*]}
{:result-set/_parent [*]}]}])]
[* {:result/type [*
{:result/workout [*]}
{:result/movement [*]}
{:result-set/_parent [*]}]}])]
:in [[user]]
:where [[result :result/user user]
[result :result/date date]]
Expand Down Expand Up @@ -260,7 +268,7 @@
:rounds-to-score rounds-to-score})))]

(biff/submit-tx ctx (concat result-tx wod-sets-tx)))
(let [{:xt/keys [id] :as w} (xt/entity db (parse-uuid (:workout params)))]
(let [{:xt/keys [id] :as _w} (xt/entity db (parse-uuid (:workout params)))]
{:status 303
:headers {"location" (str "/app/workouts/" id)}}))

Expand Down
78 changes: 78 additions & 0 deletions src/com/spicy/results/transform.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
(ns com.spicy.results.transform
(:require
[clojure.string :as string]
[com.biffweb :as biff]
[com.spicy.movements.core :refer [sets-n-reps]]
[com.spicy.results.score :refer [REPS_MULTIPLIER]]
[com.spicy.time :as t]))


(defn ->rounds-reps
[reps-per-round score]
(if (int? score)
(if (> 0 score)
"0+0"
(str (quot score reps-per-round)
"+"
(rem score reps-per-round)))
(let [rounds (.intValue score)
reps (.intValue (* REPS_MULTIPLIER (- (bigdec score) rounds)))]
(str rounds "+" reps))))


(defn display-score
[{:keys [result-set workout]}]
(case (:workout/scheme workout)
:rounds-reps (->rounds-reps (:workout/reps-per-round workout) (:result-set/score result-set))
:time (t/->time (:result-set/score result-set))
(:result-set/score result-set)))


(defn merge-set-score-with
[sets f]
(apply (partial merge-with f) (map #(select-keys % [:result-set/score]) sets)))


(defn display-summed-score
[{:keys [workout sets] :as _a}]
(when (and (not-empty workout)
(not-empty sets))
(display-score (merge {:workout workout} {:result-set (merge-set-score-with sets +)}))))


(defn normalized-result
[result]
(let [workout (-> result :result/type :result/workout)
movement (-> result :result/type :result/movement)
sets (-> result :result/type :result-set/_parent)
notes (-> result :result/type :result/notes)
scale (-> result :result/type :result/scale)
description (if (seq workout)
(-> workout :workout/description)
(str (sets-n-reps sets)))
score (if (seq workout)
(display-summed-score {:workout workout
:sets sets})
(let [best-set (first
(sort-by :result-set/weight >
(filter
#(= :pass (:result-set/status %)) sets)))]
(str (:result-set/reps best-set) "x" (:result-set/weight best-set))))
name (or (some-> movement
:movement/name
string/capitalize
(str " (" (sets-n-reps sets) ")"))
(:workout/name workout))
href (if (:workout/name workout)
(str "/app/workouts/" (:xt/id workout))
(str "/app/movements/" (:xt/id movement)))]
{:workout workout
:movement movement
:sets sets
:href href
:name name
:description description
:notes notes
:score score
:scale scale
:date (biff/format-date (:result/date result) "YYYY-MM-dd")}))
81 changes: 40 additions & 41 deletions src/com/spicy/results/ui.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,10 @@
(:require
[clojure.string :as string]
[com.biffweb :as biff]
[com.spicy.movements.core :refer [sets-n-reps]]
[com.spicy.workouts.ui :refer [display-summed-score]]
[com.spicy.results.transform :as t]
[java-time.api :as jt]))


(defn normalized-result
[result]
(let [workout (-> result :result/type :result/workout)
movement (-> result :result/type :result/movement)
sets (-> result :result/type :result-set/_parent)
notes (-> result :result/type :result/notes)
description (if (seq workout)
(-> workout :workout/description)
(str (sets-n-reps sets)))
score (if (seq workout)
(display-summed-score {:workout workout
:sets sets})
(let [best-set (first
(sort-by :result-set/weight >
(filter
#(= :pass (:result-set/status %)) sets)))]
(str (:result-set/reps best-set) "x" (:result-set/weight best-set))))
name (or (some-> movement
:movement/name
string/capitalize
(str " (" (sets-n-reps sets) ")"))
(:workout/name workout))
href (if (:workout/name workout)
(str "/app/workouts/" (:xt/id workout))
(str "/app/movements/" (:xt/id movement)))]
{:workout workout
:movement movement
:sets sets
:href href
:name name
:description description
:notes notes
:score score
:date (biff/format-date (:result/date result) "YYYY-MM-dd")}))


(defn card
[{:keys [name href date description notes score]} & children]
[:div.flex.flex-col.max-w-sm.sm:max-w-xl.mx-auto#result-ui
Expand Down Expand Up @@ -73,13 +36,49 @@

(defn result-ui
[result]
(card (normalized-result result)
(card (t/normalized-result result)
[:button {:hx-get (str "/app/results/" (:xt/id result) "/edit")
:hx-target "closest #result-ui"
:hx-swap "outerHTML"
:class (str "self-end btn-no-shadow bg-white text-sm px-4 py-2 font-normal ")} "edit"]))


(defn inline-result
[result & children]
(let [{:keys [score date notes scale] :as _normalized} (t/normalized-result result)]
(if (not-empty notes)
[:div.flex.flex-col.max-w-sm.sm:max-w-xl.mx-auto#result-ui
[:.flex.flex-row.items-baseline.justify-between.mb-3
[:div.text-2xl.font-bold score]
[:p.whitespace-pre-wrap.sm:text-left.max-w-xs.text-gray-700.mb-0 date]]
[:.flex.justify-between
[:div.flex.flex-col.gap-2
[:div.flex.justify-between
(when (not-empty notes)
[:span
notes])]]
(when (not-empty notes)
children)]]
[:div.flex.gap-3.flex-col#result-ui
[:.flex.justify-between.flex-wrap.gap-2
[:div.text-2xl.font-bold.self-center score
[:span.grow.pl-2.font-normal (name scale)]]
[:div.flex.gap-2
[:div.self-center (biff/format-date
(:result/date result) "EEE, YYYY-MM-dd")]
children]]
(when notes [:div notes])])))


(defn inline-result-ui
[result]
(inline-result result
[:button {:hx-get (str "/app/results/" (:xt/id result) "/edit")
:hx-target "closest #result-ui"
:hx-swap "outerHTML"
:class (str "self-end btn-no-shadow bg-white text-sm px-4 py-2 font-normal ")} "edit"]))


(defn scheme-forms
[{:keys [workout score identifier]
:or {identifier 0}}]
Expand Down Expand Up @@ -148,7 +147,7 @@
(if (= 1 rounds-to-score)
[:div
(scheme-forms (assoc {}
:score (display-summed-score {:workout w :sets (-> workout-result :result-set/_parent)})
:score (t/display-summed-score {:workout w :sets (-> workout-result :result-set/_parent)})
:workout w))
[:input {:type "hidden"
:name "id-0"
Expand All @@ -159,7 +158,7 @@
[:p.m-0.w-2 (str (inc i) ".")]
(scheme-forms (assoc {}
:workout w
:score (display-summed-score
:score (t/display-summed-score
{:workout workout
:sets (filter (complement nil?) [(nth (-> workout-result :result-set/_parent) i)])})
:identifier i))
Expand Down
10 changes: 5 additions & 5 deletions src/com/spicy/sugarwod/transform.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
"Push Press" "push press"
"Sotts Press" "sotts press"
"Clean & Jerk" "clean and jerk"
"Power Clean" "power clean"
"Power Clean" "power clean"
"Power Clean & Jerk" "power clean and jerk"
"Clean" "clean"
"Muscle Clean" "muscle clean"
"Hang Clean" "hang clean"
"Hang Power Clean" "hang power clean" "Clean Pull" "clean pull"
"Hang Power Clean" "hang power clean"
"Clean Pull" "clean pull"
"Hang Squat Clean" "hang squat clean"
"Squat Clean" "squat clean"
"Squat Clean Thruster" "squat clean thruster (cluster)"
"Thruster" "thruster"
"Thruster" "thruster"
"Snatch" "snatch"
"Hang Power Snatch" "hang power snatch"
"Snatch Grip Push Press" "snatch grip push press"
Expand All @@ -59,8 +60,7 @@
"Overhead Squat" "overhead squat"
"Box Squat" "box squat"
"Front Squat" "front squat"
"Front Pause Squat" "front pause squat"
})
"Front Pause Squat" "front pause squat"})


(defn sugar-lift->xt-id
Expand Down
17 changes: 17 additions & 0 deletions src/com/spicy/sugarwod/workouts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as string]
[com.biffweb :as biff]
[com.spicy.numbers :as n]
[com.spicy.sugarwod.transform :as t]))

Expand Down Expand Up @@ -86,3 +87,19 @@
(defn ->spicy
[sugar-wod]
(t/transformer sgw->spw sugar-wod))


(defn add-tx-data
[spicy-wod]
(assoc spicy-wod
:workout/created-at :db/now
:db/doc-type :workout))


(comment
(require '[com.spicy.repl :refer [get-context]])
(def workouts (concat girls heroes))
(def spicy-ws (mapv (comp add-tx-data ->spicy) workouts))

(let [ctx (get-context)]
(biff/submit-tx ctx spicy-ws)))
Loading

0 comments on commit f38c6bc

Please sign in to comment.