diff --git a/app/api/notifications/add/route.ts b/app/api/notifications/add/route.ts index b0de94d..4f6f569 100644 --- a/app/api/notifications/add/route.ts +++ b/app/api/notifications/add/route.ts @@ -14,8 +14,10 @@ export async function POST(request: NextRequest) { const body = await request.json(); const { name, type, config } = schema.parse(body); + const parsedConfig = JSON.parse(config); + const notification = await prisma.notificationProvider.create({ - data: { name, type: type as NotificationType, config, tests: {} }, + data: { name, type: type as NotificationType, config: parsedConfig}, }); return NextResponse.json({ notification }, { status: 201 }); diff --git a/app/dashboard/settings/SettingsPage.tsx b/app/dashboard/settings/SettingsPage.tsx index 9f05e17..ac07af7 100644 --- a/app/dashboard/settings/SettingsPage.tsx +++ b/app/dashboard/settings/SettingsPage.tsx @@ -2,8 +2,10 @@ import Sidebar from '@/components/Sidebar'; import ErrorToast from '@/components/Error'; +import SuccessToast from '@/components/Success'; import { ProfileSettings } from '@/components/cards/settings/ProfileSettings'; import { PasswordSettings } from '@/components/cards/settings/PasswordSettings'; +import { NotificationSettings } from '@/components/cards/settings/NotificationSettings'; import { useState } from 'react'; interface SettingsPageProps { @@ -14,7 +16,7 @@ interface SettingsPageProps { export default function SettingsPage({ username, name, email }: SettingsPageProps) { const [error, setError] = useState(''); - + const [success, setSuccess] = useState(''); return (
@@ -57,13 +59,14 @@ export default function SettingsPage({ username, name, email }: SettingsPageProp
- test +
setError('')} /> + setSuccess('')} /> ); } \ No newline at end of file diff --git a/components/cards/settings/NotificationSettings.tsx b/components/cards/settings/NotificationSettings.tsx new file mode 100644 index 0000000..3cba260 --- /dev/null +++ b/components/cards/settings/NotificationSettings.tsx @@ -0,0 +1,28 @@ +'use client'; + +import { useState } from 'react'; +import AddNotification from '@/components/dialogues/AddNotification'; +import useNotifications from '@/hooks/useNotifications'; +import { Plus } from 'lucide-react'; + +export const NotificationSettings = ({ onError, onSuccess }: { onError: (message: string) => void, onSuccess: (message: string) => void }) => { + const { loadNotifications } = useNotifications(); + + return ( +
+
+
+
+

Notification Settings

+

Manage your notification settings

+
+ +
+ +
+
+ ); +}; \ No newline at end of file diff --git a/components/dialogues/AddNotification.tsx b/components/dialogues/AddNotification.tsx new file mode 100644 index 0000000..65b88fb --- /dev/null +++ b/components/dialogues/AddNotification.tsx @@ -0,0 +1,104 @@ +"use client"; + +import { useState } from "react"; +import useNotification from "@/hooks/useNotifications"; +import { Bell, Text, BellRing, Braces } from "lucide-react"; + +interface AddNotificationProps { + onNotificationAdded?: () => void; + onError?: (message: string) => void; + onSuccess?: (message: string) => void; +} + +export default function AddNotification({ onNotificationAdded, onError, onSuccess }: AddNotificationProps) { + const [name, setName] = useState(""); + const [type, setType] = useState("Select a type"); + const [telegramBotToken, setTelegramBotToken] = useState(""); + const [telegramChatId, setTelegramChatId] = useState(""); + + const { addNotification } = useNotification(); + + const addNotificationHandler = async () => { + const config = type === "TELEGRAM" ? `{ "token": "${telegramBotToken}", "chat_id": "${telegramChatId}" }` : ""; + const response = addNotification(name, type, config); + if (typeof response === "string") { + onError && onError(response) + return + } + try { + const successMessage = await response + if (onSuccess && successMessage) { + onSuccess('Notification Provider added successfully') + onNotificationAdded && onNotificationAdded() + } + } catch (apiError: any) { + onError && onError(apiError) + } finally { + setName("") + setType("Select a type") + setTelegramBotToken("") + setTelegramChatId("") + } + } + + return ( +
+ +
+
+
+ +
+

Add Notification

+
+ +
+
+
+
+ +
+ setName(e.target.value)} /> +
+
+
+ +
+ +
+ {type === "TELEGRAM" && ( +
+
+ +
+
+ setTelegramBotToken(e.target.value)} /> + setTelegramChatId(e.target.value)} /> +
+
+ )} +
+
+
+
+ + +
+
+
+
+
+ ) +} \ No newline at end of file diff --git a/components/dialogues/AddSite.tsx b/components/dialogues/AddSite.tsx index 0397273..d93def7 100644 --- a/components/dialogues/AddSite.tsx +++ b/components/dialogues/AddSite.tsx @@ -1,7 +1,6 @@ "use client"; import { useState } from "react"; -import axios from "axios"; import ErrorToast from "@/components/Error"; import SuccessToast from "@/components/Success"; import { Building2, MapPin, FileText } from "lucide-react"; diff --git a/hooks/useNotifications.ts b/hooks/useNotifications.ts index 27f112c..83d2936 100644 --- a/hooks/useNotifications.ts +++ b/hooks/useNotifications.ts @@ -25,6 +25,9 @@ const useNotifications = () => { return axios.post('/api/notifications/add', { name, type, config }) .then((response) => { return response.data.notification; + }) + .catch(err => { + throw err.response?.data?.error || 'An error occurred'; }); }; @@ -38,5 +41,13 @@ const useNotifications = () => { }); } - return { notifications, loading, addNotification, deleteNotification }; -} \ No newline at end of file + return { + notifications, + loading, + addNotification, + deleteNotification, + loadNotifications + }; +} + +export default useNotifications; \ No newline at end of file