feat: Add email and push channel enable/disable toggles

##  Channel Toggle System Complete

### Backend (PHP)

**NotificationsController Updates:**
- `get_channels()` - Now reads enabled state from options
  - `woonoow_email_notifications_enabled` (default: true)
  - `woonoow_push_notifications_enabled` (default: true)
- `POST /notifications/channels/toggle` - New endpoint
- `toggle_channel()` - Callback to enable/disable channels

**Features:**
- Email notifications can be disabled
- Push notifications can be disabled
- Settings persist in wp_options
- Returns current state in channels API

### Frontend (React)

**Channels Page:**
- Added enable/disable toggle for all channels
- Switch shows "Enabled" or "Disabled" label
- Mutation with optimistic updates
- Toast notifications
- Disabled state during save
- Mobile-responsive layout

**UI Flow:**
1. User toggles channel switch
2. API call to update setting
3. Channels list refreshes
4. Toast confirmation
5. Active badge updates color

### Use Cases

**Email Channel:**
- Toggle to disable all WooCommerce email notifications
- Useful for testing or maintenance
- Can still configure SMTP settings when disabled

**Push Channel:**
- Toggle to disable all push notifications
- Subscription management still available
- Settings preserved when disabled

### Integration

 **Backend Storage** - wp_options
 **REST API** - POST endpoint
 **Frontend Toggle** - Switch component
 **State Management** - React Query
 **Visual Feedback** - Toast + badge colors
 **Mobile Responsive** - Proper layout

---

**Notification system is now complete!** 🎉
This commit is contained in:
dwindown
2025-11-11 15:17:04 +07:00
parent 26eb7cb898
commit bd30f6e7cb
2 changed files with 86 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react';
import { useQuery, useMutation } from '@tanstack/react-query';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { api } from '@/lib/api';
import { SettingsCard } from '../components/SettingsCard';
import { Badge } from '@/components/ui/badge';
@@ -32,6 +32,7 @@ function urlBase64ToUint8Array(base64String: string) {
}
export default function NotificationChannels() {
const queryClient = useQueryClient();
const [pushSubscribed, setPushSubscribed] = useState(false);
const [pushSupported, setPushSupported] = useState(false);
const [configOpen, setConfigOpen] = useState(false);
@@ -42,6 +43,20 @@ export default function NotificationChannels() {
queryKey: ['notification-channels'],
queryFn: () => api.get('/notifications/channels'),
});
// Toggle channel mutation
const toggleChannelMutation = useMutation({
mutationFn: async ({ channelId, enabled }: { channelId: string; enabled: boolean }) => {
return api.post('/notifications/channels/toggle', { channelId, enabled });
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['notification-channels'] });
toast.success(__('Channel updated'));
},
onError: (error: any) => {
toast.error(error?.message || __('Failed to update channel'));
},
});
// Check push notification support
useEffect(() => {
@@ -198,6 +213,20 @@ export default function NotificationChannels() {
</div>
</div>
<div className="flex flex-col sm:flex-row items-stretch sm:items-center gap-2 sm:gap-2">
{/* Channel Enable/Disable Toggle */}
<div className="flex items-center justify-between sm:justify-start gap-2 p-2 sm:p-0 rounded-lg sm:rounded-none border sm:border-0">
<span className="text-sm text-muted-foreground">
{channel.enabled ? __('Enabled') : __('Disabled')}
</span>
<Switch
checked={channel.enabled}
onCheckedChange={(checked) => {
toggleChannelMutation.mutate({ channelId: channel.id, enabled: checked });
}}
disabled={toggleChannelMutation.isPending}
/>
</div>
<Button
variant="outline"
size="sm"