Skip to content

Commit

Permalink
Merge pull request #459 from qor5/profile-ui-fix
Browse files Browse the repository at this point in the history
profile: fix ui && add PrependCompos
  • Loading branch information
molon authored Aug 9, 2024
2 parents 4d3487d + 4354b94 commit 8d749cb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 21 deletions.
24 changes: 24 additions & 0 deletions docs/docsrc/examples/examples_admin/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package examples_admin

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"slices"
Expand Down Expand Up @@ -101,6 +102,29 @@ func TestProfileExample(t *testing.T) {
},
ExpectPageBodyContainsInOrder: []string{"ProfileCompo", "clicked change password", "Change Password", "Logout"},
},
{
Name: "Index Page with PrependCompos",
Debug: true,
HandlerMaker: func() http.Handler {
pb = presets.New()
profileExample(pb, TestDB, currentProfileUser, func(pb *plogin.ProfileBuilder) {
pb.DisableNotification(true).LogoutURL("auth/logout").
PrependCompos(func(ctx context.Context, profileCompo *plogin.ProfileCompo) ([]h.HTMLComponent, error) {
profileCompoFromCtx := plogin.ProfileCompoFromContext(ctx)

return []h.HTMLComponent{
h.Div().Text("PrependCompos"),
h.Div().Text(fmt.Sprintf("ProfileCompoEquals: %t", profileCompo == profileCompoFromCtx)),
}, nil
})
})
return pb
},
ReqFunc: func() *http.Request {
return httptest.NewRequest("GET", "/", nil)
},
ExpectPageBodyContainsInOrder: []string{"ProfileCompo", "PrependCompos", "ProfileCompoEquals: true"},
},
}

for _, c := range cases {
Expand Down
76 changes: 55 additions & 21 deletions login/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type ProfileBuilder struct {
currentProfileFunc func(ctx context.Context) (*Profile, error)
renameCallback func(ctx context.Context, newName string) error
customizeButtons func(ctx context.Context, buttons ...h.HTMLComponent) ([]h.HTMLComponent, error)
prependCompos func(ctx context.Context, profileCompo *ProfileCompo) ([]h.HTMLComponent, error)
}

func NewProfileBuilder(
Expand Down Expand Up @@ -112,6 +113,11 @@ func (b *ProfileBuilder) CustomizeButtons(v func(ctx context.Context, buttons ..
return b
}

func (b *ProfileBuilder) PrependCompos(f func(ctx context.Context, profileCompo *ProfileCompo) ([]h.HTMLComponent, error)) *ProfileBuilder {
b.prependCompos = f
return b
}

func (b *ProfileBuilder) injectorName() string {
return "__profile__"
}
Expand Down Expand Up @@ -144,12 +150,28 @@ func (c *ProfileCompo) CompoID() string {
return fmt.Sprintf("ProfileCompo:%s", c.ID)
}

type ctxKeyProfileCompo struct{}

func ProfileCompoFromContext(ctx context.Context) *ProfileCompo {
v, _ := ctx.Value(ctxKeyProfileCompo{}).(*ProfileCompo)
return v
}

func ProfileCompoFromEventContext(evCtx *web.EventContext) *ProfileCompo {
return ProfileCompoFromContext(evCtx.R.Context())
}

func (c *ProfileCompo) MustGetEventContext(ctx context.Context) (*web.EventContext, *Messages) {
evCtx := web.MustGetEventContext(ctx)
return evCtx, i18n.MustGetModuleMessages(evCtx.R, I18nAdminLoginKey, Messages_en_US).(*Messages)
}

func (c *ProfileCompo) MarshalHTML(ctx context.Context) ([]byte, error) {
ctx = context.WithValue(ctx, ctxKeyProfileCompo{}, c)

evCtx := web.MustGetEventContext(ctx)
evCtx.WithContextValue(ctxKeyProfileCompo{}, c)

user, err := c.b.currentProfileFunc(ctx)
if err != nil {
return nil, err
Expand All @@ -161,27 +183,39 @@ func (c *ProfileCompo) MarshalHTML(ctx context.Context) ([]byte, error) {
return nil, err
}

return stateful.Actionable(ctx, c, web.Scope().VSlot("{ locals: xlocals }").Init("{ userCardVisible: false }").Children(
h.Div().Class("d-flex align-center ga-2 pa-3").Children(
v.VAvatar().Class("text-body-1 font-weight-medium text-primary bg-primary-lighten-2").Size(v.SizeLarge).Density(v.DensityCompact).Rounded(true).
Text(activity.FirstUpperWord(user.Name)).Children(
h.Iff(user.Avatar != "", func() h.HTMLComponent {
return v.VImg().Attr("alt", user.Name).Attr("src", user.Avatar)
}),
),
h.Div().Class("d-flex flex-column flex-1-1").StyleIf("max-width: 119px", showBellCompo).Children(
h.Div().Class("d-flex align-center ga-2 pt-1").Children(
h.Div().Attr("v-pre", true).Text(user.Name).Class("flex-1-1 text-subtitle-2 text-secondary text-truncate"),
userCardCompo,
),
h.Div().Class("text-overline text-grey-darken-1").Text(strings.ToUpper(user.GetFirstRole())),
),
h.Iff(showBellCompo, func() h.HTMLComponent {
return h.Div().Class("d-flex align-center px-4 me-n3 border-s-sm h-50").Children(
c.bellCompo(ctx, user.NotifCounts),
)
children := []h.HTMLComponent{}
if c.b.prependCompos != nil {
prependCompos, err := c.b.prependCompos(ctx, c)
if err != nil {
return nil, err
}
children = append(children, prependCompos...)
}
children = append(children, []h.HTMLComponent{
v.VAvatar().Class("text-body-1 font-weight-medium text-primary bg-primary-lighten-2").Size(v.SizeLarge).Density(v.DensityCompact).Rounded(true).
Text(activity.FirstUpperWord(user.Name)).Children(
h.Iff(user.Avatar != "", func() h.HTMLComponent {
return v.VImg().Attr("alt", user.Name).Attr("src", user.Avatar)
}),
).Attr("@click", "xlocals.userCardVisible = !xlocals.userCardVisible"),
),
h.Div().Class("d-flex flex-column flex-1-1").
StyleIf("max-width: 119px", showBellCompo).StyleIf("max-width: 184px", !showBellCompo).Children(
h.Div().Class("d-flex align-center ga-2 pt-1").Children(
h.Div().Attr("v-pre", true).Text(user.Name).Class("flex-1-1 text-subtitle-2 text-secondary text-truncate"),
userCardCompo,
),
h.Div().Class("text-overline text-grey-darken-1").Text(strings.ToUpper(user.GetFirstRole())),
),
h.Iff(showBellCompo, func() h.HTMLComponent {
return h.Div().Class("d-flex align-center px-4 me-n3 border-s-sm h-50").Children(
c.bellCompo(ctx, user.NotifCounts),
)
}),
}...)
return stateful.Actionable(ctx, c, web.Scope().VSlot("{ locals: xlocals }").Init("{ userCardVisible: false }").Children(
h.Div().Class("d-flex align-center ga-2 pa-3").Attr("@click", "xlocals.userCardVisible = !xlocals.userCardVisible").Children(
children...,
),
)).MarshalHTML(ctx)
}

Expand Down Expand Up @@ -301,7 +335,7 @@ func (c *ProfileCompo) userCardCompo(ctx context.Context, user *Profile, vmodel
h.Div().Attr("v-if", "!xlocals.editShow").Class("d-flex align-center ga-2").Children(
h.Div().Attr("v-pre", true).Text(user.Name).Class("text-subtitle-1 font-weight-medium text-truncate"),
v.VBtn("").Size(20).Variant(v.VariantText).Color(v.ColorGreyDarken1).
Attr("@click", "xlocals.editShow = true").Children(
Attr("@click", fmt.Sprintf("xlocals.editShow = true; xlocals.name = %q", user.Name)).Children(
v.VIcon("mdi-pencil-outline"),
),
),
Expand Down
2 changes: 2 additions & 0 deletions presets/listing_compo.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ if (payload && payload.ids && payload.ids.length > 0) {
`

func (c *ListingCompo) MarshalHTML(ctx context.Context) (r []byte, err error) {
ctx = context.WithValue(ctx, ctxKeyListingCompo{}, c)

evCtx, _ := c.MustGetEventContext(ctx)
evCtx.WithContextValue(ctxKeyListingCompo{}, c)

Expand Down

0 comments on commit 8d749cb

Please sign in to comment.