dwindown
3ef5087f09
fix: Critical data structure and mutation bugs
## 🐛 Critical Fixes
### Issue 1: Toggling One Channel Affects Both
**Problem:** Disabling email disabled both email and push
**Root Cause:** Optimistic update with `onSettled` refetch caused race condition
**Fix:** Removed optimistic update, use server response directly
**Before:**
```ts
onMutate: async () => {
// Optimistic update
queryClient.setQueryData(...)
}
onSettled: () => {
// This refetch caused race condition
queryClient.invalidateQueries(...)
}
```
**After:**
```ts
onSuccess: (data, variables) => {
// Update cache with verified server response
queryClient.setQueryData([...], (old) =>
old.map(channel =>
channel.id === variables.channelId
? { ...channel, enabled: data.enabled }
: channel
)
);
}
```
### Issue 2: Events Cannot Be Enabled
**Problem:** All event channels disabled and cannot be enabled
**Root Cause:** Wrong data structure in `update_event()`
**Before:**
```php
$settings[$event_id][$channel_id] = [...];
// Saved as: { "order_placed": { "email": {...} } }
```
**After:**
```php
$settings[$event_id]['channels'][$channel_id] = [...];
// Saves as: { "order_placed": { "channels": { "email": {...} } } }
```
### Issue 3: POST Data Not Parsed
**Problem:** Event updates not working
**Root Cause:** Using `get_param()` instead of `get_json_params()`
**Fix:** Changed to `get_json_params()` in `update_event()`
### What Was Fixed
1. ✅ Channel toggles work independently
2. ✅ No race conditions from optimistic updates
3. ✅ Event channel data structure matches get_events
4. ✅ Event toggles save correctly
5. ✅ POST data parsed properly
6. ✅ Boolean type enforcement
### Data Structure
**Correct Structure:**
```php
[
'order_placed' => [
'channels' => [
'email' => ['enabled' => true, 'recipient' => 'admin'],
'push' => ['enabled' => false, 'recipient' => 'admin']
]
]
]
```
---
**All toggles should now work correctly!** ✅
2025-11-11 16:05:21 +07:00
..
2025-11-11 10:43:03 +07:00
2025-11-05 10:02:40 +07:00
2025-11-10 12:23:44 +07:00
2025-11-10 22:41:18 +07:00
2025-11-09 23:44:24 +07:00
2025-11-11 16:05:21 +07:00
2025-11-10 18:56:41 +07:00
2025-11-06 14:05:18 +07:00
2025-11-04 18:08:00 +07:00
2025-11-09 23:44:24 +07:00
2025-11-11 12:11:08 +07:00
2025-11-10 10:16:51 +07:00
2025-11-11 10:12:30 +07:00
2025-11-10 22:41:18 +07:00
2025-11-10 14:09:52 +07:00