feat: login form or room info on index page

This commit is contained in:
efim
2023-11-18 12:18:56 +00:00
parent 0e804b21e5
commit c8f28bf0de
8 changed files with 113 additions and 93 deletions

View File

@@ -11,47 +11,62 @@ import (
"sunshine.industries/some-automoderation/sessions"
)
func renderIndexPage(session sessions.SessionData, w http.ResponseWriter) {
var baseFile = "templates/base.gohtml"
var templFile = "templates/index.gohtml"
type MainData struct {
SessionStringToken string
SomeString string
SessionToken sessions.SessionData
}
data := pageData{
Base: baseData{
Title: "hello base template title",
},
Header: headerData{
Title: session.RoomId,
},
Content: MainData{
fmt.Sprintf("%+v", session),
"hello!",
session,
},
}
tmpl := template.Must(template.ParseFS(templateFs, templFile, baseFile))
err := tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
type indexContent struct {
AuthedData authedData
LoginSectionData LoginSectionData
}
type authedData struct {
SessionStringToken string
SomeString string
Session sessions.SessionData
}
func (ad authedData) IsZero() bool {
return ad == authedData{}
}
// index page shows info on logged in room OR login room form on the left
// and general automoderation rules on the right
func indexPageRoute(
templateFs *embed.FS,
sessionSM sessions.SessionManagement,
roomsM rooms.RoomManager,
) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, found := getContextSession(r.Context())
if !found {
log.Printf("/ session not found, should be impossible")
session, err := getRequestSession(r, sessionSM)
if err != nil {
log.Printf("/ session not found, means should render the login section %s", err)
// TODO return error i guess
}
renderIndexPage(session, w)
baseFile := "templates/base.gohtml"
pageTemplFile := "templates/index.gohtml"
loginSectionFile := "templates/login-section.gohtml"
authData := authedData{}
if err == nil {
authData = authedData{
fmt.Sprintf("%+v", session),
"hello!",
session,
}
}
data := pageData{
Base: baseData{
Title: "hello base template title",
},
Header: headerData{
Title: session.RoomId,
},
Content: indexContent{
AuthedData: authData,
},
}
tmpl := template.Must(template.ParseFS(templateFs, pageTemplFile, baseFile, loginSectionFile))
err = tmpl.ExecuteTemplate(w, "full-page", data)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
})
}