2024-01-11 20:07:47 -08:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2024-02-10 10:52:09 -08:00
|
|
|
"nickiel.net/recount_server/db"
|
|
|
|
"nickiel.net/recount_server/web/templates"
|
2024-02-10 08:51:27 -08:00
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
"html/template"
|
2024-01-11 20:07:47 -08:00
|
|
|
"net/http"
|
2024-01-20 21:51:10 -08:00
|
|
|
"strconv"
|
2024-01-11 20:07:47 -08:00
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-01-12 15:48:31 -08:00
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
"github.com/a-h/templ"
|
2024-01-11 20:07:47 -08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2024-01-12 15:48:31 -08:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-01-11 20:07:47 -08:00
|
|
|
)
|
|
|
|
|
2024-02-10 08:51:27 -08:00
|
|
|
type IndexTemplateModel struct {
|
2024-02-27 18:38:00 -08:00
|
|
|
InnerHtml template.HTML
|
|
|
|
ActivePage string
|
|
|
|
NewTransactionPane template.HTML
|
|
|
|
QuickAccessPane template.HTML
|
2024-01-12 16:34:26 -08:00
|
|
|
}
|
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
const TemplateDir = "./web/templates/"
|
|
|
|
|
2024-01-12 15:48:31 -08:00
|
|
|
func SetLogLevel(level zerolog.Level) {
|
|
|
|
zerolog.SetGlobalLevel(level)
|
|
|
|
}
|
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
func WebRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
2024-01-20 15:05:06 -08:00
|
|
|
r.Get("/", getDashboard)
|
2024-02-27 18:38:00 -08:00
|
|
|
r.Get("/transactions", getTransactionsPage)
|
2024-03-03 20:21:25 -08:00
|
|
|
r.Post("/submitTransaction", HandleNewTransaction);
|
2024-01-24 18:58:29 -08:00
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
r.Get("/components/account_summaries", GetAccountSummaries)
|
2024-01-24 18:58:29 -08:00
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
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)
|
2024-01-24 18:58:29 -08:00
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static/"))))
|
2024-01-11 20:07:47 -08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
//for name, values := range req.Header {
|
2024-02-27 18:38:00 -08:00
|
|
|
// Loop over all values for the name.
|
2024-01-13 18:33:26 -08:00
|
|
|
// for _, value := range values {
|
|
|
|
// log.Debug().Msg(name + " " + value);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
func renderFullPage(w http.ResponseWriter, c templ.Component, pageName string) {
|
2024-02-27 18:38:00 -08:00
|
|
|
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")
|
|
|
|
}
|
2024-01-13 18:33:26 -08:00
|
|
|
}
|
2024-01-12 16:34:26 -08:00
|
|
|
|
2024-01-20 15:05:06 -08:00
|
|
|
func getDashboard(w http.ResponseWriter, req *http.Request) {
|
2024-02-27 18:38:00 -08:00
|
|
|
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")
|
|
|
|
}
|
2024-01-12 16:34:26 -08:00
|
|
|
}
|
|
|
|
|
2024-01-20 15:05:06 -08:00
|
|
|
func getTransactionsPage(w http.ResponseWriter, req *http.Request) {
|
2024-02-27 18:38:00 -08:00
|
|
|
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")
|
|
|
|
}
|
2024-01-20 15:05:06 -08:00
|
|
|
}
|