From 98ec25e3026362bc84a0e4a3dc9abac7c3dd2187 Mon Sep 17 00:00:00 2001 From: jason poage Date: Mon, 14 Jan 2019 21:53:33 -0700 Subject: [PATCH 1/3] modified: src/cljs/nightcoders/pizza.cljs --- .nightlight.edn | 2 +- src/cljs/nightcoders/pizza.cljs | 59 +++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/.nightlight.edn b/.nightlight.edn index db9a57b..a576f16 100644 --- a/.nightlight.edn +++ b/.nightlight.edn @@ -1 +1 @@ -{:auto-save? true, :theme :light, :selection "/Users/davidviramontes/local/cfd/code4pizza/src/cljs/nightcoders/pizza.cljs"} \ No newline at end of file +{:auto-save? true, :theme :light, :selection "*REPL*"} \ No newline at end of file diff --git a/src/cljs/nightcoders/pizza.cljs b/src/cljs/nightcoders/pizza.cljs index 1d457b8..bd3f395 100755 --- a/src/cljs/nightcoders/pizza.cljs +++ b/src/cljs/nightcoders/pizza.cljs @@ -7,6 +7,10 @@ (def number (r/atom 0)) +(def default-price-per-pie 10) + +(def price-per-pie (r/atom 0)) + (def sizes {:SMALL 0.5 :MEDIUM 0.4 :LARGE 0.3 @@ -33,29 +37,33 @@ (def tip-percent (r/atom 0)) (def total-charge (r/atom 0)) -(defn total-charge-input [] - [:input {:label "total-charge" - :type "number" - :on-change - (fn [e] - (let [num (js/parseFloat (-> e .-target .-value))] - (if (js/isNaN num) - (reset! total-charge 0) - (reset! total-charge num))))}]) - (defn tip-percent-input [] [:input {:label "tip-percent" :type "number" :on-change (fn [e] + (let [num (js/parseFloat (-> e .-target .-value))] (if (js/isNaN num) (reset! tip-percent 0) (reset! tip-percent num))))}]) +(defn price-per-pie-input [] + [:input {:label "price-per-pie" + :type "number" + :on-change + (fn [e] + (let [num (js/parseFloat (-> e .-target .-value)) + new-total (* @total num)] + (if (js/isNaN num) + (reset! price-per-pie @default-price-per-pie) + (reset! price-per-pie num)) + (reset! total-charge new-total)))}]) + (defn calc-tip [total tip] (/ (js/Math.round (* 100 (* total (/ tip 100)))) 100)) + (defn sub-total [total tip] (/ (js/Math.round (* 100 (+ (calc-tip total tip) total))) 100)) @@ -74,20 +82,24 @@ (defn main [] [:div {:class "main-wrapper"} - [:header - [:div - [:img {:class "cfd-logo" :src "images/cfd.jpg"}]] - [:img {:src "images/pizza.png"}] - [:h1 "Code for Pizzayyyyyy"] - [:img {:src "images/pizza.png"}]] - + [:header [:div - [:p "How many people are you feeding?"] - [number-input]] + [:img {:class "cfd-logo" :src "images/cfd.jpg"}]] + [:img {:src "images/pizza.png"}] + [:h1 "Code for Pizzayyyyyy"] + [:img {:src "images/pizza.png"}]] - [:div - [:p "What size of pizza would you like?"] - [size-selection]] + [:div + [:p "How many people are you feeding?"] + [number-input]] + + [:div + [:p "What size of pizza would you like?"] + [size-selection]] + + [:div + [:p "How much does each pie cost?"] + [price-per-pie-input]] [:div {:class "result-statement"} "If you have " [:span {:class "number"} @number " people" ] " to feed," "\n" @@ -95,8 +107,7 @@ [total-display @number @selection] [:div - [:p "Total $"] - [total-charge-input]] + [:p "Total $" @total-charge]] [:div [:p "Tip in %"] From a6c51d693b4a46e970400258c6ee7887d40511fd Mon Sep 17 00:00:00 2001 From: jason poage Date: Mon, 14 Jan 2019 22:13:50 -0700 Subject: [PATCH 2/3] modified: src/cljs/nightcoders/pizza.cljs --- src/cljs/nightcoders/pizza.cljs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cljs/nightcoders/pizza.cljs b/src/cljs/nightcoders/pizza.cljs index bd3f395..1fbfe31 100755 --- a/src/cljs/nightcoders/pizza.cljs +++ b/src/cljs/nightcoders/pizza.cljs @@ -47,6 +47,11 @@ (reset! tip-percent 0) (reset! tip-percent num))))}]) +(defn total-charge-display [pizzas num] + (let [total-calc (* @price-per-pie num)] + (reset! total-charge total-calc) + [:span (str @total-charge)])) + (defn price-per-pie-input [] [:input {:label "price-per-pie" :type "number" @@ -107,7 +112,7 @@ [total-display @number @selection] [:div - [:p "Total $" @total-charge]] + [:p "Total $" [total-charge-display @price-per-pie @total]]] [:div [:p "Tip in %"] From 7705f7b9484a338fe91a1f7eaa8784e593e09361 Mon Sep 17 00:00:00 2001 From: jason poage Date: Mon, 14 Jan 2019 22:33:11 -0700 Subject: [PATCH 3/3] modified: src/cljs/nightcoders/pizza.cljs --- src/cljs/nightcoders/pizza.cljs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/cljs/nightcoders/pizza.cljs b/src/cljs/nightcoders/pizza.cljs index 1fbfe31..83557fa 100755 --- a/src/cljs/nightcoders/pizza.cljs +++ b/src/cljs/nightcoders/pizza.cljs @@ -9,7 +9,7 @@ (def default-price-per-pie 10) -(def price-per-pie (r/atom 0)) +(def price-per-pie (r/atom default-price-per-pie)) (def sizes {:SMALL 0.5 :MEDIUM 0.4 @@ -55,14 +55,15 @@ (defn price-per-pie-input [] [:input {:label "price-per-pie" :type "number" + :default-value default-price-per-pie :on-change (fn [e] (let [num (js/parseFloat (-> e .-target .-value)) new-total (* @total num)] - (if (js/isNaN num) - (reset! price-per-pie @default-price-per-pie) - (reset! price-per-pie num)) - (reset! total-charge new-total)))}]) + (if (js/isNaN num) + (reset! price-per-pie @default-price-per-pie) + (reset! price-per-pie num)) + (reset! total-charge new-total)))}]) (defn calc-tip [total tip] (/ (js/Math.round (* 100 (* total (/ tip 100))))