fix: add isSaving to state, guard submit and catch errors#1010
Conversation
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
📝 WalkthroughWalkthrough
ChangesBadge settings save flow
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant BadgeSettingsForm
participant onSubmit
participant Swal.fire
BadgeSettingsForm->>BadgeSettingsForm: Set isSaving to true
BadgeSettingsForm->>onSubmit: Submit updated entity fields
onSubmit-->>BadgeSettingsForm: Resolve or reject
BadgeSettingsForm->>Swal.fire: Show success alert on resolve
BadgeSettingsForm->>BadgeSettingsForm: Reset isSaving in finally
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/components/forms/badge-settings-form.js`:
- Around line 172-188: Update handleSubmit’s onSubmit flow to create a Promise
boundary with Promise.resolve().then(() => this.props.onSubmit(settingsToSave)),
preserving the existing success, catch, and finally handlers so synchronous
throws or non-Promise returns always reset isSaving.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: bd39d0f8-2444-4382-9089-0b940b4d0b5f
📒 Files selected for processing (2)
src/components/forms/__tests__/badge-settings-form.test.jssrc/components/forms/badge-settings-form.js
|
|
||
| Swal.fire(success_message); | ||
| }) | ||
| .catch(() => {}) |
There was a problem hiding this comment.
@tomrndom .catch(() => {}) on this line sits after .then(), so it swallows errors thrown inside the success handler (the block above), not just rejections from onSubmit. If Swal.fire(success_message) throws for any reason (bad message shape, a sweetalert2 internal error), that error is silently absorbed here: isSaving still resets via .finally(), but the user sees neither a success nor an error message, and the bug never surfaces in the console — a real save success ends up looking identical to a swallowed exception.
Suggested fix — use the two-argument form of .then() so the reject handler only covers onSubmit's own rejection, letting a success-handler error propagate instead of being masked:
this.props
.onSubmit(settingsToSave)
.then(
() => {
Swal.fire({
title: T.translate("general.done"),
html: T.translate("badge_settings.badge_template_settings_updated"),
type: "success"
});
},
() => {} // only swallows onSubmit's own rejection
)
.finally(() => {
this.setState({ isSaving: false });
});Regression test to add alongside it in badge-settings-form.test.js — proves the success-handler error is no longer swallowed (fails on the current .then().catch() chain, passes after the fix above):
import Swal from "sweetalert2";
it("should not swallow an error thrown by the success handler", async () => {
const onSubmit = jest.fn(() => Promise.resolve());
Swal.fire.mockImplementationOnce(() => {
throw new Error("Swal render error");
});
const rejections = [];
const onUnhandledRejection = (event) => rejections.push(event.reason);
window.addEventListener("unhandledrejection", onUnhandledRejection);
const { container } = renderForm(onSubmit);
fireEvent.change(container.querySelector("#BADGE_TEMPLATE_WIDTH"), {
target: { value: "100" }
});
fireEvent.click(screen.getByRole("button", { name: "general.save" }));
await waitFor(() => expect(rejections).toHaveLength(1));
window.removeEventListener("unhandledrejection", onUnhandledRejection);
});
ref: https://app.clickup.com/t/9014802374/86batpx0h
Signed-off-by: Tomás Castillo tcastilloboireau@gmail.com
Summary by CodeRabbit