Skip to content

Add optimistic updates for admin mutations#30

Open
Chigybillionz wants to merge 2 commits into
Adamantine-guild:mainfrom
Chigybillionz:optimistic-update
Open

Add optimistic updates for admin mutations#30
Chigybillionz wants to merge 2 commits into
Adamantine-guild:mainfrom
Chigybillionz:optimistic-update

Conversation

@Chigybillionz

Copy link
Copy Markdown

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!!

@Lakes41 Lakes41 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.

Pretty decent code, Do resolve conflicts

@Chigybillionz

Copy link
Copy Markdown
Author

Pretty decent code, Do resolve conflicts

THANK you very much! Let me do that right away!!

@Chigybillionz

Copy link
Copy Markdown
Author

Pretty decent code, Do resolve conflicts

I have resolved the conflicts

@Chigybillionz Chigybillionz requested a review from Lakes41 June 20, 2026 09:55
@Chigybillionz

Copy link
Copy Markdown
Author

Pretty decent code, Do resolve conflicts

You can check it up, I have resolved the conflicts!!

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.

Add optimistic updates for admin mutations

2 participants