Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create an alert div if pollAndReplace fails #3915

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}
Loading