Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .nightlight.edn
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{:auto-save? true, :theme :light, :selection "/Users/davidviramontes/local/cfd/code4pizza/src/cljs/nightcoders/pizza.cljs"}
{:auto-save? true, :theme :light, :selection "*REPL*"}
65 changes: 41 additions & 24 deletions src/cljs/nightcoders/pizza.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

(def number (r/atom 0))

(def default-price-per-pie 10)

(def price-per-pie (r/atom default-price-per-pie))

(def sizes {:SMALL 0.5
:MEDIUM 0.4
:LARGE 0.3
Expand All @@ -33,29 +37,39 @@
(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 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"
: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)))}])

(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))
Expand All @@ -74,29 +88,32 @@

(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"
"then you need " [:span {:class "number"} @total " " @selection " pizzas!"]]
[total-display @number @selection]

[:div
[:p "Total $"]
[total-charge-input]]
[:p "Total $" [total-charge-display @price-per-pie @total]]]

[:div
[:p "Tip in %"]
Expand Down