Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

#3 added a time input field, still broken if too in the past

  • Loading branch information...
commit 01c6fae5d6f99c97dfc93e18d20be70bb317d6e1 1 parent c413dee
@skuro authored
View
24 resources/templates/index.html.mustache
@@ -2,6 +2,21 @@
<html>
<head>
<script>
+function init(stamp){
+ var timer = document.getElementById('timer');
+ if (timer != null) {
+ initTimer(timer);
+ } else {
+ startTime(stamp);
+ }
+}
+
+function initTimer(elem){
+ var now = new Date();
+ var nowString = now.getHours() + ":" + now.getMinutes();
+ elem.setAttribute("value", nowString);
+}
+
function startTime(stamp){
var elem = document.getElementById('txt');
var due = new Date(stamp);
@@ -61,7 +76,7 @@ function countdown(d){
</script>
</head>
-<body onload="startTime({{danlik.brew}})">
+<body onload="init({{danlik.brew}})">
{{#danlik}}
@@ -71,8 +86,11 @@ function countdown(d){
{{^danlik.brew}}
<h3>The teapot is off</h3>
-<form method="POST" id="start" action="/">
- <input type="submit" value="start" />
+<form method="post" id="started-at" action="/">
+ <p>I started it at:
+ <input type="time" name="stamp" id="timer" />
+ <input type="submit" value="set" />
+ </p>
</form>
{{/danlik.brew}}
View
42 src/chai/danlik.clj
@@ -12,10 +12,12 @@
(defn delayed
"Returns the timestamp (in long) of the moment mins minutes from now"
- [mins]
- (force/to-long (-> mins
- time/minutes
- time/from-now)))
+ ([from mins]
+ (force/to-long (time/plus from (time/minutes mins))))
+ ([mins]
+ (force/to-long (-> mins
+ time/minutes
+ time/from-now))))
(defn now
"Returns the timestamp of now"
@@ -40,16 +42,13 @@
(defn- get-danlik-impl
"In-memory implementation of a chaidanlik read."
[]
- (println "Getting the danlik")
(cleanup (@stock *danlik*)))
(defn create
"In-memory implementation of a chaidanlink create"
[]
(if-let [existing (get-danlik-impl)]
- (do
- (println "Danlik already exists:" existing)
- existing)
+ existing
(swap! stock assoc *danlik* {:time (now)
:label *danlik*})))
@@ -61,7 +60,7 @@
(defn get-danlik
"Delegating implementation of a chaidanlik read"
- []
+ [_]
(get-danlik-impl))
(defn update-danlik
@@ -76,11 +75,26 @@
(defn start
"Starts the chaidanlik. If no parameters are passed, the water heating is started"
([] (start :time))
- ([key]
- (let [start-time (delayed 25)]
- (update-danlik key start-time))))
+ ([key] (start :time (delayed 25)))
+ ([key time] (update-danlik key time)))
+
+(defn parse-time
+ "Accepts a string like '12:12' and returns a date describing today with the provided hours and minutes"
+ [t]
+ (try
+ (let [[hh mm] (map #(Integer/parseInt %) (clojure.string/split t #":"))
+ base (time/now)]
+ (-> base
+ (.withHourOfDay hh)
+ (.withMinuteOfHour mm)))
+ (catch Exception e
+ nil)))
(defn start-brew
"Starts brewing the tea"
- []
- (start :brew))
+ ([]
+ (start :brew))
+ ([[stamp]]
+ (if-let [t (parse-time stamp)]
+ (start :brew (delayed t 25))
+ (start :brew))))
View
6 src/chai/web.clj
@@ -17,8 +17,8 @@
;; TODO: heroku config:add REPL_USER=[...] REPL_PASSWORD=[...]
(= [user pass] [(env :repl-user false) (env :repl-password false)]))
-(defn response [f template]
- (let [context {:danlik (f)}]
+(defn response [f template & args]
+ (let [context {:danlik (f args)}]
{:status 200
:headers {"Content-Type" "text/html"}
:body (stashe/render-resource template context)}))
@@ -32,7 +32,7 @@
(ANY "/repl" {:as req}
(drawbridge req))
(GET "/" [] (response danlik/get-danlik "templates/index.html.mustache"))
- (POST "/" [] (response danlik/start-brew "templates/index.html.mustache"))
+ (POST "/" [stamp] (response danlik/start-brew "templates/index.html.mustache" stamp))
(ANY "*" []
(route/not-found (slurp (io/resource "404.html")))))
Please sign in to comment.
Something went wrong with that request. Please try again.