Skip to content

Commit

Permalink
create an alert div if pollAndReplace fails (#3915)
Browse files Browse the repository at this point in the history
Create an alert div if pollAndReplace fails.
  • Loading branch information
johrstrom authored Oct 31, 2024
1 parent f0959ad commit 65e5edd
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions apps/dashboard/app/javascript/turbo_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { setInnerHTML } from './utils';
import { alert } from './alert';

export function replaceHTML(id, html) {
const ele = document.getElementById(id);
Expand All @@ -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(() => {
Expand All @@ -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);
});
}

0 comments on commit 65e5edd

Please sign in to comment.