htmgo/framework/assets/js/htmxextensions/mutation-error.ts

24 lines
609 B
TypeScript
Raw Normal View History

import htmx from "htmx.org";
htmx.defineExtension("mutation-error", {
// @ts-ignore
onEvent: (name, evt) => {
if (!(evt instanceof CustomEvent)) {
return false;
}
if (name === "htmx:afterRequest") {
if (!evt.detail || !evt.detail.xhr) {
return;
}
const status = evt.detail.xhr.status;
if (status >= 400) {
2024-10-20 12:48:19 +00:00
document.querySelectorAll("*").forEach((element) => {
if (element.hasAttribute("hx-on::on-mutation-error")) {
htmx.trigger(element, "htmx:on-mutation-error", { status });
}
});
}
}
},
});