Skip to content

Prevent duplicate alerts when Save is clicked several times - #323

Merged
snake14 merged 5 commits into
6.x-devfrom
PG-4622
Dec 2, 2026
Merged

snake14 merged 5 commits into
6.x-devfrom
PG-4622

Conversation

@AltamashShaikh

Copy link
Copy Markdown
Contributor

Description

Clicking Save on the create/edit alert form several times created one alert per click.

Root cause: sendApiRequest() in vue/src/EditAlert/EditAlert.vue reset isLoading in .finally(). On success it did that right after it started the redirect to the alert list, so the Save button became active again while the browser was still navigating away. Every further click sent another CustomAlerts.addAlert request. Clicks that arrived before the button had re-rendered as disabled also each sent a request, because nothing in sendApiRequest() checked whether a save was already running.

Fix:

  • sendApiRequest() now ignores calls while a save is in progress.
  • isLoading is reset only when the request fails. After a successful save the button stays disabled until the redirect has left the page; after an error it is enabled again so the user can correct the form.
  • vue/dist/CustomAlerts.umd.min.js is rebuilt.

Both creating and editing alerts go through sendApiRequest(), so both are covered.

The API (CustomAlerts.addAlert) is unchanged and does not reject identical alerts, because creating two alerts with the same settings can be intended. The duplicates came from the form sending the same request several times, and that is what this PR fixes.

Issue No

Reported internally (no public GitHub issue).

Steps to Replicate the Issue

  1. Go to Administration → Personal → Alerts → Create new alert, fill in the required fields and click Save several times quickly (the button became active again before the page changed).
  2. Expected: exactly one alert is created.
  3. Actual: one alert was created per click. With 4 quick clicks in a browser, 4 addAlert requests were sent and 3 duplicate alerts showed up in the list. With this change: 1 request and 1 alert. After a failed save (validation error) the button is enabled again.

Tests: two new UI tests in tests/UI/CustomAlerts_spec.js:

  • should create only one alert when save is clicked several times in a row
  • should keep save disabled after the alert was saved until the page has changed

Both fail without the fix (2 alerts were created; the button was enabled again after the request) and pass with it. The rest of the spec still passes.

QA

Checked in a browser on a test install, following the bug's steps before and after the fix.

Steps:

  1. Set up the CustomAlerts test fixture and logged in as the fixture super user on http://127.0.0.1:8088 (test proxy), headless Chrome via puppeteer.
  2. Before (original plugin code): opened Administration > Alerts > Create new alert, entered a name, chose Email + 'Send to me', condition 'is less than', value 10, then clicked Save 4 times with the mouse 15 ms apart.
  3. Result before: 4 CustomAlerts.addAlert requests and 3 identical alerts in Manage Custom Alerts (before-1.png).
  4. After (worktree with the fix linked as the plugin): same steps; 1 addAlert request and 1 alert in the list (after-1.png).
  5. After: submitted the form with a missing required field; the error notification is shown and the Save button is enabled again (after-2.png).

Before the fix: four quick clicks on Save created three identical alerts

before-1.png

After the fix: the same four clicks create a single alert

after-1.png

After the fix: when saving fails, the error is shown and Save can be clicked again

after-2.png

Checklist

  • [✔] Tested locally or on demo2/demo3?
  • [✔] New test case added/updated?
  • [NA] Are all newly added texts included via translation?
  • [NA] Are text sanitized properly? (Eg use of v-text v/s v-html for vue)
  • [NA] Version bumped?
  • [✖] I have understood, reviewed, and tested all AI outputs before use
  • [✔] All AI instructions respect security, IP, and privacy rules
  • [NA] Documentation updated?

🤖 Generated with Claude Code

Ubuntu and others added 2 commits December 1, 2026 04:51
The save button of the alert form was re-enabled as soon as the API
request finished, before the redirect to the alert list had left the
page, and a click arriving before the button re-rendered as disabled
still sent another request. Each extra click created another alert.

Ignore saves while one is in progress and only re-enable the button
when saving failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent: fixed checks broken by this PR (ci / release-date / Release date).

The 'Release date' check failed: the PR bumped plugin.json to 6.0.6 and added a new 6.0.6 changelog entry, but 6.0.5 is still undated and unreleased (no tag), so there were two unreleased entries.
Fix (caused by this PR): put the change into the existing unreleased '* 6.0.5 - Updated translations' entry using the file's one-line format, and set plugin.json back to 6.0.5. CHANGELOG.md and plugin.json are now unchanged from 6.x-dev apart from the merged entry text.
The PR changes no PHP files, so PHPCS has nothing to check; no PHPUnit/UI tests are involved in this check.

Changed:

  • CHANGELOG.md
  • plugin.json

@AltamashShaikh
AltamashShaikh marked this pull request as ready for review December 1, 2026 07:24
@AltamashShaikh
AltamashShaikh requested a review from a team December 1, 2026 07:24

@snake14 snake14 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Repeated Save clicks now create a single alert, on both the create and edit forms, and Save comes back after a failed save. One item to check:

Medium

  1. After a successful save, the browser's Back button restores the form from the back/forward cache with isLoading still true, so Save stays disabled under "Loading data…" until the page is reloaded. Resetting isLoading in a pageshow listener when event.persisted is true should fix it.

…rd cache

After a successful save the form keeps Save disabled until the redirect
leaves the page. If the browser later restores that page with the back
button, the component state was restored too, so Save stayed disabled.
Reset the saving state on a persisted pageshow event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent, replying to @snake14:

Repeated Save clicks now create a single alert, on both the create and edit forms, and Save comes back after a failed save. One item to check:

Medium

  1. After a successful save, the browser's Back button restores the form from the back/forward cache with isLoading still true, so Save stays disabled under "Loading data…" until the page is reloaded. Resetting isLoading in a pageshow listener when event.persisted is true should fix it.

Thanks, good catch. The alert form now listens for the pageshow event and sets the saving state back to false when event.persisted is true, so Save is usable again when the page is restored from the back/forward cache. I added a UI test that saves the form, fires a persisted pageshow event and checks that Save is enabled again; it fails without the fix.

@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent: pushed d95c929 for the review feedback.

Save no longer stays disabled under "Loading data…" when the alert form is restored from the back/forward cache after a successful save: a pageshow listener resets the saving state when event.persisted is true.
Added a UI test for this case, moved the shared "save without redirect" steps of the UI tests into one helper, and rebuilt the Vue bundle.

Changed:

  • tests/UI/CustomAlerts_spec.js
  • vue/dist/CustomAlerts.umd.min.js
  • vue/src/EditAlert/EditAlert.vue

@snake14 snake14 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Repeated Save clicks still create a single alert, and Save is now enabled again when the form is restored with the browser's Back button. A couple of things to check:

Low

  1. In "should keep save disabled after the alert was saved until the page has changed", the click() on the disabled Save button does nothing (click() on a disabled form control dispatches no event), so the alert count after it would pass without the isLoading guard. The disabled check already covers this case, so the click and the count could be dropped.
  2. The waitForTimeout(250) in saveWithoutLeavingPage() can likely go, since the button should already have re-rendered by the time waitForFunction('!!window.redirectAfterSave') resolves.

Clicking a disabled button dispatches no event, so the extra click and
alert count proved nothing beyond the disabled check. The button is
already re-rendered once the save request has resolved, so the 250ms
wait is not needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent, replying to @snake14:

Repeated Save clicks still create a single alert, and Save is now enabled again when the form is restored with the browser's Back button. A couple of things to check:

Low

  1. In "should keep save disabled after the alert was saved until the page has changed", the click() on the disabled Save button does nothing (click() on a disabled form control dispatches no event), so the alert count after it would pass without the isLoading guard. The disabled check already covers this case, so the click and the count could be dropped.
  2. The waitForTimeout(250) in `saveWithoutLeav

Agreed on both points. In "should keep save disabled after the alert was saved until the page has changed" I removed the click on the disabled Save button and the alert count that followed it, so the test now relies on the disabled check (the rapid-click test still checks that only one alert is created). I also removed the waitForTimeout(250) from saveWithoutLeavingPage(), since the button is already disabled by the time the save request resolves; the three new UI tests still pass.

@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent: pushed 7d64269 for the review feedback.

Simplified the new save-button UI tests after review: removed a click on the disabled Save button (it dispatches no event) and the alert count after it, and removed an unneeded 250ms wait after saving. No change to the plugin code.

Changed:

  • tests/UI/CustomAlerts_spec.js

@snake14
snake14 merged commit 04bea2d into 6.x-dev Dec 2, 2026
24 checks passed
@snake14
snake14 deleted the PG-4622 branch December 2, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants