Fix: Enable/Disable notifications directly

- Make the status switch functional to toggle notifications directly from the notification channel list.
This commit is contained in:
Tola Leng
2025-09-15 21:04:10 +07:00
parent 6887dc291b
commit 8d8e871b9f
@@ -1,6 +1,7 @@
import { AlertConfiguration } from "@/services/alertConfigService"; import { AlertConfiguration } from "@/services/alertConfigService";
import { Bell, Edit, Trash2 } from "lucide-react"; import { Bell, Edit, Trash2 } from "lucide-react";
import { useState } from "react";
import { import {
Table, Table,
TableBody, TableBody,
@@ -33,29 +34,40 @@ export const NotificationChannelList = ({
onEdit, onEdit,
onDelete onDelete
}: NotificationChannelListProps) => { }: NotificationChannelListProps) => {
const [optimisticStates, setOptimisticStates] = useState<Record<string, boolean>>({});
const toggleEnabled = async (config: CombinedChannel) => { const toggleEnabled = async (config: CombinedChannel) => {
if (!config.id) return; if (!config.id) return;
const currentEnabled = typeof config.enabled === 'string'
? config.enabled === "true" || config.enabled === "on"
: !!config.enabled;
// Apply optimistic update immediately
setOptimisticStates(prev => ({
...prev,
[config.id!]: !currentEnabled
}));
try {
if (config.isWebhook) { if (config.isWebhook) {
// Handle webhook toggle // Handle webhook toggle
try { const newEnabled = currentEnabled ? "off" : "on";
const newEnabled = config.enabled ? "off" : "on";
await pb.collection('webhook').update(config.id, { await pb.collection('webhook').update(config.id, {
enabled: newEnabled enabled: newEnabled
}); });
// Trigger refresh by calling onEdit with empty config
onEdit({} as AlertConfiguration);
} catch (error) {
console.error("Error updating webhook:", error);
}
} else { } else {
// Handle alert config toggle // Handle alert config toggle
await alertConfigService.updateAlertConfiguration(config.id, { await alertConfigService.updateAlertConfiguration(config.id, {
enabled: !config.enabled enabled: !currentEnabled
}); });
}
// The parent component will refresh the list } catch (error) {
onEdit(config as AlertConfiguration); setOptimisticStates(prev => ({
...prev,
[config.id!]: currentEnabled
}));
} }
}; };
@@ -133,7 +145,9 @@ export const NotificationChannelList = ({
<TableCell> <TableCell>
<Switch <Switch
checked={ checked={
typeof channel.enabled === 'string' channel.id && optimisticStates.hasOwnProperty(channel.id)
? optimisticStates[channel.id]
: typeof channel.enabled === 'string'
? channel.enabled === "true" || channel.enabled === "on" ? channel.enabled === "true" || channel.enabled === "on"
: !!channel.enabled : !!channel.enabled
} }
@@ -149,7 +163,7 @@ export const NotificationChannelList = ({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={() => onEdit(channel as AlertConfiguration)} onClick={() => onEdit(channel as AlertConfiguration)}
disabled={channel.isWebhook} // Disable edit for webhooks for now disabled={channel.isWebhook}
> >
<Edit className="h-4 w-4" /> <Edit className="h-4 w-4" />
</Button> </Button>