feat(webui): implement state versions list

This commit is contained in:
Julien Dessaux 2025-01-27 22:00:51 +01:00
parent 80958d5b0f
commit 26e10a9399
Signed by: adyxax
GPG key ID: F92E51B86E07177E
12 changed files with 195 additions and 10 deletions

View file

@ -12,11 +12,10 @@
<span>Login</span>
</a>
{{ else }}
<a href="/states"{{ if eq .Page.Section "states" }} class="active"{{ end}}>
<a href="/states"{{ if eq .Page.Section "states" }} class="fill"{{ end}}>
<i>home_storage</i>
<span>States</span>
</a>
<hr>
<a href="/logout">
<i>logout</i>
<span>Logout</span>
@ -39,6 +38,11 @@
<nav class="bottom s">{{ template "nav" . }}</nav>
<header>
<nav>
{{ if ne .Page.Precedent "" }}
<a href="{{ .Page.Precedent }}" class="button circle chip">
<i>arrow_back</i>
</a>
{{ end }}
<h5 class="max center-align">{{ .Page.Title }}</h5>
</nav>
</header>

30
pkg/webui/html/state.html Normal file
View file

@ -0,0 +1,30 @@
{{ define "main" }}
<main class="responsive" id="main">
<p>
Locked:
{{ if eq .State.Lock nil }}no{{ else }}
<span>yes</span>
<div class="tooltip left max">
<b>Lock</b>
<p>{{ .State.Lock }}</p>
</div>
{{ end }}
</p>
<table class="clickable-rows no-space">
<thead>
<tr>
<th>By</th>
<th>Created</th>
</tr>
</thead>
<tbody>
{{ range .Versions }}
<tr>
<td><a href="/version/{{ .Id }}">{{ index $.Usernames .AccountId }}</a></td>
<td><a href="/version/{{ .Id }}">{{ .Created }}</a></td>
</tr>
{{ end }}
</tbody>
</table>
</main>
{{ end }}

View file

@ -1,6 +1,6 @@
{{ define "main" }}
<main class="responsive" id="main">
<table class="clickable-rows no-space stripes">
<table class="clickable-rows no-space">
<thead>
<tr>
<th>Path</th>

View file

@ -6,8 +6,9 @@ import (
)
type Page struct {
Section string
Title string
Precedent string
Section string
Title string
}
func handleIndexGET() http.Handler {

View file

@ -17,6 +17,7 @@ func addRoutes(
mux.Handle("POST /login", requireSession(handleLoginPOST(db)))
mux.Handle("GET /logout", requireLogin(handleLogoutGET(db)))
mux.Handle("GET /states", requireLogin(handleStatesGET(db)))
mux.Handle("GET /state/{id}", requireLogin(handleStateGET(db)))
mux.Handle("GET /static/", cache(http.FileServer(http.FS(staticFS))))
mux.Handle("GET /", requireLogin(handleIndexGET()))
}

54
pkg/webui/state.go Normal file
View file

@ -0,0 +1,54 @@
package webui
import (
"html/template"
"net/http"
"strconv"
"git.adyxax.org/adyxax/tfstated/pkg/database"
"git.adyxax.org/adyxax/tfstated/pkg/model"
)
var stateTemplate = template.Must(template.ParseFS(htmlFS, "html/base.html", "html/state.html"))
func handleStateGET(db *database.DB) http.Handler {
type StatesData struct {
Page
State *model.State
Usernames map[int]string
Versions []model.Version
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
stateIdStr := r.PathValue("id")
stateId, err := strconv.Atoi(stateIdStr)
if err != nil {
errorResponse(w, http.StatusBadRequest, err)
return
}
state, err := db.LoadStateById(stateId)
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return
}
versions, err := db.LoadVersionsByState(state)
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return
}
usernames, err := db.LoadAccountUsernames()
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return
}
render(w, stateTemplate, http.StatusOK, StatesData{
Page: Page{
Precedent: "/states",
Section: "states",
Title: state.Path,
},
State: state,
Usernames: usernames,
Versions: versions,
})
})
}

View file

@ -16,7 +16,7 @@ func handleStatesGET(db *database.DB) http.Handler {
States []model.State
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
states, err := db.LoadStatesByPath()
states, err := db.LoadStates()
if err != nil {
errorResponse(w, http.StatusInternalServerError, err)
return

View file

@ -1,6 +1,5 @@
.clickable-rows tbody a {
table tbody a {
display: block;
padding: 0 1em 0;
text-decoration: none;
transition: all 0.25s ease-out;
}