track-me/index.go

159 lines
4.4 KiB
Go

package main
import (
"go.etcd.io/bbolt"
"html/template"
"log"
"net/http"
"time"
)
const IndexPageTemplate = `<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Add moments of your day</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css"
integrity="sha256-zIG416V1ynj3Wgju/scU80KAEWOsO5rRLfVyRDuOv7Q=" crossorigin="anonymous"/>
</head>
<body>
<div class="section">
<div class="container">
<p>Logged in as: <a href="{{ .Me }}">{{ .Me }}</a> | <a href="/auth/logout">Logout</a></p>
<h1 class="title">Remember moments: <em class="js-counter is-primary">N</em> minutes since your last memo</h1>
<form action="/moment" class="form" method="post">
<div class="field">
<label class="label">What did you do in the last <em class="js-counter"></em> minutes:</label>
<div class="control"><input type="text" name="memo" class="input" autofocus
placeholder="add note / accomplishments"></div>
</div>
<div class="field">
<div class="control">
<button class="button is-primary" type="submit">Save moment</button>
</div>
</div>
</form>
</div>
</div>
{{ if .Moments }}
<div class="section">
<div class="container">
<table class="table">
{{ range .Moments }}
<tr>
<td>{{ (.Time.Format "15:04") }}</td>
<td style="text-align:right">{{ if ne .Diff 0 }}{{ .Diff }}m{{ else }}start{{ end }}</td>
<td> {{ .Memo }}</td>
</tr>
{{ end }}
</table>
</div>
</div>
<script>
var timeout = 20000;
var timeoutID = setTimeout(adjustCounterText, timeout);
var lastTime = {{ .LastMomentSeconds }};
adjustCounterText();
function adjustCounterText() {
var counters = document.querySelectorAll(".js-counter");
var date = new Date();
var minutes = Math.floor((date.getTime() / 1000 - lastTime) / 60);
if (minutes >= 15) {
notifyMe(minutes + " minutes passed since writing the last memory");
}
counters.forEach(function (v) {
v.innerHTML = minutes + "";
})
setTimeout(adjustCounterText, timeout);
}
function notifyMe(text) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
return;
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification(text);
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification(text);
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}
</script>
{{ end }}
</body>
</html>
`
type indexHandler struct {
DB *bbolt.DB
}
func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sess, err := NewSession(w, r)
if err != nil {
log.Printf("Error loading session: %s", err)
return
}
defer sess.Flush()
if !sess.LoggedIn {
http.Redirect(w, r, "/auth/", 302)
return
}
moments, err := loadMoments(h.DB, time.Now().Format("2006-01-02"))
if err != nil {
log.Println(err)
}
type indexPageInfo struct {
Moments []Moment
LastMomentSeconds int64
Me string
}
indexPage := indexPageInfo{Moments: moments}
indexPage.Me = sess.Me
if len(moments) > 0 {
Reverse(moments)
lastMoment := moments[0]
indexPage.LastMomentSeconds = lastMoment.Time.Unix()
}
t, err := template.New("index").Parse(IndexPageTemplate)
if err != nil {
log.Println(err)
return
}
err = t.Execute(w, indexPage)
if err != nil {
log.Println(err)
return
}
}