htmgo/assets/js/htmxextensions/mutation-error.ts
maddalax da1f2b47d6 working on landing page
allow additional scripts to be compiled
2024-09-13 10:47:18 -05:00

21 lines
539 B
TypeScript

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) {
htmx.findAll("[hx-on\\:\\:mutation-error]").forEach((element) => {
htmx.trigger(element, "htmx:mutation-error", { status });
});
}
}
},
});