From 9f85bbeb049cff06116819c1ab316724abdb5439 Mon Sep 17 00:00:00 2001 From: mahendar Date: Wed, 6 Nov 2024 20:46:16 +0530 Subject: [PATCH 1/2] Handling of error messages on clicking subscribe button in notification section --- src/Locale/en.json | 4 + .../Notifications/NotificationsList.tsx | 131 ++++++++++-------- 2 files changed, 78 insertions(+), 57 deletions(-) diff --git a/src/Locale/en.json b/src/Locale/en.json index 9a7f4d750c2..e6539405229 100644 --- a/src/Locale/en.json +++ b/src/Locale/en.json @@ -888,6 +888,7 @@ "notes": "Notes", "notes_placeholder": "Type your Notes", "notice_board": "Notice Board", + "notification_cancelled": "Notification cancelled", "notification_permission_denied": "Notification permission denied", "notification_permission_granted": "Notification permission granted", "number_of_aged_dependents_above_60": "Number Of Aged Dependents (Above 60)", @@ -1157,7 +1158,9 @@ "submitting": "Submitting", "subscribe": "Subscribe", "subscribe_on_this_device": "Subscribe on this device", + "subscribed_successfully": "Subscribed Successfully", "subscription_error": "Subscription Error", + "subscription_failed": "Subscription Failed", "suggested_investigations": "Suggested Investigations", "summary": "Summary", "support": "Support", @@ -1200,6 +1203,7 @@ "unlink_camera_and_bed": "Unlink this bed from this camera", "unsubscribe": "Unsubscribe", "unsubscribe_failed": "Unsubscribe failed.", + "unsubscribed_successfully": "Unsubscribed Successfully.", "unsupported_browser": "Unsupported Browser", "unsupported_browser_description": "Your browser ({{name}} version {{version}}) is not supported. Please update your browser to the latest version or switch to a supported browser for the best experience.", "up": "Up", diff --git a/src/components/Notifications/NotificationsList.tsx b/src/components/Notifications/NotificationsList.tsx index f15d5e8cb2a..9d4f9b3c3ab 100644 --- a/src/components/Notifications/NotificationsList.tsx +++ b/src/components/Notifications/NotificationsList.tsx @@ -215,30 +215,6 @@ export default function NotificationsList({ } }; }, [data, totalCount]); - useEffect(() => { - let intervalId: ReturnType; - if (isSubscribing) { - const checkNotificationPermission = () => { - if (Notification.permission === "denied") { - Warn({ - msg: t("notification_permission_denied"), - }); - setIsSubscribing(false); - clearInterval(intervalId); - } else if (Notification.permission === "granted") { - Success({ - msg: t("notification_permission_granted"), - }); - setIsSubscribing(false); - clearInterval(intervalId); - } - }; - - checkNotificationPermission(); - intervalId = setInterval(checkNotificationPermission, 1000); - } - return () => clearInterval(intervalId); - }, [isSubscribing]); const intialSubscriptionState = async () => { try { @@ -262,7 +238,14 @@ export default function NotificationsList({ const handleSubscribeClick = () => { const status = isSubscribed; if (status === "NotSubscribed" || status === "SubscribedOnAnotherDevice") { - subscribe(); + if (Notification.permission === "denied") { + Warn({ + msg: t("notification_permission_denied"), + }); + setIsSubscribing(false); + } else { + subscribe(); + } } else { unsubscribe(); } @@ -317,6 +300,10 @@ export default function NotificationsList({ body: data, }); + Warn({ + msg: t("unsubscribed_successfully"), + }); + setIsSubscribed("NotSubscribed"); setIsSubscribing(false); }) @@ -337,43 +324,73 @@ export default function NotificationsList({ async function subscribe() { setIsSubscribing(true); - const response = await request(routes.getPublicKey); - const public_key = response.data?.public_key; - const sw = await navigator.serviceWorker.ready; - const push = await sw.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: public_key, - }); - const p256dh = btoa( - String.fromCharCode.apply( - null, - new Uint8Array(push.getKey("p256dh") as any) as any, - ), - ); - const auth = btoa( - String.fromCharCode.apply( - null, - new Uint8Array(push.getKey("auth") as any) as any, - ), - ); + try { + const response = await request(routes.getPublicKey); + const public_key = response.data?.public_key; + const sw = await navigator.serviceWorker.ready; + const push = await sw.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: public_key, + }); + const p256dh = btoa( + String.fromCharCode.apply( + null, + new Uint8Array(push.getKey("p256dh") as any) as any, + ), + ); + const auth = btoa( + String.fromCharCode.apply( + null, + new Uint8Array(push.getKey("auth") as any) as any, + ), + ); - const data = { - pf_endpoint: push.endpoint, - pf_p256dh: p256dh, - pf_auth: auth, - }; + const data = { + pf_endpoint: push.endpoint, + pf_p256dh: p256dh, + pf_auth: auth, + }; - const { res } = await request(routes.updateUserPnconfig, { - pathParams: { username: username }, - body: data, - }); + const { res } = await request(routes.updateUserPnconfig, { + pathParams: { username: username }, + body: data, + }); + + if (res?.ok) { + setIsSubscribed("SubscribedOnThisDevice"); + Success({ + msg: t("subscribed_successfully"), + }); + setIsSubscribing(false); + } else { + Error({ + msg: t("subscription_failed"), + }); + setIsSubscribing(false); + } + } catch (error) { + const permission = Notification.permission; - if (res?.ok) { - setIsSubscribed("SubscribedOnThisDevice"); + if (permission === "denied") { + Warn({ + msg: t("notification_permission_denied"), + }); + setIsSubscribing(false); + return; + } else if (permission === "default") { + Warn({ + msg: t("notification_cancelled"), + }); + setIsSubscribing(false); + return; + } + Error({ + msg: t("subscription_failed"), + }); + } finally { + setIsSubscribing(false); } - setIsSubscribing(false); } - const handleMarkAllAsRead = async () => { setIsMarkingAllAsRead(true); await Promise.all( From 115d9b1fe01e7311e9ae7dd7c1af3a927620c4e3 Mon Sep 17 00:00:00 2001 From: mahendar Date: Thu, 7 Nov 2024 18:22:00 +0530 Subject: [PATCH 2/2] error handling --- src/components/Notifications/NotificationsList.tsx | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/components/Notifications/NotificationsList.tsx b/src/components/Notifications/NotificationsList.tsx index 67bcb082709..75ec9afa7bc 100644 --- a/src/components/Notifications/NotificationsList.tsx +++ b/src/components/Notifications/NotificationsList.tsx @@ -376,18 +376,12 @@ export default function NotificationsList({ } catch (error) { const permission = Notification.permission; - if (permission === "denied") { + if (permission === "denied" || permission === "default") { Warn({ msg: t("notification_permission_denied"), }); setIsSubscribing(false); return; - } else if (permission === "default") { - Warn({ - msg: t("notification_cancelled"), - }); - setIsSubscribing(false); - return; } Error({ msg: t("subscription_failed"),