-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.templ
161 lines (145 loc) · 4.85 KB
/
app.templ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"time"
"fmt"
"strings"
)
templ page(body templ.Component) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tracker</title>
<script src="https://unpkg.com/htmx.org@2.0.0/dist/htmx.js" integrity="sha384-Xh+GLLi0SMFPwtHQjT72aPG19QvKB8grnyRbYBNIdHWc2NkCrz65jlU7YrzO6qRp" crossorigin="anonymous"></script>
<link rel="stylesheet" href="/styles.css"/>
</head>
<body id="body">
@body
</body>
</html>
}
templ strComp(str string) {
<span>{ str }</span>
}
templ mainContent(content... templ.Component) {
<main>
<h1>Activity Tracker</h1>
<div class="version">{ version }</div>
for _, c := range content {
@c
}
</main>
}
// ran into issues with v0.2.334, but not in v0.2.731
css effortClass(e DayEntry) {
height: 1em;
background-color: { effortColor(e) };
width: { fmt.Sprintf("%fem", min(10.0, 4.0 * float32(e.Duration) / float32(time.Hour))) };
}
templ entryDisplay(e DayEntry) {
<div>{ e.Description }</div>
<div class={ effortClass(e) } title={ e.Duration.String() }></div>
}
func entryModalCreationVals(d DayLog) string {
return fmt.Sprintf(`{"date": "%s"}`, d.Date.Format(time.DateOnly))
}
func scoreStr(s float64) string {
return fmt.Sprintf("%.0f%%", s)
}
func sumStr(s time.Duration) string {
str := s.String()
if strings.HasSuffix(str, "m0s") {
str = str[:len(str)-2]
}
if strings.HasSuffix(str, "h0m") {
str = str[:len(str)-2]
}
return str
}
func heartRate(f float64) string {
return fmt.Sprintf("%.0f BPM", f)
}
templ summarySection(s Summary) {
<section class="summary">
<span class="combo-score">{ scoreStr(s.ComboScore) }<div>of<br/>goal</div></span>
<span class="combo-score" title={ sumStr(s.RemainingModerateTime/2) + " high remaining" }>{ sumStr(s.RemainingModerateTime) }<div>moderate<br/>remaining</div></span>
<div class="score-breakdown">
<div>
{ scoreStr(s.LowIntensityScore) } / { sumStr(s.LowIntensitySum) } Low Intensity
</div>
<div title={ "> " + heartRate(s.ModerateIntensityHeartRate) }>
{ scoreStr(s.ModerateIntensityScore) } / { sumStr(s.ModerateIntensitySum) } Moderate Intensity
</div>
<div title={ "> " + heartRate(s.HighIntensityHeartRate) }>
{ scoreStr(s.HighIntensityScore) } / { sumStr(s.HighIntensitySum) } High Intensity
</div>
</div>
</section>
}
templ tracker(logs []DayLog, summary Summary) {
<section>
<div class="tracker-container">
for _, d := range logs {
<div id={ dateToID(d.Date.Format(time.DateOnly)) } class="day">
<div style="flex: 1" hx-get="/add-entry-modal" hx-vals={ entryModalCreationVals(d) } hx-target={ "#" + dateToID(d.Date.Format(time.DateOnly)) } hx-swap="beforeend">
<div><strong>{ d.Date.Format("Monday") }</strong></div>
<div><strong>{ d.Date.Format("Jan _2") }</strong></div>
</div>
if len(d.Entries) == 0 {
<div class="nothing">nothing</div>
}
for _, e := range d.Entries {
@entryDisplay(e)
}
</div>
}
</div>
</section>
}
func dateToID(date string) string {
return "date-" + date
}
script closeModal() {
document.getElementById('modal').remove();
}
templ addLogModal(date string) {
<div id="modal">
<div class="modal-underlay" onClick={ closeModal() }></div>
<div class="modal-content">
<form hx-post="/entries" hx-target="#modal" hx-swap="outerHTML">
<h1>Add Entry</h1>
<div class="form-item">
<label for="date">Date:</label>
<input type="date" id="date" value={ date } name="date"/>
</div>
<div class="form-item">
<label for="description">Description:</label>
<input type="text" id="description" name="description" style="width:8.55em"/>
</div>
<div class="form-item">
<label for="duration">Duration:</label>
<input type="text" id="duration" name="duration" style="width:8.55em"/>
</div>
<div class="form-item">
<label for="effort">Effort:</label>
<input type="range" id="effort" name="effort" min="0" max="1.0" step="0.05" style="width:9em"/>
</div>
<div class="form-item">
<button onClick={ closeModal() }>Cancel</button>
<div style="flex:1"></div>
<button type="submit">Submit</button>
</div>
</form>
</div>
</div>
}
templ loginForm() {
<form action="/login" method="POST">
<input name="username" type="text"/>
<input name="password" type="password"/>
<button type="submit">
Login
</button>
</form>
}