-
Notifications
You must be signed in to change notification settings - Fork 267
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
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7b1a60d
Implemented the handleTestNotification function.
Skorpios604 845b769
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 9aa0041
Used the network service class for network requests.
Skorpios604 b811fb9
Use explicit type checking.
Skorpios604 f7b247f
Error handling and a more safe approach for the getFieldKey function.
Skorpios604 798a680
Got rid of magic numbers in the modal.
Skorpios604 8e026ed
Refactored out constant keys.
Skorpios604 f735b08
Removed comment
Skorpios604 de8edbf
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 b1194eb
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 00700c8
Return an array rather than an object.
Skorpios604 8994b4b
Use undefined instead of null for conventional purposes.
Skorpios604 182c513
Abort early if the typeId or fieldId are not present.
Skorpios604 910defb
Used the theme for the circular progress.
Skorpios604 4cbcebb
Used theme for sizes and margins.
Skorpios604 13b8bb6
Used theme for colors.
Skorpios604 caab2c3
Removed fallback strings.
Skorpios604 c84a547
Extracted string to use i18n implementation.
Skorpios604 3136bdb
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 840690d
Merge remote-tracking branch 'upstream/develop' into feat/handleTestN…
Skorpios604 100c5d3
Use built-in loading prop for save button.
Skorpios604 9462632
Remove fallback string from save button.
Skorpios604 a0920f9
Resolve merge conflict: Remove src/locales/en.json
Skorpios604 51da6b1
Updated the new locales file with the proper translation keys.
Skorpios604 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
119 changes: 119 additions & 0 deletions
119
src/Components/NotificationIntegrationModal/Hooks/useNotification.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { useState } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
import { useTranslation } from "react-i18next"; | ||
import { networkService } from '../../../Utils/NetworkService'; | ||
|
||
// Define constants for notification types to avoid magic values | ||
const NOTIFICATION_TYPES = { | ||
SLACK: 'slack', | ||
DISCORD: 'discord', | ||
TELEGRAM: 'telegram', | ||
WEBHOOK: 'webhook' | ||
}; | ||
|
||
// Define constants for field IDs | ||
const FIELD_IDS = { | ||
WEBHOOK: 'webhook', | ||
TOKEN: 'token', | ||
CHAT_ID: 'chatId', | ||
URL: 'url' | ||
}; | ||
|
||
/** | ||
* Custom hook for notification-related operations | ||
*/ | ||
const useNotifications = () => { | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(undefined); | ||
const { t } = useTranslation(); | ||
|
||
/** | ||
* Send a test notification | ||
* @param {string} type - The notification type (slack, discord, telegram, webhook) | ||
* @param {object} config - Configuration object with necessary params | ||
*/ | ||
const sendTestNotification = async (type, config) => { | ||
setLoading(true); | ||
setError(undefined); | ||
|
||
// Validation based on notification type | ||
let payload = { platform: type }; | ||
let isValid = true; | ||
let errorMessage = ''; | ||
|
||
switch(type) { | ||
case NOTIFICATION_TYPES.SLACK: | ||
payload.webhookUrl = config.webhook; | ||
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') { | ||
isValid = false; | ||
errorMessage = t('notifications.slack.webhookRequired'); | ||
} | ||
break; | ||
|
||
case NOTIFICATION_TYPES.DISCORD: | ||
payload.webhookUrl = config.webhook; | ||
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') { | ||
isValid = false; | ||
errorMessage = t('notifications.discord.webhookRequired'); | ||
} | ||
break; | ||
|
||
case NOTIFICATION_TYPES.TELEGRAM: | ||
payload.botToken = config.token; | ||
payload.chatId = config.chatId; | ||
if (typeof payload.botToken === 'undefined' || payload.botToken === '' || | ||
typeof payload.chatId === 'undefined' || payload.chatId === '') { | ||
isValid = false; | ||
errorMessage = t('notifications.telegram.fieldsRequired'); | ||
} | ||
break; | ||
|
||
case NOTIFICATION_TYPES.WEBHOOK: | ||
payload.webhookUrl = config.url; | ||
payload.platform = NOTIFICATION_TYPES.SLACK; | ||
if (typeof payload.webhookUrl === 'undefined' || payload.webhookUrl === '') { | ||
isValid = false; | ||
errorMessage = t('notifications.webhook.urlRequired'); | ||
} | ||
break; | ||
|
||
default: | ||
isValid = false; | ||
errorMessage = t('notifications.unsupportedType'); | ||
} | ||
|
||
// If validation fails, show error and return | ||
if (isValid === false) { | ||
toast.error(errorMessage); | ||
setLoading(false); | ||
return; | ||
} | ||
|
||
try { | ||
const response = await networkService.testNotification({ | ||
platform: type, | ||
payload: payload | ||
}); | ||
|
||
if (response.data.success === true) { | ||
toast.success(t('notifications.testSuccess')); | ||
} else { | ||
throw new Error(response.data.msg || t('notifications.testFailed')); | ||
} | ||
} catch (error) { | ||
const errorMsg = error.response?.data?.msg || error.message || t('notifications.networkError'); | ||
toast.error(`${t('notifications.testFailed')}: ${errorMsg}`); | ||
setError(errorMsg); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return [ | ||
loading, | ||
error, | ||
sendTestNotification | ||
]; | ||
}; | ||
|
||
export default useNotifications; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
New isLoading prop added to component signature
You've added the isLoading prop to the component parameters, which is used throughout the component to disable interactions during async operations.
Consider adding PropTypes validation for all props to satisfy the ESLint warnings:
🏁 Script executed:
Length of output: 1148
Action Required: Add missing PropTypes validation in TabComponent.jsx
The new isLoading prop is now being used correctly, but our verification shows that the PropTypes import and corresponding prop types definition are absent. To satisfy ESLint warnings, please add the following changes:
His palms are sweaty, knees weak, arms are heavy—let’s fix this before we call it good to go!
🧰 Tools
🪛 ESLint
[error] 18-18: 'handleTestNotification' is missing in props validation
(react/prop-types)
[error] 19-19: 'isLoading' is missing in props validation
(react/prop-types)