From 1e93f84b242d3ba872cc65659a135aff5933d20b Mon Sep 17 00:00:00 2001 From: headlesdev Date: Sun, 25 May 2025 00:56:54 +0200 Subject: [PATCH] Delete & View Notification Providers --- app/api/notifications/add/route.ts | 7 ++ app/api/notifications/delete/route.ts | 4 +- .../cards/settings/NotificationSettings.tsx | 36 ++++++++- components/dialogues/DeleteNotification.tsx | 73 +++++++++++++++++++ hooks/useNotifications.ts | 4 +- 5 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 components/dialogues/DeleteNotification.tsx diff --git a/app/api/notifications/add/route.ts b/app/api/notifications/add/route.ts index 4f6f569..ea3b3d6 100644 --- a/app/api/notifications/add/route.ts +++ b/app/api/notifications/add/route.ts @@ -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}, diff --git a/app/api/notifications/delete/route.ts b/app/api/notifications/delete/route.ts index 57b5078..7e1c3ff 100644 --- a/app/api/notifications/delete/route.ts +++ b/app/api/notifications/delete/route.ts @@ -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); diff --git a/components/cards/settings/NotificationSettings.tsx b/components/cards/settings/NotificationSettings.tsx index 3cba260..f19e86d 100644 --- a/components/cards/settings/NotificationSettings.tsx +++ b/components/cards/settings/NotificationSettings.tsx @@ -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(null); return (
@@ -21,7 +23,37 @@ export const NotificationSettings = ({ onError, onSuccess }: { onError: (message Add Provider
+
+ + + + + + + + + + + {notifications.map((notification: any) => ( + + + + + + + ))} + +
#NameTypeActions
{notification.id}{notification.name}{notification.type} +
+ + +
+
+
+ {deleteNotificationId && ( + + )} ); diff --git a/components/dialogues/DeleteNotification.tsx b/components/dialogues/DeleteNotification.tsx new file mode 100644 index 0000000..70385fd --- /dev/null +++ b/components/dialogues/DeleteNotification.tsx @@ -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 ( +
+ +
+
+
+ +
+

Delete Notification Provider

+
+ +
+

+ Are you sure you want to delete this Notification Provider? +

+

+ You will no longer receive notifications from this provider. +

+
+ + This action cannot be undone. +
+
+ +
+
+ + +
+
+
+
+
+ ) +} \ No newline at end of file diff --git a/hooks/useNotifications.ts b/hooks/useNotifications.ts index 83d2936..f1eafa0 100644 --- a/hooks/useNotifications.ts +++ b/hooks/useNotifications.ts @@ -31,8 +31,8 @@ const useNotifications = () => { }); }; - const deleteNotification = (notificationId: number) => { - axios.delete('/api/notifications/delete', { params: { notificationId } }) + const deleteNotification = (notificationId: number): Promise | string => { + return axios.delete('/api/notifications/delete', { params: { notificationId } }) .then(() => { return "Notification deleted successfully"; })