Skip to content

Commit

Permalink
Merged in r2-2619-frontend-react-404-error (pull request #6514)
Browse files Browse the repository at this point in the history
R2-2619 - Front react to 404 errors

Approved-by: Joshua Toliver
  • Loading branch information
aespinoza-quoin authored and jtoliver-quoin committed Sep 22, 2023
2 parents 8a851b5 + 813c125 commit 85ec523
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_ATTEMPTS = 3;
let attempts = 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 (attempts % MAX_ATTEMPTS !== 0) {
postMessage({
type: POST_MESSAGES.SUBSCRIBE_NOTIFICATIONS
});
}

if (attempts % MAX_ATTEMPTS === 0) {
postMessage({
type: POST_MESSAGES.ATTEMPTS_SUBSCRIPTION_FAILED
});
}
attempts += 1;
}
}

async function subscribe() {
Expand Down

0 comments on commit 85ec523

Please sign in to comment.