Skip to content

[*] refactor EnabledSourceSwitch#1319

Open
Mazen050 wants to merge 1 commit intocybertec-postgresql:masterfrom
Mazen050:fix/enabled-toggle
Open

[*] refactor EnabledSourceSwitch#1319
Mazen050 wants to merge 1 commit intocybertec-postgresql:masterfrom
Mazen050:fix/enabled-toggle

Conversation

@Mazen050
Copy link
Contributor

Description

Fixes #
The enabled toggle doesn’t update after toggling enable/disable in the edit form and clicking Update.
Cause: useState(source.IsEnabled) only initializes once, so it gets out of sync with server updates.

The component used:

const [checked, setChecked] = useState(source.IsEnabled);

This initializes state only on first render, so when new server data arrives, checked does not update.

The previous useEffect workaround synced state after render but Introduced an extra render cycle

The solution is to remove local state and derive the value directly from mutation:

const checked = editEnabled.isPending ? !source.IsEnabled : source.IsEnabled;

this shows optimistic toggle (!source.IsEnabled) while mutation is pending and after success reflects updated server value amd on error reverts automatically to original value

No extra renders are required with this aproach and no stale states. also keeps UI in sync with server.

AI & Automation Policy

  • I am the human author and take full personal responsibility for every change in this PR.
  • No AI or automated generative tool was used in any part of this PR OR I have disclosed all tool(s) below.

AI/automation tools used (leave blank if none):
claude sonnet 4.6 helped in debuging and was used to review

Checklist

  • Code compiles and existing tests pass locally.
  • New or updated tests are included where applicable.
  • Documentation is updated where applicable.

@Mazen050 Mazen050 force-pushed the fix/enabled-toggle branch from f6753df to 6ea6d34 Compare March 20, 2026 14:59
@Mazen050 Mazen050 force-pushed the fix/enabled-toggle branch from 6ea6d34 to 310ac1e Compare March 20, 2026 15:09
@mohamadyasser118
Copy link
Contributor

Hello, You can consider making the switch disabled while the request is running (editEnabled.isPending) so the action doesn’t happen twice by adding disabled={editEnabled.isPending} to the <Switch>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants