Skip to content

Commit c621f9a

Browse files
committed
Update the list of watchers and stargazers when clicking watch/unwatch or star/unstar
We make sure the user cards are updated Signed-off-by: Yarden Shoham <[email protected]>
1 parent 0d5abd9 commit c621f9a

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

routers/web/repo/repo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ func Action(ctx *context.Context) {
352352
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
353353
}
354354

355+
// if we have user cards on the page we should refresh them
356+
ctx.RespHeader().Add("HX-Trigger", "refreshCards")
357+
355358
switch ctx.PathParam(":action") {
356359
case "watch", "unwatch", "star", "unstar":
357360
// we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed

routers/web/repo/view.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const (
6565
tplRepoHome base.TplName = "repo/home"
6666
tplRepoViewList base.TplName = "repo/view_list"
6767
tplWatchers base.TplName = "repo/watchers"
68+
tplCards base.TplName = "repo/user_cards"
6869
tplForks base.TplName = "repo/forks"
6970
tplMigrating base.TplName = "repo/migrate/migrating"
7071
)
@@ -1122,25 +1123,42 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp
11221123
ctx.HTML(http.StatusOK, tpl)
11231124
}
11241125

1126+
func renderUserList(ctx *context.Context, pageType string, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) {
1127+
ctx.Data["Title"] = ctx.Tr(pageType)
1128+
ctx.Data["CardsTitle"] = ctx.Tr(pageType)
1129+
RenderUserCards(ctx, total, getter, tpl)
1130+
}
1131+
11251132
// Watchers render repository's watch users
11261133
func Watchers(ctx *context.Context) {
1127-
ctx.Data["Title"] = ctx.Tr("repo.watchers")
1128-
ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
1129-
ctx.Data["PageIsWatchers"] = true
1134+
renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches,
1135+
func(opts db.ListOptions) ([]*user_model.User, error) {
1136+
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
1137+
}, tplWatchers)
1138+
}
11301139

1131-
RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) {
1132-
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
1133-
}, tplWatchers)
1140+
// WatchersCards renders a repository's watchers user cards
1141+
func WatchersCards(ctx *context.Context) {
1142+
renderUserList(ctx, "repo.watchers", ctx.Repo.Repository.NumWatches,
1143+
func(opts db.ListOptions) ([]*user_model.User, error) {
1144+
return repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, opts)
1145+
}, tplCards)
11341146
}
11351147

11361148
// Stars render repository's starred users
11371149
func Stars(ctx *context.Context) {
1138-
ctx.Data["Title"] = ctx.Tr("repo.stargazers")
1139-
ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
1140-
ctx.Data["PageIsStargazers"] = true
1141-
RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) {
1142-
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
1143-
}, tplWatchers)
1150+
renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars,
1151+
func(opts db.ListOptions) ([]*user_model.User, error) {
1152+
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
1153+
}, tplWatchers)
1154+
}
1155+
1156+
// StarsCards renders a repository's stargazers user cards
1157+
func StarsCards(ctx *context.Context) {
1158+
renderUserList(ctx, "repo.stargazers", ctx.Repo.Repository.NumStars,
1159+
func(opts db.ListOptions) ([]*user_model.User, error) {
1160+
return repo_model.GetStargazers(ctx, ctx.Repo.Repository, opts)
1161+
}, tplCards)
11441162
}
11451163

11461164
// Forks render repository's forked users

routers/web/web.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,9 @@ func registerRoutes(m *web.Router) {
15991599

16001600
m.Group("/{username}/{reponame}", func() {
16011601
m.Get("/stars", repo.Stars)
1602+
m.Get("/stars/cards", repo.StarsCards)
16021603
m.Get("/watchers", repo.Watchers)
1604+
m.Get("/watchers/cards", repo.WatchersCards)
16031605
m.Get("/search", reqRepoCodeReader, repo.Search)
16041606
m.Post("/action/{action}", reqSignIn, repo.Action)
16051607
}, optSignIn, context.RepoAssignment, context.RepoRef())

templates/repo/watchers.tmpl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
{{template "base/head" .}}
22
<div role="main" aria-label="{{.Title}}" class="page-content repository watchers">
33
{{template "repo/header" .}}
4-
<div class="ui container">
4+
<div class="no-loading-indicator tw-hidden"></div>
5+
<div
6+
hx-trigger="refreshCards from:body"
7+
hx-indicator=".no-loading-indicator"
8+
hx-swap="innerHTML"
9+
hx-get="{{$.Link}}/cards"
10+
class="ui container"
11+
>
512
{{template "repo/user_cards" .}}
613
</div>
714
</div>

0 commit comments

Comments
 (0)