Rename everything to locker/lock

This commit is contained in:
2024-04-27 23:55:31 +02:00
parent df3068728a
commit a55cfe8205
17 changed files with 335 additions and 328 deletions

80
view/lock.templ Normal file
View File

@ -0,0 +1,80 @@
package view
import (
"fmt"
"stevenlr.com/locker/model"
)
templ LockTokenForm(lock model.Lock) {
<p class="token-form">
<button
type="button"
hx-post={ fmt.Sprint("/lock/", lock.Id, "/resetToken") }
hx-target="closest .token-form"
hx-confirm="Are you sure you want to reset the token for this lock?"
>Reset token</button>
<button
type="button"
hx-get={ fmt.Sprint("/lock/", lock.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=".lock-info"
hx-post={ fmt.Sprint("/lock/", id, "/addTime") }
hx-include="next input"
>{ renderTimeString(value, unit) }</button>
<input type="hidden" name="timeToAdd" value={ fmt.Sprint(value, "", unit[:1]) } />
}
templ LockInfo(lock model.Lock) {
<h1>Lock "{ lock.Name }"</h1>
<p>Start time: <local-date>{ lock.StartTime.AsUTCString() }</local-date></p>
<p>End time: <local-date>{ lock.EndTime.AsUTCString() }</local-date></p>
<p>
Total time:
<lock-countdown
start={ lock.StartTime.AsUTCString() }
end={ lock.EndTime.AsUTCString() }
></lock-countdown>
</p>
<p>
Remaining time:
<lock-countdown
end={ lock.EndTime.AsUTCString() }
></lock-countdown>
</p>
}
templ LockView(lock model.Lock) {
<p><a href="/">Back to list</a></p>
<div class="lock-info">
@LockInfo(lock)
</div>
if !lock.IsFinished() {
<h3>Add time</h3>
<p>
@timeButton(lock.Id, 15, "minute")
@timeButton(lock.Id, 30, "minute")
@timeButton(lock.Id, 1, "hour")
@timeButton(lock.Id, 2, "hour")
@timeButton(lock.Id, 6, "hour")
@timeButton(lock.Id, 12, "hour")
@timeButton(lock.Id, 1, "day")
@timeButton(lock.Id, 1, "week")
@timeButton(lock.Id, 4, "week")
</p>
}
<h3>API token</h3>
@LockTokenForm(lock)
}

51
view/locks_list.templ Normal file
View File

@ -0,0 +1,51 @@
package view
import (
"stevenlr.com/locker/model"
"fmt"
)
templ lock(t model.Lock) {
<p class="lock-row">
<a href={ templ.URL(fmt.Sprint("/lock/", t.Id)) }>{ t.Name }</a>
-
<a
href="javascript:void(0);"
hx-delete={ fmt.Sprint("/lock/", t.Id) }
hx-target="closest .lock-row"
hx-confirm={ fmt.Sprint("Are you sure you want to delete lock \"", t.Name , "\"?") }
>Delete</a>
</p>
}
templ LockCreateForm(lockName string, err string) {
<form
hx-put="/lock"
hx-target="closest .locks-list"
hx-target-error="this"
>
<p>
<input type="text" name="lockName" value={ lockName } placeholder="Name" />
<input type="number" name="days" placeholder="Days" />
<input type="number" name="hours" placeholder="Hours" />
<button type="submit">Create</button>
</p>
if err != "" {
<p class="error">{ err }</p>
}
</form>
}
templ LocksList(locks []model.Lock, isSignedIn bool) {
<div class="locks-list">
if isSignedIn {
<h1>Locks</h1>
for _, t := range locks {
@lock(t)
}
<h4>Create lock</h4>
@LockCreateForm("", "")
}
</div>
}

View File

@ -1,7 +1,7 @@
package view
import (
"stevenlr.com/timer/model"
"stevenlr.com/locker/model"
)
templ LoginFormError(currentUser *model.User, err string) {

View File

@ -1,14 +1,14 @@
package view
import (
"stevenlr.com/timer/model"
"stevenlr.com/locker/model"
)
templ Main(contents templ.Component, currentUser *model.User) {
<!DOCTYPE html>
<html>
<head>
<title>Cool timer app</title>
<title>Cool locker app</title>
<link rel="stylesheet" href="/static/style.css" />
<script type="module" src="/static/ui-components.js"></script>
<script src="/static/htmx.min.js"></script>

View File

@ -1,80 +0,0 @@
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>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) {
<p><a href="/">Back to list</a></p>
<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)
}

View File

@ -1,51 +0,0 @@
package view
import (
"stevenlr.com/timer/model"
"fmt"
)
templ timer(t model.Timer) {
<p class="timer-row">
<a href={ templ.URL(fmt.Sprint("/timer/", t.Id)) }>{ t.Name }</a>
-
<a
href="javascript:void(0);"
hx-delete={ fmt.Sprint("/timer/", t.Id) }
hx-target="closest .timer-row"
hx-confirm={ fmt.Sprint("Are you sure you want to delete timer \"", t.Name , "\"?") }
>Delete</a>
</p>
}
templ TimerCreateForm(timerName string, err string) {
<form
hx-put="/timer"
hx-target="closest .timers-list"
hx-target-error="this"
>
<p>
<input type="text" name="timerName" value={ timerName } placeholder="Name" />
<input type="number" name="days" placeholder="Days" style="width: 5em;" />
<input type="number" name="hours" placeholder="Hours" style="width: 5em;" />
<button type="submit">Create</button>
</p>
if err != "" {
<p class="error">{ err }</p>
}
</form>
}
templ TimersList(timers []model.Timer, isSignedIn bool) {
<div class="timers-list">
if isSignedIn {
<h1>Timers</h1>
for _, t := range timers {
@timer(t)
}
<h4>Create timer</h4>
@TimerCreateForm("", "")
}
</div>
}