Skip to content

Update the list of watchers and stargazers when clicking watch/unwatch or star/unstar #32570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ func Action(ctx *context.Context) {
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
}

// if we have user cards on the page we should refresh them
ctx.RespHeader().Add("HX-Trigger", "refreshCards")

switch ctx.PathParam(":action") {
case "watch", "unwatch", "star", "unstar":
// we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed
Expand Down
42 changes: 30 additions & 12 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
tplRepoHome base.TplName = "repo/home"
tplRepoViewList base.TplName = "repo/view_list"
tplWatchers base.TplName = "repo/watchers"
tplCards base.TplName = "repo/user_cards"
tplForks base.TplName = "repo/forks"
tplMigrating base.TplName = "repo/migrate/migrating"
)
Expand Down Expand Up @@ -1122,25 +1123,42 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp
ctx.HTML(http.StatusOK, tpl)
}

func renderUserList(ctx *context.Context, pageType string, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) {
ctx.Data["Title"] = ctx.Tr(pageType)
ctx.Data["CardsTitle"] = ctx.Tr(pageType)
RenderUserCards(ctx, total, getter, tpl)
}

// Watchers render repository's watch users
func Watchers(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.watchers")
ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
ctx.Data["PageIsWatchers"] = true
renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches,
func(opts db.ListOptions) ([]*user_model.User, error) {
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
}, tplWatchers)
}

RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) {
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
}, tplWatchers)
// WatchersCards renders a repository's watchers user cards
func WatchersCards(ctx *context.Context) {
renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches,
func(opts db.ListOptions) ([]*user_model.User, error) {
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
}, tplCards)
}

// Stars render repository's starred users
func Stars(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.stargazers")
ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
ctx.Data["PageIsStargazers"] = true
RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) {
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
}, tplWatchers)
renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars,
func(opts db.ListOptions) ([]*user_model.User, error) {
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
}, tplWatchers)
}

// StarsCards renders a repository's stargazers user cards
func StarsCards(ctx *context.Context) {
renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars,
func(opts db.ListOptions) ([]*user_model.User, error) {
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
}, tplCards)
}

// Forks render repository's forked users
Expand Down
2 changes: 2 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,9 @@ func registerRoutes(m *web.Router) {

m.Group("/{username}/{reponame}", func() {
m.Get("/stars", repo.Stars)
m.Get("/stars/cards", repo.StarsCards)
m.Get("/watchers", repo.Watchers)
m.Get("/watchers/cards", repo.WatchersCards)
m.Get("/search", reqRepoCodeReader, repo.Search)
m.Post("/action/{action}", reqSignIn, repo.Action)
}, optSignIn, context.RepoAssignment, context.RepoRef())
Expand Down
9 changes: 8 additions & 1 deletion templates/repo/watchers.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content repository watchers">
{{template "repo/header" .}}
<div class="ui container">
<div class="no-loading-indicator tw-hidden"></div>
<div
hx-trigger="refreshCards from:body"
hx-indicator=".no-loading-indicator"
hx-swap="innerHTML"
hx-get="{{$.Link}}/cards"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it could use hx-select to partially reloading, then no need to add these /cards endpoints.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will try it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed 🤣

Managed to get it work in da86679, only a few changed lines now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing, it should use CurrentURL but not Link, because there could be "page" query parameters.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks great and the diff is smaller, thank you.

class="ui container"
>
{{template "repo/user_cards" .}}
</div>
</div>
Expand Down
Loading