From b4dfb953fa1a2c538670e89837878e268e2bc397 Mon Sep 17 00:00:00 2001 From: Alberto Espinoza Date: Fri, 22 Sep 2023 12:36:11 -0600 Subject: [PATCH 1/2] R2-2619 - Front react to 404 errors --- .../push-notifications-toggle/component.jsx | 6 ++++++ app/javascript/config/constants.js | 3 ++- app/javascript/libs/service-worker-utils.js | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/javascript/components/push-notifications-toggle/component.jsx b/app/javascript/components/push-notifications-toggle/component.jsx index 13b1322a3f..2a49322aa3 100644 --- a/app/javascript/components/push-notifications-toggle/component.jsx +++ b/app/javascript/components/push-notifications-toggle/component.jsx @@ -17,6 +17,7 @@ import { saveNotificationSubscription } from "../user"; import ConditionalTooltip from "../conditional-tooltip"; +import { enqueueSnackbar } from "../notifier"; import css from "./styles.css"; @@ -93,6 +94,11 @@ function Component() { if (event?.data?.type === POST_MESSAGES.DISPATCH_SAVE_SUBSCRIPTION) { dispatch(saveNotificationSubscription(event?.data?.endpoint)); } + + if (event?.data?.type === POST_MESSAGES.ATTEMPTS_SUBSCRIPTION_FAILED) { + setValue(false); + dispatch(enqueueSnackbar("Attempts subscription failed")); + } }; useEffect(() => { diff --git a/app/javascript/config/constants.js b/app/javascript/config/constants.js index dcf45a5cd5..51a15c1951 100644 --- a/app/javascript/config/constants.js +++ b/app/javascript/config/constants.js @@ -746,5 +746,6 @@ export const POST_MESSAGES = { SUBSCRIBE_NOTIFICATIONS: "subscribe_notifications", UNSUBSCRIBE_NOTIFICATIONS: "unsubscribe_notifications", DISPATCH_REMOVE_SUBSCRIPTION: "dispatch_remove_subscription", - DISPATCH_SAVE_SUBSCRIPTION: "dispatch_save_subscription" + DISPATCH_SAVE_SUBSCRIPTION: "dispatch_save_subscription", + ATTEMPTS_SUBSCRIPTION_FAILED: "attempts_subscription_failed" }; diff --git a/app/javascript/libs/service-worker-utils.js b/app/javascript/libs/service-worker-utils.js index e484804506..e02df432f2 100644 --- a/app/javascript/libs/service-worker-utils.js +++ b/app/javascript/libs/service-worker-utils.js @@ -4,6 +4,8 @@ import getToken from "../middleware/utils/get-token"; import DB, { DB_STORES } from "../db"; const SERVICE_WORKER_PATH = `${window.location.origin}/worker.js`; +const MAX_ATTEMPS = 3; +let attemps = 1; async function getServiceWorker() { return navigator.serviceWorker.ready; @@ -63,6 +65,21 @@ async function sendSubscriptionStatusToServer(isSubscribing = true, data = {}) { type: POST_MESSAGES.DISPATCH_REMOVE_SUBSCRIPTION }); } + + if (isSubscribing && response.status === 404) { + if (attemps % MAX_ATTEMPS !== 0) { + postMessage({ + type: POST_MESSAGES.SUBSCRIBE_NOTIFICATIONS + }); + } + + if (attemps % MAX_ATTEMPS === 0) { + postMessage({ + type: POST_MESSAGES.ATTEMPTS_SUBSCRIPTION_FAILED + }); + } + attemps += 1; + } } async function subscribe() { From 813c125a0207bb3ac44655694c3e2028d05b5fd3 Mon Sep 17 00:00:00 2001 From: Alberto Espinoza Date: Fri, 22 Sep 2023 14:41:21 -0600 Subject: [PATCH 2/2] R2-2619 - Fix typo --- app/javascript/libs/service-worker-utils.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/javascript/libs/service-worker-utils.js b/app/javascript/libs/service-worker-utils.js index e02df432f2..469d5c52c7 100644 --- a/app/javascript/libs/service-worker-utils.js +++ b/app/javascript/libs/service-worker-utils.js @@ -4,8 +4,8 @@ import getToken from "../middleware/utils/get-token"; import DB, { DB_STORES } from "../db"; const SERVICE_WORKER_PATH = `${window.location.origin}/worker.js`; -const MAX_ATTEMPS = 3; -let attemps = 1; +const MAX_ATTEMPTS = 3; +let attempts = 1; async function getServiceWorker() { return navigator.serviceWorker.ready; @@ -67,18 +67,18 @@ async function sendSubscriptionStatusToServer(isSubscribing = true, data = {}) { } if (isSubscribing && response.status === 404) { - if (attemps % MAX_ATTEMPS !== 0) { + if (attempts % MAX_ATTEMPTS !== 0) { postMessage({ type: POST_MESSAGES.SUBSCRIBE_NOTIFICATIONS }); } - if (attemps % MAX_ATTEMPS === 0) { + if (attempts % MAX_ATTEMPTS === 0) { postMessage({ type: POST_MESSAGES.ATTEMPTS_SUBSCRIPTION_FAILED }); } - attemps += 1; + attempts += 1; } }