use sessionStorage & add user admin pages

This commit is contained in:
vinchent 2024-08-23 13:55:21 +02:00
parent 0e7b9d8c20
commit 1971169e2f
9 changed files with 66 additions and 10 deletions

View File

@ -417,3 +417,15 @@ func (app *application) ShowSubscriptions(w http.ResponseWriter, r *http.Request
app.errorLog.Println(err) app.errorLog.Println(err)
} }
} }
func (app *application) AllUsers(w http.ResponseWriter, r *http.Request) {
if err := app.renderTemplate(w, r, "all-users", &templateData{}); err != nil {
app.errorLog.Println(err)
}
}
func (app *application) OneUser(w http.ResponseWriter, r *http.Request) {
if err := app.renderTemplate(w, r, "one-user", &templateData{}); err != nil {
app.errorLog.Println(err)
}
}

View File

@ -19,6 +19,8 @@ func (app *application) routes() http.Handler {
mux.Get("/all-subscriptions", app.AllSubscriptions) mux.Get("/all-subscriptions", app.AllSubscriptions)
mux.Get("/sales/{id}", app.ShowSale) mux.Get("/sales/{id}", app.ShowSale)
mux.Get("/subscriptions/{id}", app.ShowSubscriptions) mux.Get("/subscriptions/{id}", app.ShowSubscriptions)
mux.Get("/all-users", app.AllUsers)
mux.Get("/all-users/{id}", app.OneUser)
}) })
// mux.Post("/virtual-terminal-payment-succeeded", app.VirtualTerminalPaymentSucceeded) // mux.Post("/virtual-terminal-payment-succeeded", app.VirtualTerminalPaymentSucceeded)

View File

@ -29,9 +29,9 @@ All Sales
import {showTable, currentPage} from "/static/js/all-sales.js" import {showTable, currentPage} from "/static/js/all-sales.js"
let curPage = currentPage; let curPage = currentPage;
if (localStorage.getItem("cur-page") !== null) { if (sessionStorage.getItem("cur-page") !== null) {
curPage = localStorage.getItem("cur-page"); curPage = sessionStorage.getItem("cur-page");
localStorage.removeItem("cur-page"); sessionStorage.removeItem("cur-page");
} }
showTable({{.API}}, pageSize, curPage); showTable({{.API}}, pageSize, curPage);
</script> </script>

View File

@ -32,9 +32,9 @@ All Subscriptions
import {showTable, currentPage} from "/static/js/all-subscriptions.js" import {showTable, currentPage} from "/static/js/all-subscriptions.js"
let curPage = currentPage; let curPage = currentPage;
if (localStorage.getItem("cur-page") !== null) { if (sessionStorage.getItem("cur-page") !== null) {
curPage = localStorage.getItem("cur-page"); curPage = sessionStorage.getItem("cur-page");
localStorage.removeItem("cur-page"); sessionStorage.removeItem("cur-page");
} }
showTable({{.API}}, pageSize, curPage); showTable({{.API}}, pageSize, curPage);
</script> </script>

View File

@ -0,0 +1,25 @@
{{ template "base". }}
{{ define "title" }}
All Users
{{ end }}
{{ define "content" }}
<h2 class="mt-5">All Admin users</h2>
<hr>
<div class="float-end">
<a class="btn btn-outline-secondary" href="/admin/all-users/0">Add User</a>
</div>
<div class="clearfix"></div>
<table id="user-table" class="table table-striped">
<thead>
<tr>
<th>User</th>
<th>Email</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{{ end }}
{{ define "js" }}
<script>let tbody = document.getElementById("user-table").getElementByTagName("tbody")[0]</script>
{{ end }}

View File

@ -70,6 +70,12 @@
<li> <li>
<hr class="dropdown-divider"> <hr class="dropdown-divider">
</li> </li>
<li>
<a class="dropdown-item" href="/admin/all-users">All Users</a>
</li>
<li>
<hr class="dropdown-divider">
</li>
<li> <li>
<a class="dropdown-item" href="/logout">Logout</a> <a class="dropdown-item" href="/logout">Logout</a>
</li> </li>

View File

@ -0,0 +1,11 @@
{{ template "base". }}
{{ define "title" }}
Admin User
{{ end }}
{{ define "content" }}
<h2 class="mt-5">Admin user</h2>
<hr>
{{ end }}
{{ define "js" }}
{{ end }}

View File

@ -36,8 +36,8 @@ export function showTable(api, ps, cp) {
newCell.innerHTML = `<a href="#!">Order ${i.id}</a>`; newCell.innerHTML = `<a href="#!">Order ${i.id}</a>`;
newCell.addEventListener("click", function (evt) { newCell.addEventListener("click", function (evt) {
// put the current_page into localStorage // put the current_page into sessionStorage
localStorage.setItem("cur-page", data.current_page); sessionStorage.setItem("cur-page", data.current_page);
// redirect // redirect
location.href = `/admin/sales/${i.id}`; location.href = `/admin/sales/${i.id}`;

View File

@ -34,8 +34,8 @@ export function showTable(api, ps, cp) {
newCell.innerHTML = `<a href="#!">Order ${i.id}</a>`; newCell.innerHTML = `<a href="#!">Order ${i.id}</a>`;
newCell.addEventListener("click", function (evt) { newCell.addEventListener("click", function (evt) {
// put the current_page into localStorage // put the current_page into sessionStorage
localStorage.setItem("cur-page", data.current_page); sessionStorage.setItem("cur-page", data.current_page);
// redirect // redirect
location.href = `/admin/subscriptions/${i.id}`; location.href = `/admin/subscriptions/${i.id}`;