diff --git a/web/src/toast.ts b/web/src/toast.ts index 284713d..ab7c80c 100644 --- a/web/src/toast.ts +++ b/web/src/toast.ts @@ -138,14 +138,31 @@ const setupHTMXListeners = (): void => { const customEvent = evt as CustomEvent; - // Check if request failed - if (customEvent.detail.succeeded === false && customEvent.detail.xhr) { + if (customEvent.detail.xhr) { const xhr = customEvent.detail.xhr; - // Show toast for HTTP errors - if (xhr.status >= 400 && xhr.status < 600) { + if ( + customEvent.detail.succeeded === false && + xhr.status >= 400 && + xhr.status < 600 + ) { const errorMessage = parseXHRError(xhr); showToast(errorMessage, "error"); + } else if (xhr.status >= 200 && xhr.status < 300) { + const contentType = xhr.getResponseHeader("content-type"); + if (contentType && contentType.includes("application/json")) { + try { + const response = JSON.parse(xhr.responseText); + if (response.message) { + showToast(response.message, "success"); + if (customEvent.detail.target) { + customEvent.detail.target.innerHTML = ""; + } + } + } catch (e) { + // Not valid JSON — ignore + } + } } } });