2025-05-25 00:32:16 +02:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import AddNotification from '@/components/dialogues/AddNotification';
|
2025-05-25 00:56:54 +02:00
|
|
|
import DeleteNotification from '@/components/dialogues/DeleteNotification';
|
2025-05-25 21:03:37 +02:00
|
|
|
import TestNotification from '@/components/dialogues/TestNotification';
|
2025-05-25 00:32:16 +02:00
|
|
|
import useNotifications from '@/hooks/useNotifications';
|
2025-05-25 01:01:15 +02:00
|
|
|
import { Plus, Trash2, Play, Bell } from 'lucide-react';
|
2025-05-25 01:10:21 +02:00
|
|
|
import Loading from '@/components/Loading';
|
2025-05-25 00:32:16 +02:00
|
|
|
|
|
|
|
|
export const NotificationSettings = ({ onError, onSuccess }: { onError: (message: string) => void, onSuccess: (message: string) => void }) => {
|
2025-05-25 21:03:37 +02:00
|
|
|
const { loadNotifications, notifications, loading, testNotification } = useNotifications();
|
2025-05-25 00:56:54 +02:00
|
|
|
const [deleteNotificationId, setDeleteNotificationId] = useState<number | null>(null);
|
2025-05-25 00:32:16 +02:00
|
|
|
|
2025-05-25 21:03:37 +02:00
|
|
|
|
|
|
|
|
const testNotificationHandler = async (notificationProviderId: number) => {
|
|
|
|
|
(document.getElementById('test_notification') as HTMLDialogElement)?.showModal();
|
|
|
|
|
await testNotification(notificationProviderId);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 00:32:16 +02:00
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="w-full bg-base-200 p-4 rounded-2xl border border-stone-800">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div>
|
|
|
|
|
<h2 className="text-lg font-bold">Notification Settings</h2>
|
|
|
|
|
<p className="text-sm opacity-70">Manage your notification settings</p>
|
|
|
|
|
</div>
|
|
|
|
|
<button className="btn btn-primary" onClick={() => (document.getElementById('add_notification') as HTMLDialogElement)?.showModal()}>
|
|
|
|
|
<Plus className="w-5 h-5" />
|
|
|
|
|
Add Provider
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-05-25 01:10:21 +02:00
|
|
|
{loading ? (
|
|
|
|
|
<div className="flex justify-center items-center h-64 w-full">
|
|
|
|
|
<Loading />
|
|
|
|
|
</div>
|
|
|
|
|
) : notifications && notifications.length > 0 ? (
|
|
|
|
|
<div className="rounded-xl overflow-hidden border border-base-200 mt-4">
|
|
|
|
|
<div className='overflow-x-auto'>
|
|
|
|
|
<table className='table table-zebra table-pin-cols'>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>#</th>
|
|
|
|
|
<th>Name</th>
|
|
|
|
|
<th>Type</th>
|
|
|
|
|
<th className='w-24 text-center'>Actions</th>
|
2025-05-25 01:01:15 +02:00
|
|
|
</tr>
|
2025-05-25 01:10:21 +02:00
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
{notifications.map((notification: any) => (
|
|
|
|
|
<tr key={notification.id}>
|
|
|
|
|
<td>{notification.id}</td>
|
|
|
|
|
<td>{notification.name}</td>
|
|
|
|
|
<td>{notification.type}</td>
|
|
|
|
|
<td>
|
|
|
|
|
<div className='flex items-center gap-2'>
|
2025-05-25 21:03:37 +02:00
|
|
|
<button className='btn btn-sm btn-success' onClick={() => testNotificationHandler(notification.id)}><Play className='w-4 h-4' /></button>
|
2025-05-25 01:10:21 +02:00
|
|
|
<button className='btn btn-sm btn-error' onClick={() => {setDeleteNotificationId(notification.id); (document.getElementById('delete_notification') as HTMLDialogElement)?.showModal()}}><Trash2 className='w-4 h-4' /></button>
|
|
|
|
|
</div>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
))}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2025-05-25 01:01:15 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className='pt-4'>
|
|
|
|
|
<div className="flex flex-col items-center justify-center py-12 bg-base-300 rounded-xl">
|
2025-05-25 01:10:21 +02:00
|
|
|
<div className="bg-base-100 p-4 rounded-full mb-4">
|
|
|
|
|
<Bell className="h-8 w-8 text-base-content/50" />
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-base-content/70 mb-4">No notification providers have been added yet.</p>
|
2025-05-25 01:01:15 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-05-25 00:32:16 +02:00
|
|
|
<AddNotification onNotificationAdded={loadNotifications} onSuccess={onSuccess} onError={onError} />
|
2025-05-25 00:56:54 +02:00
|
|
|
{deleteNotificationId && (
|
|
|
|
|
<DeleteNotification notificationId={deleteNotificationId} onNotificationDeleted={loadNotifications} onError={onError} onSuccess={onSuccess} />
|
|
|
|
|
)}
|
2025-05-25 21:03:37 +02:00
|
|
|
<TestNotification />
|
2025-05-25 00:32:16 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|