Skip to content

feat: auto-refresh Items list on WebSocket events#14

Merged
omattsson merged 2 commits intomainfrom
feature/items-auto-refresh
Mar 14, 2026
Merged

feat: auto-refresh Items list on WebSocket events#14
omattsson merged 2 commits intomainfrom
feature/items-auto-refresh

Conversation

@omattsson
Copy link
Owner

Summary

Auto-refreshes the Items list whenever item.created, item.updated, or item.deleted WebSocket events arrive, keeping the list in sync across browser tabs without requiring a manual reload.

Changes

  • frontend/src/pages/Items/index.tsx — Refactored fetchItems into a useCallback with a silent boolean parameter. WebSocket event handlers (item.created, item.updated, item.deleted) now call fetchItems({ silent: true }) so background refreshes do not surface error alerts on transient failures.
  • frontend/src/pages/Items/__tests__/Items.test.tsx — Added 10 new unit tests covering re-fetch assertions, silent-fail behaviour, UI updates triggered by events, rapid-event deduplication, and error recovery.

Bug fixes

  1. Error state not cleared on successsetError(null) was missing from the success path1. Error state not cleared on successsetError(nd 1. **Error state not cleared on success** — setError(nulls i1. **Error state noerr1. Error state not cleared on successsetError(null) was missing from the success path1. Error state not cleared on successsetError(nd 1. **Error state not cleared on success* 63 unit tests passing (cd frontend && npm test`)

Closes #7

Refactored fetchItems into a useCallback with a silent param so that
background refreshes triggered by WebSocket events do not surface error
alerts on failure. Wired item.created, item.updated, and item.deleted
event handlers to call fetchItems({ silent: true }) so the list stays
in sync across tabs without a hard reload.

Bug fixes:
- Error state was not cleared (setError(null)) on a successful fetch
- Silent background refreshes were incorrectly showing the error alert
  on transient fetch failures

Tests extended to 63 unit tests covering re-fetch assertions, silent-
fail behaviour, UI updates from events, rapid-event deduplication, and
error recovery.

Refs #7
Copilot AI review requested due to automatic review settings March 14, 2026 20:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the frontend Items page to automatically refresh its list when relevant WebSocket events arrive, keeping the UI in sync with backend changes.

Changes:

  • Refactors initial item loading into a reusable fetchItems callback and separates initial loading handling.
  • Triggers a background re-fetch on item.created, item.updated, and item.deleted WebSocket events.
  • Adds/extends Vitest coverage to validate re-fetch behavior, silent-fail behavior, and DOM updates after refreshes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
frontend/src/pages/Items/index.tsx Adds memoized fetchItems and re-fetch-on-WebSocket-event behavior.
frontend/src/pages/Items/tests/Items.test.tsx Adds tests covering event-driven re-fetching, silent refresh failures, and UI updates.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 56 to 72
useEffect(() => {
const unsubCreated = subscribe('item.created', (msg: WebSocketMessage) => {
const item = msg.payload as Item;
setToast({ open: true, message: `Item '${item.name}' created`, severity: 'success' });
void fetchItems(true);
});

const unsubUpdated = subscribe('item.updated', (msg: WebSocketMessage) => {
const item = msg.payload as Item;
setToast({ open: true, message: `Item '${item.name}' updated`, severity: 'info' });
void fetchItems(true);
});

const unsubDeleted = subscribe('item.deleted', () => {
setToast({ open: true, message: 'Item deleted', severity: 'warning' });
void fetchItems(true);
});
@omattsson omattsson merged commit 09610bb into main Mar 14, 2026
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 real-time Item event toast notifications

2 participants