Skip to content

Commit 2c4e610

Browse files
yardenshohamjolheiser
authored andcommitted
Don't do a full page load when clicking Watch or Star (go-gitea#29001)
- The watch/unwatch button and star/unstar get their own template - The backend returns HTML instead of redirect --------- Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: John Olheiser <[email protected]>
1 parent 897a438 commit 2c4e610

File tree

4 files changed

+61
-30
lines changed

4 files changed

+61
-30
lines changed

routers/web/repo/repo.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ func CreatePost(ctx *context.Context) {
302302
handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form)
303303
}
304304

305+
const (
306+
tplWatchUnwatch base.TplName = "repo/watch_unwatch"
307+
tplStarUnstar base.TplName = "repo/star_unstar"
308+
)
309+
305310
// Action response for actions to a repository
306311
func Action(ctx *context.Context) {
307312
var err error
@@ -334,6 +339,32 @@ func Action(ctx *context.Context) {
334339
return
335340
}
336341

342+
switch ctx.Params(":action") {
343+
case "watch", "unwatch":
344+
ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
345+
case "star", "unstar":
346+
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
347+
}
348+
349+
switch ctx.Params(":action") {
350+
case "watch", "unwatch", "star", "unstar":
351+
// we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed
352+
ctx.Data["Repository"], err = repo_model.GetRepositoryByName(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.Name)
353+
if err != nil {
354+
ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
355+
return
356+
}
357+
}
358+
359+
switch ctx.Params(":action") {
360+
case "watch", "unwatch":
361+
ctx.HTML(http.StatusOK, tplWatchUnwatch)
362+
return
363+
case "star", "unstar":
364+
ctx.HTML(http.StatusOK, tplStarUnstar)
365+
return
366+
}
367+
337368
ctx.RedirectToFirst(ctx.FormString("redirect_to"), ctx.Repo.RepoLink)
338369
}
339370

templates/repo/header.tmpl

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,9 @@
5858
{{svg "octicon-rss" 16}}
5959
</a>
6060
{{end}}
61-
<form method="post" action="{{$.RepoLink}}/action/{{if $.IsWatchingRepo}}un{{end}}watch?redirect_to={{$.Link}}">
62-
{{$.CsrfTokenHtml}}
63-
<div class="ui labeled button" {{if not $.IsSigned}}data-tooltip-content="{{ctx.Locale.Tr "repo.watch_guest_user"}}"{{end}}>
64-
<button type="submit" class="ui compact small basic button"{{if not $.IsSigned}} disabled{{end}}>
65-
{{if $.IsWatchingRepo}}
66-
{{svg "octicon-eye-closed" 16}}<span class="text">{{ctx.Locale.Tr "repo.unwatch"}}</span>
67-
{{else}}
68-
{{svg "octicon-eye"}}<span class="text">{{ctx.Locale.Tr "repo.watch"}}</span>
69-
{{end}}
70-
</button>
71-
<a class="ui basic label" href="{{.Link}}/watchers">
72-
{{CountFmt .NumWatches}}
73-
</a>
74-
</div>
75-
</form>
61+
{{template "repo/watch_unwatch" $}}
7662
{{if not $.DisableStars}}
77-
<form method="post" action="{{$.RepoLink}}/action/{{if $.IsStaringRepo}}un{{end}}star?redirect_to={{$.Link}}">
78-
{{$.CsrfTokenHtml}}
79-
<div class="ui labeled button" {{if not $.IsSigned}}data-tooltip-content="{{ctx.Locale.Tr "repo.star_guest_user"}}"{{end}}>
80-
<button type="submit" class="ui compact small basic button"{{if not $.IsSigned}} disabled{{end}}>
81-
{{if $.IsStaringRepo}}
82-
{{svg "octicon-star-fill"}}<span class="text">{{ctx.Locale.Tr "repo.unstar"}}</span>
83-
{{else}}
84-
{{svg "octicon-star"}}<span class="text">{{ctx.Locale.Tr "repo.star"}}</span>
85-
{{end}}
86-
</button>
87-
<a class="ui basic label" href="{{.Link}}/stars">
88-
{{CountFmt .NumStars}}
89-
</a>
90-
</div>
91-
</form>
63+
{{template "repo/star_unstar" $}}
9264
{{end}}
9365
{{if and (not .IsEmpty) ($.Permission.CanRead $.UnitTypeCode)}}
9466
<div class="ui labeled button

templates/repo/star_unstar.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<form hx-boost="true" hx-target="this" method="post" action="{{$.RepoLink}}/action/{{if $.IsStaringRepo}}un{{end}}star">
2+
<div class="ui labeled button" {{if not $.IsSigned}}data-tooltip-content="{{ctx.Locale.Tr "repo.star_guest_user"}}"{{end}}>
3+
<button type="submit" class="ui compact small basic button"{{if not $.IsSigned}} disabled{{end}}>
4+
{{if $.IsStaringRepo}}
5+
{{svg "octicon-star-fill"}}<span class="text">{{ctx.Locale.Tr "repo.unstar"}}</span>
6+
{{else}}
7+
{{svg "octicon-star"}}<span class="text">{{ctx.Locale.Tr "repo.star"}}</span>
8+
{{end}}
9+
</button>
10+
<a hx-boost="false" class="ui basic label" href="{{$.RepoLink}}/stars">
11+
{{CountFmt .Repository.NumStars}}
12+
</a>
13+
</div>
14+
</form>

templates/repo/watch_unwatch.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<form hx-boost="true" hx-target="this" method="post" action="{{$.RepoLink}}/action/{{if $.IsWatchingRepo}}un{{end}}watch">
2+
<div class="ui labeled button" {{if not $.IsSigned}}data-tooltip-content="{{ctx.Locale.Tr "repo.watch_guest_user"}}"{{end}}>
3+
<button type="submit" class="ui compact small basic button"{{if not $.IsSigned}} disabled{{end}}>
4+
{{if $.IsWatchingRepo}}
5+
{{svg "octicon-eye-closed" 16}}<span class="text">{{ctx.Locale.Tr "repo.unwatch"}}</span>
6+
{{else}}
7+
{{svg "octicon-eye"}}<span class="text">{{ctx.Locale.Tr "repo.watch"}}</span>
8+
{{end}}
9+
</button>
10+
<a hx-boost="false" class="ui basic label" href="{{.RepoLink}}/watchers">
11+
{{CountFmt .Repository.NumWatches}}
12+
</a>
13+
</div>
14+
</form>

0 commit comments

Comments
 (0)