Skip to content

Commit

Permalink
Don't do a full page load when clicking the subscribe button
Browse files Browse the repository at this point in the history
- 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.

Signed-off-by: Yarden Shoham <[email protected]>
  • Loading branch information
yardenshoham committed Jan 20, 2024
1 parent 6c771a3 commit 864cc22
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"esbuild-loader": "4.0.2",
"escape-goat": "4.0.0",
"fast-glob": "3.3.2",
"htmx.org": "1.9.10",
"jquery": "3.7.1",
"katex": "0.16.9",
"license-checker-webpack-plugin": "0.2.1",
Expand Down
9 changes: 8 additions & 1 deletion routers/web/repo/issue_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
"strconv"

issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
)

const (
tplWatching base.TplName = "repo/issue/view_content/watching"
)

// IssueWatch sets issue watching
func IssueWatch(ctx *context.Context) {
issue := GetActionIssue(ctx)
Expand Down Expand Up @@ -52,5 +57,7 @@ func IssueWatch(ctx *context.Context) {
return
}

ctx.Redirect(issue.Link())
ctx.Data["Issue"] = issue
ctx.Data["IssueWatch"] = &issues_model.IssueWatch{IsWatching: watch}
ctx.HTML(http.StatusOK, tplWatching)
}
14 changes: 1 addition & 13 deletions templates/repo/issue/view_content/sidebar.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,7 @@
<div class="ui watching">
<span class="text"><strong>{{ctx.Locale.Tr "notification.notifications"}}</strong></span>
<div class="gt-mt-3">
<form method="post" action="{{.Issue.Link}}/watch">
<input type="hidden" name="watch" value="{{if $.IssueWatch.IsWatching}}0{{else}}1{{end}}">
{{$.CsrfTokenHtml}}
<button class="fluid ui button">
{{if $.IssueWatch.IsWatching}}
{{svg "octicon-mute" 16 "gt-mr-3"}}
{{ctx.Locale.Tr "repo.issues.unsubscribe"}}
{{else}}
{{svg "octicon-unmute" 16 "gt-mr-3"}}
{{ctx.Locale.Tr "repo.issues.subscribe"}}
{{end}}
</button>
</form>
{{template "repo/issue/view_content/watching" .}}
</div>
</div>
{{end}}
Expand Down
13 changes: 13 additions & 0 deletions templates/repo/issue/view_content/watching.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<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">
<input type="hidden" name="watch" value="{{if $.IssueWatch.IsWatching}}0{{else}}1{{end}}">
{{$.CsrfTokenHtml}}
<button class="fluid ui button">
{{if $.IssueWatch.IsWatching}}
{{svg "octicon-mute" 16 "gt-mr-3"}}
{{ctx.Locale.Tr "repo.issues.unsubscribe"}}
{{else}}
{{svg "octicon-unmute" 16 "gt-mr-3"}}
{{ctx.Locale.Tr "repo.issues.subscribe"}}
{{end}}
</button>
</form>
1 change: 1 addition & 0 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {showTemporaryTooltip} from '../modules/tippy.js';
import {confirmModal} from './comp/ConfirmModal.js';
import {showErrorToast} from '../modules/toast.js';
import {request, POST} from '../modules/fetch.js';
import 'htmx.org';

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

Expand Down

0 comments on commit 864cc22

Please sign in to comment.