Add optimistic updates for admin mutations#30
Open
Chigybillionz wants to merge 2 commits into
Open
Conversation
Lakes41
reviewed
Jun 19, 2026
Lakes41
left a comment
Contributor
There was a problem hiding this comment.
Pretty decent code, Do resolve conflicts
Author
THANK you very much! Let me do that right away!! |
Author
I have resolved the conflicts |
Author
You can check it up, I have resolved the conflicts!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Close #22
Summary of the issue
Admin mutations for assigning roles and editing access policies waited for the server response before updating the UI. This made admin actions feel slow and gave users limited feedback when saves were pending, successful, or reverted after a server rejection.
Root cause
The admin pages only invalidated React Query caches in onSuccess. They did not use mutation lifecycle snapshots, so the UI had no optimistic cache update and no reliable previous state to restore on failure.
Solution implemented
Implemented optimistic React Query mutation handling for member role assignment and policy edits. Each mutation now snapshots the relevant cache in onMutate, updates the visible cached data immediately, restores the snapshot in onError, and invalidates the query in onSettled.
Key changes made
Added shared optimistic cache helpers for members and policies.
Updated app/admin/members/page.tsx to optimistically update role assignments.
Updated app/admin/policies/page.tsx to optimistically update policy values.
Added pending “Saving” indicators plus success and rollback messages.
Preserved backend validation messages in rollback/error UI.
Added Node-based tests covering optimistic success and rollback behavior.
Fixed strict TypeScript mapper issues in lib/api/live.ts.
Added a zero-dependency npm test script.
Trade-offs or considerations
Policy edits disable policy controls while a save is pending to avoid overlapping mutations with ambiguous rollback UX. The affected row still shows a clear pending indicator.
Testing steps
Run npm install.
Run npm run typecheck.
Run npm run lint.
Run npm test.
In the admin UI, assign a role and confirm the member list updates immediately.
Edit a policy and confirm the selected value updates immediately.
Force a mutation failure and confirm the previous cached state is restored with a useful error message.
and please if there is any further correction regarding the task, please kindly let me know!!