Skip to content

Commit 780c7e1

Browse files
committed
Add prevention for duplicate form submissions
1 parent bc28654 commit 780c7e1

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

templates/repo/pulls/fork.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</h3>
77
<div class="ui attached segment">
88
{{template "base/alert" .}}
9-
<form class="ui form left-right-form" action="{{.Link}}" method="post">
9+
<form class="ui form left-right-form" action="{{.Link}}" method="post" data-prevent-duplicate>
1010
{{.CsrfTokenHtml}}
1111
<div class="inline required field {{if .Err_Owner}}error{{end}}">
1212
<label>{{ctx.Locale.Tr "repo.owner"}}</label>
@@ -75,7 +75,7 @@
7575

7676
<div class="inline field">
7777
<label></label>
78-
<button class="ui primary button{{if not .CanForkRepoInNewOwner}} disabled{{end}}">
78+
<button class="ui primary button{{if not .CanForkRepoInNewOwner}} disabled{{end}}" type="submit">
7979
{{ctx.Locale.Tr "repo.fork_repo"}}
8080
</button>
8181
</div>

web_src/js/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {initGlobalButtonClickOnEnter, initGlobalButtons, initGlobalDeleteButton}
6565
import {initGlobalComboMarkdownEditor, initGlobalEnterQuickSubmit, initGlobalFormDirtyLeaveConfirm} from './features/common-form.ts';
6666
import {callInitFunctions} from './modules/init.ts';
6767
import {initRepoViewFileTree} from './features/repo-view-file-tree.ts';
68+
import {initFormSubmitHandlers} from './modules/form.ts';
6869

6970
initGiteaFomantic();
7071
initSubmitEventPolyfill();
@@ -85,6 +86,7 @@ onDomReady(() => {
8586
initGlobalComboMarkdownEditor,
8687
initGlobalDeleteButton,
8788
initGlobalInput,
89+
initFormSubmitHandlers,
8890

8991
initCommonOrganization,
9092
initCommonIssueListQuickGoto,

web_src/js/modules/form.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Form handling utilities
3+
*/
4+
5+
import {addDelegatedEventListener} from '../utils/dom.ts';
6+
7+
/**
8+
* Prevent duplicate form submission
9+
* @param form The form element
10+
* @param submitter The submit button element
11+
*/
12+
function preventDuplicateSubmit(form: HTMLFormElement, submitter: HTMLElement) {
13+
form.addEventListener('submit', () => {
14+
submitter.classList.add('disabled');
15+
submitter.setAttribute('disabled', 'disabled');
16+
});
17+
}
18+
19+
/**
20+
* Initialize form submit handlers
21+
*/
22+
export function initFormSubmitHandlers() {
23+
// Add delegated event listener for forms with data-prevent-duplicate attribute
24+
addDelegatedEventListener(document, 'submit', 'form[data-prevent-duplicate]', (form: HTMLFormElement) => {
25+
const submitter = form.querySelector<HTMLElement>('button[type="submit"]');
26+
if (submitter) {
27+
preventDuplicateSubmit(form, submitter);
28+
}
29+
});
30+
}

0 commit comments

Comments
 (0)