Skip to content

Commit

Permalink
R2-2619 - Front react to 404 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aespinoza-quoin committed Sep 22, 2023
1 parent 8a851b5 commit b4dfb95
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
saveNotificationSubscription
} from "../user";
import ConditionalTooltip from "../conditional-tooltip";
import { enqueueSnackbar } from "../notifier";

import css from "./styles.css";

Expand Down Expand Up @@ -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(() => {
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
17 changes: 17 additions & 0 deletions app/javascript/libs/service-worker-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit b4dfb95

Please sign in to comment.