From 1be0880a5410cdaf9e0221749d217477e8d09947 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 31 Jan 2024 17:50:49 +0000 Subject: [PATCH 1/5] Don't do a full page load when clicking `Watch` or `Star` - The watch/unwatch button and star/unstar get their own template - The backend returns HTML instead of redirect Signed-off-by: Yarden Shoham --- routers/web/repo/repo.go | 29 ++++++++++++++++++++++++++++ templates/repo/header.tmpl | 32 ++----------------------------- templates/repo/star_unstar.tmpl | 14 ++++++++++++++ templates/repo/watch_unwatch.tmpl | 14 ++++++++++++++ 4 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 templates/repo/star_unstar.tmpl create mode 100644 templates/repo/watch_unwatch.tmpl diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index b64db914060f4..0620a58318bbf 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -302,6 +302,11 @@ func CreatePost(ctx *context.Context) { handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form) } +const ( + tplWatchUnwatch base.TplName = "repo/watch_unwatch" + tplStarUnstar base.TplName = "repo/star_unstar" +) + // Action response for actions to a repository func Action(ctx *context.Context) { var err error @@ -334,6 +339,30 @@ func Action(ctx *context.Context) { return } + switch ctx.Params(":action") { + case "watch", "unwatch": + ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) + case "star", "unstar": + ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) + } + + switch ctx.Params(":action") { + case "watch", "unwatch", "star", "unstar": + ctx.Data["Repository"], err = repo_model.GetRepositoryByName(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.Name) + if err != nil { + ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err) + return + } + } + + switch ctx.Params(":action") { + case "watch", "unwatch": + ctx.HTML(http.StatusOK, tplWatchUnwatch) + return + case "star", "unstar": + ctx.HTML(http.StatusOK, tplStarUnstar) + } + ctx.RedirectToFirst(ctx.FormString("redirect_to"), ctx.Repo.RepoLink) } diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index dac30af600c3a..93102467ccb3f 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -58,37 +58,9 @@ {{svg "octicon-rss" 16}} {{end}} -
- {{$.CsrfTokenHtml}} -
- - - {{CountFmt .NumWatches}} - -
-
+ {{template "repo/watch_unwatch" $}} {{if not $.DisableStars}} -
- {{$.CsrfTokenHtml}} -
- - - {{CountFmt .NumStars}} - -
-
+ {{template "repo/star_unstar" $}} {{end}} {{if and (not .IsEmpty) ($.Permission.CanRead $.UnitTypeCode)}}
+
+ + + {{CountFmt .Repository.NumStars}} + +
+ diff --git a/templates/repo/watch_unwatch.tmpl b/templates/repo/watch_unwatch.tmpl new file mode 100644 index 0000000000000..c42bc5a9e77d9 --- /dev/null +++ b/templates/repo/watch_unwatch.tmpl @@ -0,0 +1,14 @@ +
+
+ + + {{CountFmt .Repository.NumWatches}} + +
+
From 2c5aaa55826c992e3f113577dae16dac3667e6a6 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 1 Feb 2024 09:08:37 +0200 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: John Olheiser --- routers/web/repo/repo.go | 2 +- templates/repo/star_unstar.tmpl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 0620a58318bbf..918d2ee2370da 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -343,7 +343,7 @@ func Action(ctx *context.Context) { case "watch", "unwatch": ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) case "star", "unstar": - ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) + ctx.Data["IsStarringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) } switch ctx.Params(":action") { diff --git a/templates/repo/star_unstar.tmpl b/templates/repo/star_unstar.tmpl index 9c342f4065944..d574e015134b8 100644 --- a/templates/repo/star_unstar.tmpl +++ b/templates/repo/star_unstar.tmpl @@ -1,7 +1,7 @@ -
+