Delete & View Notification Providers

This commit is contained in:
headlesdev 2025-05-25 00:56:54 +02:00
parent 6ba144b7bd
commit 1e93f84b24
5 changed files with 118 additions and 6 deletions

View File

@ -14,7 +14,14 @@ export async function POST(request: NextRequest) {
const body = await request.json();
const { name, type, config } = schema.parse(body);
if(type !== "TELEGRAM") {
return NextResponse.json({ error: "Invalid notification type" }, { status: 400 });
}
const parsedConfig = JSON.parse(config);
if(parsedConfig.token === "" || parsedConfig.chat_id === "") {
return NextResponse.json({ error: "Invalid config" }, { status: 400 });
}
const notification = await prisma.notificationProvider.create({
data: { name, type: type as NotificationType, config: parsedConfig},

View File

@ -3,7 +3,7 @@ import prisma from "@/app/prisma";
import { z } from "zod/v4";
const schema = z.object({
notificationId: z.number(),
notificationId: z.string(),
});
export async function DELETE(request: NextRequest) {
@ -12,7 +12,7 @@ export async function DELETE(request: NextRequest) {
try {
const notification = await prisma.notificationProvider.delete({
where: { id: notificationId.notificationId },
where: { id: parseInt(notificationId.notificationId) },
});
return NextResponse.json(notification);

View File

@ -2,11 +2,13 @@
import { useState } from 'react';
import AddNotification from '@/components/dialogues/AddNotification';
import DeleteNotification from '@/components/dialogues/DeleteNotification';
import useNotifications from '@/hooks/useNotifications';
import { Plus } from 'lucide-react';
import { Plus, Trash2, Play } from 'lucide-react';
export const NotificationSettings = ({ onError, onSuccess }: { onError: (message: string) => void, onSuccess: (message: string) => void }) => {
const { loadNotifications } = useNotifications();
const { loadNotifications, notifications } = useNotifications();
const [deleteNotificationId, setDeleteNotificationId] = useState<number | null>(null);
return (
<div>
@ -21,7 +23,37 @@ export const NotificationSettings = ({ onError, onSuccess }: { onError: (message
Add Provider
</button>
</div>
<div className='overflow-x-auto pt-4'>
<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>
</tr>
</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'>
<button className='btn btn-sm btn-success'><Play className='w-4 h-4' /></button>
<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>
</div>
<AddNotification onNotificationAdded={loadNotifications} onSuccess={onSuccess} onError={onError} />
{deleteNotificationId && (
<DeleteNotification notificationId={deleteNotificationId} onNotificationDeleted={loadNotifications} onError={onError} onSuccess={onSuccess} />
)}
</div>
</div>
);

View File

@ -0,0 +1,73 @@
"use client";
import useNotifications from "@/hooks/useNotifications";
import { Trash2, AlertTriangle } from "lucide-react";
import { useState } from "react";
interface DeleteNotificationProps {
notificationId: number;
onNotificationDeleted?: () => void;
onError?: (message: string) => void;
onSuccess?: (message: string) => void;
}
export default function DeleteNotification({ notificationId, onNotificationDeleted, onError, onSuccess }: DeleteNotificationProps) {
const { deleteNotification } = useNotifications();
const handleDelete = async () => {
const response = deleteNotification(notificationId);
if (typeof response === "string") {
onError && onError(response)
return
}
try {
const successMessage = await response
if (onNotificationDeleted && successMessage) {
onNotificationDeleted()
onSuccess && onSuccess(successMessage)
}
} catch (apiError: any) {
onError && onError(apiError)
}
}
return (
<div>
<dialog id="delete_notification" className="modal">
<div className="modal-box w-11/12 max-w-md border-l-4 border-error">
<div className="flex items-center gap-3 mb-3">
<div className="bg-error text-error-content rounded-full p-2 flex items-center justify-center">
<Trash2 className="h-6 w-6" />
</div>
<h3 className="font-bold text-xl">Delete Notification Provider</h3>
</div>
<div className="bg-base-200 p-4 rounded-lg mb-4">
<p className="text-sm">
<span className="font-medium">Are you sure you want to delete this Notification Provider?</span>
</p>
<p className="text-sm mt-2">
You will no longer receive notifications from this provider.
</p>
<div className="mt-2 flex items-center gap-2 text-error">
<AlertTriangle className="h-5 w-5" />
<span className="font-bold">This action cannot be undone.</span>
</div>
</div>
<div className="modal-action">
<form method="dialog" className="flex gap-2">
<button className="btn btn-outline">Cancel</button>
<button
className="btn btn-error text-error-content"
onClick={handleDelete}
>
Delete Notification Provider
</button>
</form>
</div>
</div>
</dialog>
</div>
)
}

View File

@ -31,8 +31,8 @@ const useNotifications = () => {
});
};
const deleteNotification = (notificationId: number) => {
axios.delete('/api/notifications/delete', { params: { notificationId } })
const deleteNotification = (notificationId: number): Promise<string> | string => {
return axios.delete('/api/notifications/delete', { params: { notificationId } })
.then(() => {
return "Notification deleted successfully";
})