Recount-Server/web/router.go
2024-03-03 20:21:25 -08:00

143 lines
3.9 KiB
Go

package web
import (
"nickiel.net/recount_server/db"
"nickiel.net/recount_server/web/templates"
"html/template"
"net/http"
"strconv"
"bytes"
"context"
"github.com/a-h/templ"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type IndexTemplateModel struct {
InnerHtml template.HTML
ActivePage string
NewTransactionPane template.HTML
QuickAccessPane template.HTML
}
const TemplateDir = "./web/templates/"
func SetLogLevel(level zerolog.Level) {
zerolog.SetGlobalLevel(level)
}
func WebRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getDashboard)
r.Get("/transactions", getTransactionsPage)
r.Post("/submitTransaction", HandleNewTransaction);
r.Get("/components/account_summaries", GetAccountSummaries)
r.Get("/components/data/transaction_table_rows", GetTransactionsRows)
r.Get("/components/data/expenditure_chart", GetExpenditureChart)
r.Get("/components/data/account_summary/{accountID}", GetAccountSummaryChart)
r.Get("/components/data/transaction_quick_access", GetTransactionQuickAccessEntries)
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static/"))))
return r
}
//for name, values := range req.Header {
// Loop over all values for the name.
// for _, value := range values {
// log.Debug().Msg(name + " " + value);
// }
// }
func renderFullPage(w http.ResponseWriter, c templ.Component, pageName string) {
var main_component bytes.Buffer
// Render the provided templ component
err := c.Render(context.Background(), &main_component)
if err != nil {
log.Fatal().Err(err).Msg("Fatal error reading hello template")
}
// get the index template
index, err := template.ParseFiles(TemplateDir + "index.html")
if err != nil {
log.Fatal().
Err(err).
Msg("Fatal error reading index template")
}
var new_tp bytes.Buffer
quick_trans_types, err := db.GetTransPaneEntries(0)
if err != nil {
log.Fatal().Err(err).Msg("Could not get transaction pane entries for user")
}
user_acnts, err := db.GetUserAccounts(0)
if err != nil {
log.Fatal().Err(err).Msg("Could not get accounts for user")
}
user_bkts, err := db.GetUserBuckets(0)
if err != nil {
log.Fatal().Err(err).Msg("Could not get buckets for user")
}
err = templates.NewTransactionPane(&quick_trans_types, &user_acnts, &user_bkts).Render(context.Background(), &new_tp)
if err != nil {
log.Fatal().Err(err).Msg("Could not render new transaction pane for index")
}
var quick_ap bytes.Buffer
err = templates.QuickAccessPane().Render(context.Background(), &quick_ap)
if err != nil {
log.Fatal().Err(err).Msg("Could not render quick access pane for index")
}
// Inject the templ component html into the index template
err = index.Execute(w,
IndexTemplateModel{
InnerHtml: template.HTML(main_component.String()),
ActivePage: pageName,
NewTransactionPane: template.HTML(new_tp.String()),
QuickAccessPane: template.HTML(quick_ap.String()),
})
if err != nil {
log.Fatal().Err(err).Msg("Fatal error reading hello template")
}
}
func getDashboard(w http.ResponseWriter, req *http.Request) {
component := templates.Dashboard()
_, ok := req.Header["Hx-Request"]
if ok {
err := component.Render(context.Background(), w)
if err != nil {
log.Fatal().Err(err).Msg("Couldn't render dashboard templ template")
}
} else {
renderFullPage(w, component, "Dashboard")
}
}
func getTransactionsPage(w http.ResponseWriter, req *http.Request) {
accounts := make([]string, 10)
for i := 0; i < 10; i++ {
accounts[i] = strconv.Itoa(i) + " Account"
}
component := templates.TransactionsPage(1, &accounts)
_, ok := req.Header["Hx-Request"]
if ok {
err := component.Render(context.Background(), w)
if err != nil {
log.Fatal().Err(err).Msg("Couldn't render transactions templ template")
}
} else {
renderFullPage(w, component, "Transactions")
}
}