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

Fix repo diff review comment counter increment behaviour #31641

Conversation

henrygoodman
Copy link
Contributor

@henrygoodman henrygoodman commented Jul 16, 2024

Fixes #31622

Check above issue for specific behavioural issues this PR will fix

Issue 1

Currently, initializing the review comment form adds the submit handler each time the button is clicked, which means the button can end up with many handlers. When this happens, finally submitting (i.e. when the textarea is non-empty) will call all submit handler instances, incrementing the review comment counter by a lot. This is all UI so reloading the page will get the correct value from database.

Fix 1

Use a flag when attaching the handler, only attach if the flag is unset.

Issue 2

Additionally, the form is currently submitted on the button click, without validating the form on frontend (i.e. we manually call the form submit within the click handler, but the HTML will not allow the form to submit if the textarea is empty).

I'd like to point out that if we can disable the button whilst the textarea is empty, we don't have to do any validation here (as the button cannot be clicked anyway until the form can officially be submitted).

Fix 2

Prevent default behaviour in this handler if the textarea is empty.

Issue 3

We were not handling form submission on CTRL+Enter, only on button click.

Fix 3

Added explicit handler for keydown event to check CTRL+Enter and submit form to increment the counter.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 16, 2024
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 16, 2024
@techknowlogick techknowlogick added type/bug topic/ui Change the appearance of the Gitea UI labels Jul 19, 2024
@yardenshoham
Copy link
Member

New PRs shouldn't use jQuery https://docs.gitea.com/contributing/guidelines-frontend#framework-usage

@henrygoodman henrygoodman force-pushed the fix-repo-diff-review-comment-counter branch from cdb99cc to 3bbb210 Compare August 2, 2024 11:32
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 'Enter') {
const textarea = e.target;
if (textarea.tagName.toLowerCase() === 'textarea') {
Copy link
Member

Choose a reason for hiding this comment

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

toLowerCase? 🤨

Copy link
Member

Choose a reason for hiding this comment

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

Also, why is web_src/js/features/common_form.ts not dispatching this event already?
Fixing the reason why it isn't handled sounds like a better idea to me than duplicating the Ctrl+Enter behavior here manually.


// Handle submit on click
document.addEventListener('click', (e) => {
if (e.target.name === 'pending_review') {
Copy link
Member

Choose a reason for hiding this comment

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

This condition seems wrong to me:
Isn't the target name button here?
Shouldn't it be hasAttribute?

Copy link
Member

Choose a reason for hiding this comment

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

Either way, it's probably better to use

Suggested change
if (e.target.name === 'pending_review') {
if (e.target.matches('[name="pending_review"]') {

// Watch for the form's submit event.
$form.on('submit', () => {
const num = parseInt(counter.getAttribute('data-pending-comment-number')) + 1 || 1;
function handleFormSubmit(form, textarea) {
Copy link
Member

Choose a reason for hiding this comment

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

This architecture is a bit weird:
Why remove and re-add the handler all the time?
Typically, our behavior would rather be "initialize once on page load, and then keep it".

@henrygoodman
Copy link
Contributor Author

henrygoodman commented Nov 21, 2024

Checked if reproducible, issue seems to be fixed now (I guess externally as delvh mentioned, ctrl-Enter handling likely was fixed elsewhere). Closing

EDIT: Thought it was fixed when checking on main, but forgot to clear browser cache. Issue was still present.

@wxiaoguang
Copy link
Contributor

The bug is still there and needs to be fixed. Will propose a fix.

@wxiaoguang
Copy link
Contributor

-> Fix PR diff review form submit #32596

@henrygoodman
Copy link
Contributor Author

henrygoodman commented Nov 21, 2024

The bug is still there and needs to be fixed. Will propose a fix.

Thanks @wxiaoguang, yes bug was actually still present, just checked.
I thought it was fixed in main as I switched branch, but forgot to disable cache (silly).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. topic/ui Change the appearance of the Gitea UI type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PR Review Add Comment increments counter in Review button but CTRL+Enter shortcut doesn't
6 participants