♻️ Refactored web ui with bootstrap and jQuery

This commit is contained in:
2024-06-02 22:13:41 +08:00
parent 1c36b429ea
commit f1ab0f203f
16 changed files with 543 additions and 1072 deletions

View File

@ -1,12 +1,11 @@
package admin
import (
"git.solsynth.dev/hydrogen/passport/pkg/utils"
"github.com/gofiber/fiber/v2"
)
func MapAdminEndpoints(A *fiber.App, authFunc utils.AuthFunc) {
admin := A.Group("/api/admin").Use(authFunc)
func MapAdminEndpoints(A *fiber.App, authMiddleware fiber.Handler) {
admin := A.Group("/api/admin").Use(authMiddleware)
{
admin.Post("/badges", grantBadge)
admin.Delete("/badges/:badgeId", revokeBadge)

View File

@ -144,7 +144,7 @@ func NewServer() {
URL: "/favicon.png",
}))
admin.MapAdminEndpoints(A, authFunc)
admin.MapAdminEndpoints(A, authMiddleware)
ui.MapUserInterface(A, authFunc)
}

View File

@ -1,51 +0,0 @@
package ui
import (
"fmt"
"html/template"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/database"
"git.solsynth.dev/hydrogen/passport/pkg/models"
"github.com/gofiber/fiber/v2"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/sujit-baniya/flash"
)
func otherUserinfoPage(c *fiber.Ctx) error {
name := c.Params("account")
var data models.Account
if err := database.C.
Where(&models.Account{Name: name}).
Preload("Profile").
Preload("PersonalPage").
Preload("Contacts").
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
birthday := "Unknown"
if data.Profile.Birthday != nil {
birthday = data.Profile.Birthday.Format(time.RFC822)
}
doc := parser.
NewWithExtensions(parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock).
Parse([]byte(data.PersonalPage.Content))
renderer := html.NewRenderer(html.RendererOptions{Flags: html.CommonFlags | html.HrefTargetBlank})
return c.Render("views/users/directory/userinfo", fiber.Map{
"info": flash.Get(c)["message"],
"uid": fmt.Sprintf("%08d", data.ID),
"joined_at": data.CreatedAt.Format(time.RFC822),
"birthday_at": birthday,
"personal_page": template.HTML(markdown.Render(doc, renderer)),
"userinfo": data,
"avatar": data.GetAvatar(),
"banner": data.GetBanner(),
}, "views/layouts/user-center")
}

View File

@ -43,10 +43,5 @@ func MapUserInterface(A *fiber.App, authFunc utils.AuthFunc) {
pages.Post("/mfa/apply", mfaApplyAction)
pages.Post("/authorize", authCheckWare, authorizeAction)
pages.Get("/@:account", otherUserinfoPage)
pages.Get("/users/me", authCheckWare, selfUserinfoPage)
pages.Get("/users/me/personalize", authCheckWare, personalizePage)
pages.Post("/users/me/personalize", authCheckWare, personalizeAction)
}

View File

@ -1,101 +0,0 @@
package ui
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/database"
"git.solsynth.dev/hydrogen/passport/pkg/models"
"git.solsynth.dev/hydrogen/passport/pkg/services"
"git.solsynth.dev/hydrogen/passport/pkg/utils"
"github.com/gofiber/fiber/v2"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/samber/lo"
"github.com/sujit-baniya/flash"
"strings"
"time"
)
func personalizePage(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
localizer := c.Locals("localizer").(*i18n.Localizer)
var data models.Account
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
Preload("Profile").
Preload("PersonalPage").
Preload("Contacts").
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
var birthday any
if data.Profile.Birthday != nil {
birthday = strings.SplitN(data.Profile.Birthday.Format(time.RFC3339), "T", 1)[0]
}
apply, _ := localizer.LocalizeMessage(&i18n.Message{ID: "apply"})
back, _ := localizer.LocalizeMessage(&i18n.Message{ID: "back"})
return c.Render("views/users/personalize", fiber.Map{
"info": flash.Get(c)["message"],
"birthday_at": birthday,
"userinfo": data,
"i18n": fiber.Map{
"apply": apply,
"back": back,
},
}, "views/layouts/user-center")
}
func personalizeAction(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data struct {
Nick string `form:"nick" validate:"required,min=4,max=24"`
Description string `form:"description"`
FirstName string `form:"first_name"`
LastName string `form:"last_name"`
Birthday string `form:"birthday"`
}
if err := utils.BindAndValidate(c, &data); err != nil {
return flash.WithInfo(c, fiber.Map{
"message": err.Error(),
}).Redirect("/users/me/personalize")
}
var account models.Account
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
Preload("Profile").
First(&account).Error; err != nil {
return flash.WithInfo(c, fiber.Map{
"message": fmt.Sprintf("unable to get your userinfo: %v", err),
}).Redirect("/users/me/personalize")
}
account.Nick = data.Nick
account.Description = data.Description
account.Profile.FirstName = data.FirstName
account.Profile.LastName = data.LastName
if birthday, err := time.Parse(time.DateOnly, data.Birthday); err == nil {
account.Profile.Birthday = lo.ToPtr(birthday)
}
if err := database.C.Save(&account).Error; err != nil {
return flash.WithInfo(c, fiber.Map{
"message": fmt.Sprintf("unable to personalize your account: %v", err),
}).Redirect("/users/me/personalize")
} else if err := database.C.Save(&account.Profile).Error; err != nil {
return flash.WithInfo(c, fiber.Map{
"message": fmt.Sprintf("unable to personalize your profile: %v", err),
}).Redirect("/users/me/personalize")
}
services.InvalidAuthCacheWithUser(account.ID)
return flash.WithInfo(c, fiber.Map{
"message": "your account has been personalized",
}).Redirect("/users/me")
}