Skip to content

Commit

Permalink
simplify ui code
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Mar 26, 2024
1 parent ef2340b commit a1b2f05
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 109 deletions.
13 changes: 0 additions & 13 deletions app/webapi/assets/components/check_results.html

This file was deleted.

46 changes: 0 additions & 46 deletions app/webapi/assets/components/samples_list.html

This file was deleted.

37 changes: 0 additions & 37 deletions app/webapi/assets/components/users_list.html

This file was deleted.

54 changes: 51 additions & 3 deletions app/webapi/assets/manage_samples.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,58 @@ <h2>Manage Samples</h2>
</div>

<!-- Include the samples list template -->
{{template "samples_list.html" .}}
{{template "samples_list" .}}
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>

</body>
</html>


<!-- list of samples, both spam and ham -->
{{define "samples_list"}}
<div class="row" id="samples-list">
<div class="col-md-6">
<h4>
Spam Samples ({{.TotalSpamSamples}})
<a href="/download/spam" class="btn btn-custom-blue btn-sm">Download</a>
</h4>
<ul class="list-group" id="spam-samples-list">
{{range .SpamSamples}}
<li class="list-group-item d-flex justify-content-between align-items-center">
<span id="loading-{{.ID}}">{{.Sample}} <img class="htmx-indicator" src="/spinner.svg"/></span>
<form method="POST" hx-post="/delete/spam" hx-target="#samples-list" hx-swap="outerHTML" hx-indicator="#loading-{{.ID}}">
<input type="hidden" name="msg" value="{{.Sample}}">
<button type="submit" class="btn btn-sm btn-danger">
<i class="bi bi-trash"></i>
</button>
</form>
</li>
{{else}}
<li class="list-group-item">No spam samples found</li>
{{end}}
</ul>
</div>

<div class="col-md-6">
<h4>
Ham Samples ({{.TotalHamSamples}})
<a href="/download/ham" class="btn btn-custom-blue btn-sm">Download</a>
</h4>
<ul class="list-group" id="ham-samples-list">
{{range .HamSamples}}
<li class="list-group-item d-flex justify-content-between align-items-center">
{{.Sample}}
<form method="POST" hx-post="/delete/ham" hx-target="#samples-list" hx-swap="outerHTML">
<input type="hidden" name="msg" value="{{.Sample}}">
<button type="submit" class="btn btn-sm btn-danger">
<i class="bi bi-trash"></i>
</button>
</form>
</li>
{{else}}
<li class="list-group-item">No ham samples found</li>
{{end}}
</ul>
</div>
</div>
{{end}}
47 changes: 44 additions & 3 deletions app/webapi/assets/manage_users.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,51 @@ <h2>Manage Approved Users</h2>
</div>
</div>

<!-- Include the Approved Users List component -->
{{template "users_list.html" .}}

{{template "users_list" .}}
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</body>
</html>


<!-- list of users -->
{{define "users_list"}}
<div class="row" id="users-list">
<div class="col-md-12">
<div id="error-message"></div> <!-- Error message container -->
<h4>Approved Users ({{.TotalApprovedUsers}})</h4>
<table class="table table-striped">
<thead class="custom-table-header">
<tr>
<th>ID</th>
<th>Name</th>
<th>Timestamp</th>
<th></th> <!-- Header for the action column -->
</tr>
</thead>
<tbody>
{{range .ApprovedUsers}}
<tr>
<td>{{.UserID}}</td>
<td>{{.UserName}}</td>
<td>{{.Timestamp.Format "2006-01-02 15:04:05"}}</td>
<td class="text-end">
<form method="POST" hx-post="/users/delete" hx-target="#users-list" hx-swap="outerHTML" class="d-inline">
<input type="hidden" name="user_id" value="{{.UserID}}">
<button type="submit" class="btn btn-danger btn-sm" title="Delete User">
<i class="bi bi-trash"></i>
</button>
</form>
</td>
</tr>
{{else}}
<tr>
<td colspan="4">No approved users found</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
{{end}}
18 changes: 16 additions & 2 deletions app/webapi/assets/spam_check.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,21 @@ <h2 class="text-center mb-4">Message Checker</h2>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>

</body>
</html>

{{define "check_results"}}
<div id="result">
<div id="error-message"></div> <!-- Error message container -->
<div class="alert alert-light" role="alert">
<div class="alert {{if .Spam}}alert-danger{{else}}alert-success{{end}}">
<strong>Result:</strong> {{if .Spam}}Spam detected{{else}}No spam detected{{end}}
</div>
{{range .Checks}}
<div class="mb-2 {{if .Spam}}text-danger{{else}}text-success{{end}}">
<strong>{{.Name}}:</strong> {{.Details}}
</div>
{{end}}
</div>
</div>
{{end}}
9 changes: 4 additions & 5 deletions app/webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (s *Server) checkHandler(w http.ResponseWriter, r *http.Request) {
Checks: cr,
}

if err := tmpl.ExecuteTemplate(w, "check_results.html", resultDisplay); err != nil {
if err := tmpl.ExecuteTemplate(w, "check_results", resultDisplay); err != nil {
log.Printf("[WARN] can't execute result template: %v", err)
http.Error(w, "Error rendering result", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -323,7 +323,7 @@ func (s *Server) updateSampleHandler(updFn func(msg string) error) func(w http.R
}

if isHtmxRequest {
s.renderSamples(w, "samples_list.html")
s.renderSamples(w, "samples_list")
} else {
rest.RenderJSON(w, rest.JSON{"updated": true, "msg": req.Msg})
}
Expand Down Expand Up @@ -355,7 +355,7 @@ func (s *Server) deleteSampleHandler(delFn func(msg string) (int, error)) func(w
}

if isHtmxRequest {
s.renderSamples(w, "samples_list.html")
s.renderSamples(w, "samples_list")
} else {
rest.RenderJSON(w, rest.JSON{"deleted": true, "msg": req.Msg, "count": count})
}
Expand Down Expand Up @@ -421,8 +421,7 @@ func (s *Server) updateApprovedUsersHandler(updFn func(ui approved.UserInfo) err
TotalApprovedUsers: len(users),
}

if err := tmpl.ExecuteTemplate(w, "users_list.html", tmplData); err != nil {
log.Printf("[WARN] can't execute template: %v", err)
if err := tmpl.ExecuteTemplate(w, "users_list", tmplData); err != nil {
http.Error(w, "Error executing template", http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit a1b2f05

Please sign in to comment.