Skip to content

Commit 14f6fcf

Browse files
authored
Don't do a full page load when clicking the subscribe button (#28871)
- Refactor the form around the subscribe button into its own template - Use htmx to perform the form submission - `hx-boost="true"` to prevent the default form submission behavior of a full page load - `hx-sync="this:replace"` to replace the current request (in case the button is clicked again before the response is returned) - `hx-target="this"` to replace the form tag with the new form tag - `hx-push-url="false"` to disable a change to the URL - `hx-swap="show:no-scroll"` to preserve the scroll position - Change the backend response to return a `<form>` tag instead of a redirect to the issue page - Include `htmx.org` in javascript imports This change introduces htmx with the hope we could use it to make Gitea more reactive while keeping our "HTML rendered on the server" approach. # Before ![before](https://github.com/go-gitea/gitea/assets/20454870/4ec3e81e-4dbf-4338-9968-b0655c276d4c) # After ![after](https://github.com/go-gitea/gitea/assets/20454870/8c8841af-9bfe-40b2-b1cd-cd1f3c90ba4d) --------- Signed-off-by: Yarden Shoham <[email protected]>
1 parent 80d4862 commit 14f6fcf

File tree

7 files changed

+31
-14
lines changed

7 files changed

+31
-14
lines changed

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"esbuild-loader": "4.0.2",
2626
"escape-goat": "4.0.0",
2727
"fast-glob": "3.3.2",
28+
"htmx.org": "1.9.10",
2829
"jquery": "3.7.1",
2930
"katex": "0.16.9",
3031
"license-checker-webpack-plugin": "0.2.1",

routers/web/repo/issue_watch.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ import (
88
"strconv"
99

1010
issues_model "code.gitea.io/gitea/models/issues"
11+
"code.gitea.io/gitea/modules/base"
1112
"code.gitea.io/gitea/modules/context"
1213
"code.gitea.io/gitea/modules/log"
1314
)
1415

16+
const (
17+
tplWatching base.TplName = "repo/issue/view_content/watching"
18+
)
19+
1520
// IssueWatch sets issue watching
1621
func IssueWatch(ctx *context.Context) {
1722
issue := GetActionIssue(ctx)
@@ -52,5 +57,7 @@ func IssueWatch(ctx *context.Context) {
5257
return
5358
}
5459

55-
ctx.Redirect(issue.Link())
60+
ctx.Data["Issue"] = issue
61+
ctx.Data["IssueWatch"] = &issues_model.IssueWatch{IsWatching: watch}
62+
ctx.HTML(http.StatusOK, tplWatching)
5663
}

templates/repo/issue/view_content/sidebar.tmpl

+1-13
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,7 @@
270270
<div class="ui watching">
271271
<span class="text"><strong>{{ctx.Locale.Tr "notification.notifications"}}</strong></span>
272272
<div class="gt-mt-3">
273-
<form method="post" action="{{.Issue.Link}}/watch">
274-
<input type="hidden" name="watch" value="{{if $.IssueWatch.IsWatching}}0{{else}}1{{end}}">
275-
{{$.CsrfTokenHtml}}
276-
<button class="fluid ui button">
277-
{{if $.IssueWatch.IsWatching}}
278-
{{svg "octicon-mute" 16 "gt-mr-3"}}
279-
{{ctx.Locale.Tr "repo.issues.unsubscribe"}}
280-
{{else}}
281-
{{svg "octicon-unmute" 16 "gt-mr-3"}}
282-
{{ctx.Locale.Tr "repo.issues.subscribe"}}
283-
{{end}}
284-
</button>
285-
</form>
273+
{{template "repo/issue/view_content/watching" .}}
286274
</div>
287275
</div>
288276
{{end}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<form hx-boost="true" hx-sync="this:replace" hx-target="this" hx-push-url="false" hx-swap="show:no-scroll" method="post" action="{{.Issue.Link}}/watch">
2+
<input type="hidden" name="watch" value="{{if $.IssueWatch.IsWatching}}0{{else}}1{{end}}">
3+
{{$.CsrfTokenHtml}}
4+
<button class="fluid ui button">
5+
{{if $.IssueWatch.IsWatching}}
6+
{{svg "octicon-mute" 16 "gt-mr-3"}}
7+
{{ctx.Locale.Tr "repo.issues.unsubscribe"}}
8+
{{else}}
9+
{{svg "octicon-unmute" 16 "gt-mr-3"}}
10+
{{ctx.Locale.Tr "repo.issues.subscribe"}}
11+
{{end}}
12+
</button>
13+
</form>

web_src/js/features/common-global.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {showTemporaryTooltip} from '../modules/tippy.js';
1212
import {confirmModal} from './comp/ConfirmModal.js';
1313
import {showErrorToast} from '../modules/toast.js';
1414
import {request, POST} from '../modules/fetch.js';
15+
import 'htmx.org';
1516

1617
const {appUrl, appSubUrl, csrfToken, i18n} = window.config;
1718

webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ export default {
214214
},
215215
override: {
216216
'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33
217+
'[email protected]': {licenseName: 'BSD-2-Clause'}, // "BSD 2-Clause" -> "BSD-2-Clause"
217218
},
218219
emitError: true,
219220
allow: '(Apache-2.0 OR BSD-2-Clause OR BSD-3-Clause OR MIT OR ISC OR CPAL-1.0 OR Unlicense OR EPL-1.0 OR EPL-2.0)',

0 commit comments

Comments
 (0)