summaryrefslogtreecommitdiff
path: root/view/timer.templ
blob: 45194c5110e04263d6e731fe3783051f646548cb (plain)
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
package view

import (
	"fmt"
	"stevenlr.com/timer/model"
)

templ TimerTokenForm(timer model.Timer) {
	<p class="token-form">
		<button
			type="button"
			hx-post={ fmt.Sprint("/timer/", timer.Id, "/resetToken") }
			hx-target="closest .token-form"
			hx-confirm="Are you sure you want to reset the token for this timer?"
		>Reset token</button>
		<button
			type="button"
			hx-get={ fmt.Sprint("/timer/", timer.Id, "/token") }
			hx-swap="outerHTML"
		>Show token</button>
	</p>
}

func renderTimeString(value int, unit string) string {
	s := ""
	if value > 1 { s = "s" }
	return fmt.Sprint(value, " ", unit, s)
}

templ timeButton(id model.UUID, value int, unit string) {
	<button
		hx-target=".timer-info"
		hx-post={ fmt.Sprint("/timer/", id, "/addTime") }
		hx-include="next input"
	>{ renderTimeString(value, unit) }</button>
	<input type="hidden" name="timeToAdd" value={ fmt.Sprint(value, "", unit[:1]) } />
}

templ TimerInfo(timer model.Timer) {
	<h1>Timer "{ timer.Name }"</h1>
	<p><a href="/">Back to list</a></p>
	<p>Start time: <local-date>{ timer.StartTime.AsUTCString() }</local-date></p>
	<p>End time: <local-date>{ timer.EndTime.AsUTCString() }</local-date></p>
	<p>
		Total time:
		<timer-countdown
			start={ timer.StartTime.AsUTCString() }
			end={ timer.EndTime.AsUTCString() }
		></timer-countdown>
	</p>
	<p>
		Remaining time:
		<timer-countdown
			end={ timer.EndTime.AsUTCString() }
		></timer-countdown>
	</p>
}

templ TimerView(timer model.Timer) {
	<div class="timer-info">
		@TimerInfo(timer)
	</div>
	if !timer.IsFinished() {
		<h3>Add time</h3>
		<p>
			@timeButton(timer.Id, 15, "minute")
			@timeButton(timer.Id, 30, "minute")
			@timeButton(timer.Id, 1, "hour")
			@timeButton(timer.Id, 2, "hour")
			@timeButton(timer.Id, 6, "hour")
			@timeButton(timer.Id, 12, "hour")
			@timeButton(timer.Id, 1, "day")
			@timeButton(timer.Id, 1, "week")
			@timeButton(timer.Id, 4, "week")
		</p>
	}
	<h3>API token</h3>
	@TimerTokenForm(timer)
}