Refresh notification settings in the background

React · React · advanced · modification

Adds background refresh to the notification settings page: the one-shot load moves into a reusable refreshSettings() and an interval re-fetches every 20 seconds, so changes made on another device show up without a reload. Toggling still saves immediately and renders the server's response. Tested by flipping channels in one tab and watching a second tab pick the change up on the next tick.

The account settings area of a team app. Users commonly have the page open on two devices, which is why the poll exists. The API sits behind variable mobile latency; the PATCH response body is the full updated settings record.

Requirements

Files touched

--- src/components/NotificationSettings.jsx
 import { useEffect, useState } from 'react';
+
+const POLL_INTERVAL_MS = 20000;
 
 const CHANNELS = [
 /**
  * Notification settings for the account page. The server owns the settings
- * record; the PATCH response is the full updated record.
+ * record; other devices can change it, so the page refreshes it in the
+ * background while mounted. The PATCH response is the full updated record.
  */
 export function NotificationSettings() {
   const [error, setError] = useState(null);
 
+  async function refreshSettings() {
+    try {
+      const res = await fetch('/api/notification-settings');
+      if (!res.ok) {
+        return; // transient — the next tick retries
+      }
+      const data = await res.json();
+      setSettings(data);
+    } catch {
+      // transient network failure — the next tick retries
+    }
+  }
+
   useEffect(() => {
-    async function load() {

Review this PR

React practice