Skip to content

Fix actions fetching logic and loading state, prevent duplicate toasts #31124

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
default preventDuplicates to default, use single key, use it for htmx
  • Loading branch information
silverwind committed Jun 25, 2024
commit abfbb59e5608ffa6b974794dbb4ee4cdcb5790d9
2 changes: 1 addition & 1 deletion web_src/js/components/RepoActionView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ const sfc = {
]);
} catch (err) {
if (err instanceof TypeError) return; // avoid network error while unloading page
showErrorToast(err.message);
showErrorToast(err.message, {preventDuplicates: true});
// reset all step loading states, we can't easily tell which one failed at this point
for (let i = 0; i < this.currentJob.steps.length; i++) {
if (this.currentJobStepsStates[i].loading) {
Expand Down
8 changes: 6 additions & 2 deletions web_src/js/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ htmx.config.scrollIntoViewOnBoost = false;
// https://htmx.org/events/#htmx:sendError
document.body.addEventListener('htmx:sendError', (event) => {
// TODO: add translations
showErrorToast(`Network error when calling ${event.detail.requestConfig.path}`);
showErrorToast(`Network error when calling ${event.detail.requestConfig.path}`, {
preventDuplicates: true,
});
});

// https://htmx.org/events/#htmx:responseError
document.body.addEventListener('htmx:responseError', (event) => {
// TODO: add translations
showErrorToast(`Error ${event.detail.xhr.status} when calling ${event.detail.requestConfig.path}`);
showErrorToast(`Error ${event.detail.xhr.status} when calling ${event.detail.requestConfig.path}`, {
preventDuplicates: true,
});
});
8 changes: 4 additions & 4 deletions web_src/js/modules/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const levels = {
};

// See https://github.com/apvarun/toastify-js#api for options
function showToast(message, level, {gravity, position, duration, useHtmlBody, allowDuplicates, ...other} = {}) {
function showToast(message, level, {gravity, position, duration, useHtmlBody, preventDuplicates, ...other} = {}) {
const body = String(useHtmlBody ? message : htmlEscape(message));
const key = `${level}-${body}`;

// prevent showing duplicate toasts with same level and message
if (!allowDuplicates && document.querySelector(`.toastify[data-body="${CSS.escape(body)}"][data-level="${CSS.escape(level)}"]`)) {
if (preventDuplicates && document.querySelector(`.toastify[data-key="${CSS.escape(key)}"]`)) {
return;
}

Expand All @@ -45,8 +46,7 @@ function showToast(message, level, {gravity, position, duration, useHtmlBody, al
});

toast.showToast();
toast.toastElement.setAttribute('data-body', body);
toast.toastElement.setAttribute('data-level', level);
toast.toastElement.setAttribute('data-key', key);
toast.toastElement.querySelector('.toast-close').addEventListener('click', () => toast.hideToast());
return toast;
}
Expand Down