Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the handleTestNotification function. #1904

Merged
merged 24 commits into from
Mar 19, 2025
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7b1a60d
Implemented the handleTestNotification function.
Skorpios604 Mar 10, 2025
845b769
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 Mar 11, 2025
9aa0041
Used the network service class for network requests.
Skorpios604 Mar 12, 2025
b811fb9
Use explicit type checking.
Skorpios604 Mar 12, 2025
f7b247f
Error handling and a more safe approach for the getFieldKey function.
Skorpios604 Mar 12, 2025
798a680
Got rid of magic numbers in the modal.
Skorpios604 Mar 12, 2025
8e026ed
Refactored out constant keys.
Skorpios604 Mar 12, 2025
f735b08
Removed comment
Skorpios604 Mar 12, 2025
de8edbf
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 Mar 12, 2025
b1194eb
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 Mar 16, 2025
00700c8
Return an array rather than an object.
Skorpios604 Mar 16, 2025
8994b4b
Use undefined instead of null for conventional purposes.
Skorpios604 Mar 16, 2025
182c513
Abort early if the typeId or fieldId are not present.
Skorpios604 Mar 16, 2025
910defb
Used the theme for the circular progress.
Skorpios604 Mar 16, 2025
4cbcebb
Used theme for sizes and margins.
Skorpios604 Mar 16, 2025
13b8bb6
Used theme for colors.
Skorpios604 Mar 16, 2025
caab2c3
Removed fallback strings.
Skorpios604 Mar 16, 2025
c84a547
Extracted string to use i18n implementation.
Skorpios604 Mar 16, 2025
3136bdb
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 Mar 17, 2025
840690d
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 Mar 17, 2025
100c5d3
Use built-in loading prop for save button.
Skorpios604 Mar 17, 2025
9462632
Remove fallback string from save button.
Skorpios604 Mar 17, 2025
a0920f9
Resolve merge conflict: Remove src/locales/en.json
Skorpios604 Mar 18, 2025
51da6b1
Updated the new locales file with the proper translation keys.
Skorpios604 Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useState, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { toast } from 'react-toastify';

import {
Dialog,
DialogContent,
Expand Down Expand Up @@ -129,11 +131,87 @@ const NotificationIntegrationModal = ({
}));
};

const handleTestNotification = (type) => {
console.log(`Testing ${type} notification`);
//implement the test notification functionality
};
const handleTestNotification = async (type) => {
// Get the notification type details
const notificationType = activeNotificationTypes.find(t => t.id === type);

if (notificationType === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use typeof === "undefined", this is a safer way to check for undefined and is the standard for this app

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return;
}

// Helper to get the field state key
const getFieldKey = (typeId, fieldId) => {
return `${typeId}${fieldId.charAt(0).toUpperCase() + fieldId.slice(1)}`;
};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will throw uncaught errors under many conditions. typeId or fieldId undefined, null, not a string etc.

Let's make this safer and catch errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// Prepare request payload based on notification type
let payload = { platform: type };

switch(type) {
case 'slack':
payload.webhookUrl = integrations[getFieldKey('slack', 'webhook')];
if (!payload.webhookUrl) {
toast.error('Please enter a Slack webhook URL first.');
return;
}
break;

case 'discord':
payload.webhookUrl = integrations[getFieldKey('discord', 'webhook')];
if (!payload.webhookUrl) {
toast.error('Please enter a Discord webhook URL first.');
return;
}
break;

case 'telegram':
payload.botToken = integrations[getFieldKey('telegram', 'token')];
payload.chatId = integrations[getFieldKey('telegram', 'chatId')];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant keys should be refactored out

const TELEGRAM_FIELD = "telegram"

etc. Always best to avoid magic values

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (!payload.botToken || !payload.chatId) {
toast.error('Please enter both Telegram bot token and chat ID.');
return;
}
break;

case 'webhook':
payload.webhookUrl = integrations[getFieldKey('webhook', 'url')];
payload.platform = 'slack'; // Use slack as platform for webhooks
if (!payload.webhookUrl) {
toast.error('Please enter a webhook URL first.');
return;
}
break;

default:
toast.error('This notification type cannot be tested.');
return;
}

try {
const apiUrl = `${import.meta.env.VITE_APP_API_BASE_URL || 'http://localhost:5000'}/api/v1/notifications/test-webhook`;


const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload),
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Netwrok request should be made through the NetworkService class using the configured Axios instance.

They should also be carried out via a hook that provides a loading state and an error state. Please see other components/pages that make network requests for examples

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


const data = await response.json();

if (response.ok && data.success) {
toast.success('Test notification sent successfully!');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User facing strings need to go through the il8n implementation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

} else {
throw new Error(data.msg || 'Failed to send test notification');
}
} catch (error) {
toast.error(`Failed to send test notification: ${error.message}`);
}
};


const handleSave = () => {
//notifications array for selected integrations
const notifications = [...(monitor?.notifications || [])];
Expand Down