feat: set timezone user level
This commit is contained in:
parent
ad0989510a
commit
945069052f
11 changed files with 261 additions and 18 deletions
|
|
@ -13,17 +13,27 @@ import (
|
|||
)
|
||||
|
||||
type settingsHandler struct {
|
||||
authService *service.AuthService
|
||||
userService *service.UserService
|
||||
authService *service.AuthService
|
||||
userService *service.UserService
|
||||
profileService *service.ProfileService
|
||||
}
|
||||
|
||||
func NewSettingsHandler(authService *service.AuthService, userService *service.UserService) *settingsHandler {
|
||||
func NewSettingsHandler(authService *service.AuthService, userService *service.UserService, profileService *service.ProfileService) *settingsHandler {
|
||||
return &settingsHandler{
|
||||
authService: authService,
|
||||
userService: userService,
|
||||
authService: authService,
|
||||
userService: userService,
|
||||
profileService: profileService,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *settingsHandler) currentTimezone(r *http.Request) string {
|
||||
profile := ctxkeys.Profile(r.Context())
|
||||
if profile != nil && profile.Timezone != nil {
|
||||
return *profile.Timezone
|
||||
}
|
||||
return "UTC"
|
||||
}
|
||||
|
||||
func (h *settingsHandler) SettingsPage(w http.ResponseWriter, r *http.Request) {
|
||||
user := ctxkeys.User(r.Context())
|
||||
|
||||
|
|
@ -35,7 +45,7 @@ func (h *settingsHandler) SettingsPage(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
ui.Render(w, r, pages.AppSettings(fullUser.HasPassword(), ""))
|
||||
ui.Render(w, r, pages.AppSettings(fullUser.HasPassword(), "", h.currentTimezone(r)))
|
||||
}
|
||||
|
||||
func (h *settingsHandler) SetPassword(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -53,6 +63,8 @@ func (h *settingsHandler) SetPassword(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
currentTz := h.currentTimezone(r)
|
||||
|
||||
err = h.authService.SetPassword(user.ID, currentPassword, newPassword, confirmPassword)
|
||||
if err != nil {
|
||||
slog.Warn("set password failed", "error", err, "user_id", user.ID)
|
||||
|
|
@ -66,12 +78,12 @@ func (h *settingsHandler) SetPassword(w http.ResponseWriter, r *http.Request) {
|
|||
msg = "Password must be at least 12 characters"
|
||||
}
|
||||
|
||||
ui.Render(w, r, pages.AppSettings(fullUser.HasPassword(), msg))
|
||||
ui.Render(w, r, pages.AppSettings(fullUser.HasPassword(), msg, currentTz))
|
||||
return
|
||||
}
|
||||
|
||||
// Password set successfully — render page with success toast
|
||||
ui.Render(w, r, pages.AppSettings(true, ""))
|
||||
ui.Render(w, r, pages.AppSettings(true, "", currentTz))
|
||||
ui.RenderToast(w, r, toast.Toast(toast.Props{
|
||||
Title: "Password updated",
|
||||
Variant: toast.VariantSuccess,
|
||||
|
|
@ -80,3 +92,37 @@ func (h *settingsHandler) SetPassword(w http.ResponseWriter, r *http.Request) {
|
|||
Duration: 5000,
|
||||
}))
|
||||
}
|
||||
|
||||
func (h *settingsHandler) SetTimezone(w http.ResponseWriter, r *http.Request) {
|
||||
user := ctxkeys.User(r.Context())
|
||||
tz := r.FormValue("timezone")
|
||||
|
||||
fullUser, err := h.userService.ByID(user.ID)
|
||||
if err != nil {
|
||||
slog.Error("failed to fetch user for set timezone", "error", err, "user_id", user.ID)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.profileService.UpdateTimezone(user.ID, tz)
|
||||
if err != nil {
|
||||
slog.Warn("set timezone failed", "error", err, "user_id", user.ID)
|
||||
|
||||
msg := "Invalid timezone selected"
|
||||
if !errors.Is(err, service.ErrInvalidTimezone) {
|
||||
msg = "An error occurred. Please try again."
|
||||
}
|
||||
|
||||
ui.Render(w, r, pages.AppSettings(fullUser.HasPassword(), msg, h.currentTimezone(r)))
|
||||
return
|
||||
}
|
||||
|
||||
ui.Render(w, r, pages.AppSettings(fullUser.HasPassword(), "", tz))
|
||||
ui.RenderToast(w, r, toast.Toast(toast.Props{
|
||||
Title: "Timezone updated",
|
||||
Variant: toast.VariantSuccess,
|
||||
Icon: true,
|
||||
Dismissible: true,
|
||||
Duration: 5000,
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue