From 65e5eddbc287f07b87ee0792257ccdca58aa528c Mon Sep 17 00:00:00 2001 From: Jeff Ohrstrom Date: Thu, 31 Oct 2024 11:53:59 -0400 Subject: [PATCH] create an alert div if pollAndReplace fails (#3915) Create an alert div if pollAndReplace fails. --- apps/dashboard/app/javascript/turbo_shim.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/apps/dashboard/app/javascript/turbo_shim.js b/apps/dashboard/app/javascript/turbo_shim.js index 17494d2967..4c12a473ac 100644 --- a/apps/dashboard/app/javascript/turbo_shim.js +++ b/apps/dashboard/app/javascript/turbo_shim.js @@ -6,6 +6,7 @@ */ import { setInnerHTML } from './utils'; +import { alert } from './alert'; export function replaceHTML(id, html) { const ele = document.getElementById(id); @@ -24,7 +25,15 @@ export function replaceHTML(id, html) { export function pollAndReplace(url, delay, id, callback) { fetch(url, { headers: { Accept: "text/vnd.turbo-stream.html" } }) - .then(response => response.ok ? Promise.resolve(response) : Promise.reject(response.text())) + .then((response) => { + if(response.status == 200) { + return Promise.resolve(response); + } else if(response.status == 401) { + return Promise.reject("This page cannot update becase you are no longer authenticated. Please refresh the page to log back in.") + } else { + return Promise.reject(response.text()); + } + }) .then((r) => r.text()) .then((html) => replaceHTML(id, html)) .then(() => { @@ -34,7 +43,11 @@ export function pollAndReplace(url, delay, id, callback) { } }) .catch((err) => { - console.log('Cannot retrieve partial due to error:'); + if (typeof err == 'string') { + alert(err); + } else { + alert('This page has encountered an unexpected error. Please refresh the page.'); + } console.log(err); }); }