fix: Channel toggle not working and multiple API calls
## 🐛 Bug Fixes ### Issue 1: Toggle Not Saving **Problem:** Channel toggle always returned `enabled: true` **Root Cause:** Backend using `get_param()` instead of `get_json_params()` **Fix:** Updated `toggle_channel()` to properly parse JSON POST data **Backend Changes:** ```php // Before $channel_id = $request->get_param('channelId'); $enabled = $request->get_param('enabled'); // After $params = $request->get_json_params(); $channel_id = isset($params['channelId']) ? $params['channelId'] : null; $enabled = isset($params['enabled']) ? $params['enabled'] : null; ``` ### Issue 2: Multiple API Calls (3x) **Problem:** Single toggle triggered 3 requests **Root Cause:** Query invalidation causing immediate refetch **Fix:** Implemented optimistic updates with rollback **Frontend Changes:** - ✅ `onMutate` - Cancel pending queries + optimistic update - ✅ `onSuccess` - Show toast only - ✅ `onError` - Rollback + show error - ✅ `onSettled` - Refetch to sync with server **Request Flow:** ``` Before: Toggle → API call → Invalidate → Refetch (3 requests) After: Toggle → Optimistic update → API call → Refetch (2 requests) ``` ### Benefits 1. **Instant UI feedback** - Toggle responds immediately 2. **Fewer API calls** - Reduced from 3 to 2 requests 3. **Error handling** - Automatic rollback on failure 4. **Better UX** - No flickering or delays ### Testing - [x] Toggle email channel on/off - [x] Toggle push channel on/off - [x] Verify state persists on reload - [x] Check network tab for request count - [x] Test error handling (disconnect network) --- **Channel toggles now work correctly!** ✅
This commit is contained in:
@@ -553,8 +553,9 @@ class NotificationsController {
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function toggle_channel(WP_REST_Request $request) {
|
||||
$channel_id = $request->get_param('channelId');
|
||||
$enabled = $request->get_param('enabled');
|
||||
$params = $request->get_json_params();
|
||||
$channel_id = isset($params['channelId']) ? $params['channelId'] : null;
|
||||
$enabled = isset($params['enabled']) ? $params['enabled'] : null;
|
||||
|
||||
if (empty($channel_id)) {
|
||||
return new WP_Error(
|
||||
@@ -564,6 +565,14 @@ class NotificationsController {
|
||||
);
|
||||
}
|
||||
|
||||
if ($enabled === null) {
|
||||
return new WP_Error(
|
||||
'invalid_enabled',
|
||||
__('Enabled parameter is required', 'woonoow'),
|
||||
['status' => 400]
|
||||
);
|
||||
}
|
||||
|
||||
// Only allow toggling built-in channels
|
||||
if ($channel_id === 'email') {
|
||||
update_option('woonoow_email_notifications_enabled', (bool) $enabled);
|
||||
|
||||
Reference in New Issue
Block a user