Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent double click new issue/pull/comment button #16157

Merged
merged 12 commits into from
May 7, 2022
2 changes: 1 addition & 1 deletion templates/repo/issue/new_form.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</div>
{{template "repo/issue/comment_tab" .}}
<div class="text right">
<button class="ui green button" tabindex="6">
<button class="ui green button loading-button" tabindex="6">
{{if .PageIsComparePull}}
{{.i18n.Tr "repo.pulls.create"}}
{{else}}
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/issue/view_content.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
</div>
{{end}}
{{end}}
<button class="ui green button" tabindex="5">
<button class="ui green button loading-button" tabindex="5">
{{.i18n.Tr "repo.issues.create_comment"}}
</button>
</div>
Expand Down Expand Up @@ -172,7 +172,7 @@
</div>
{{end}}
{{end}}
<button class="ui green button" tabindex="5">
<button class="ui green button loading-button" tabindex="5">
{{.i18n.Tr "repo.issues.create_comment"}}
</button>
</div>
Expand Down
12 changes: 12 additions & 0 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ export function initGlobalCommon() {
window.location = href;
}
});

// loading-button this logic used to prevent push one form more than one time
$(document).on('click', '.button.loading-button', function (e) {
const $btn = $(this);

if ($btn.hasClass('loading')) {
e.preventDefault();
return false;
}
Comment on lines +146 to +153
Copy link
Contributor

@wxiaoguang wxiaoguang May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need capture instead of bubble here. Otherwise the event handler on the button would be executed first (for example: ajax, IIRC some form buttons are doing ajax requests)

https://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing

Copy link
Contributor

@wxiaoguang wxiaoguang May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, what I mean is:

If we are trying to introduce a general method, should the all cases be considered together? Will this mechanism be used for ajax buttons in future?

  • If this mechanism is for traditional forms (non-ajax) only, these CSS names are too broad, it will be mis-used by ajax buttons in future.

  • If this mechanism is for both traditional forms and ajax requests, it could be fine tuned some more.


update: I would suggest to use form-once-submit as the CSS name for these form-only buttons.

In future, we can have ajax-once-request CSS class for ajax buttons (if necessary), or ajax buttons should handle the repeated clicks by themselves.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be generic for all use cases where technically possible and needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either is fine to me:

  • Make the CSS name in this PR clear for only traditional form buttons, and get this PR merged.
  • (OR) Let's spend some more time to fine tune some more.

How do you think?

Copy link
Member Author

@a1012112796 a1012112796 May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either is fine to me:

  • Make the CSS name in this PR clear for only traditional form buttons, and get this PR merged.
  • (OR) Let's spend some more time to fine tune some more.

How do you think?

how about limit it by this way?

Suggested change
// loading-button this logic used to prevent push one form more than one time
$(document).on('click', '.button.loading-button', function (e) {
const $btn = $(this);
if ($btn.hasClass('loading')) {
e.preventDefault();
return false;
}
// loading-button this logic used to prevent push one form more than one time
// like double create new issue , pull request or comments
$(document).on('click', 'form .button.loading-button', function (e) {
const $btn = $(this);
if ($btn.hasClass('loading')) {
e.preventDefault();
return false;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loading-button is not a proper name ......

If you are reading some code, when you see a button called loading-button, you might think it is a button which is loading something. BUT, the purpose of the class is to make the button can only submit the form once.

I would suggest to use a clear name to reduce misunderstanding.


$btn.addClass('loading disabled');
Copy link
Contributor

@wxiaoguang wxiaoguang May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$btn.addClass('loading disabled');
$btn.addClass('loading disabled');
$btn.prop('disabled', true);

Should we use $btn.prop('disabled', true); to disable the button entirely? Then it's not clickable anymore, and we do not need to check .loading anymore, and no need to return false (preventDefault)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks semantic ui has handled it, just add disabled class is enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what if it is not a Fomantic UI button? For example, what if some one writes a form without .ui class but uses your code?

If you require the developers to use your code in Fomantic UI context, you should change the selector to .ui.button .xxxxxx to make sure the code only works for Fomantic UI.

});
}

export function initGlobalDropzone() {
Expand Down