Skip to content

Commit 89e0133

Browse files
committed
Integrate SweetAlert2 for improved alerts and confirmations in HTMX functionality. Adjust alert handling across event triggers
1 parent 95527b2 commit 89e0133

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

internal/view/static/js/app.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { initSweetAlert2 } from './init-sweetalert2.js'
12
import { initNotyf } from './init-notyf.js'
23
import { initHTMX } from './init-htmx.js'
34
import { initAlpineComponents } from './init-alpine-components.js'
45
import { initHelpers } from './init-helpers.js'
56
import { initDashboardAsideScroll } from './dashboard-aside-scroll.js'
67

8+
initSweetAlert2()
79
initNotyf()
810
initHTMX()
911
initAlpineComponents()

internal/view/static/js/init-htmx.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ export function initHTMX () {
22
const triggers = {
33
ctm_alert: function (evt) {
44
const message = decodeURIComponent(evt.detail.value)
5-
alert(message)
5+
window.swalAlert(message)
66
},
77
ctm_alert_with_refresh: function (evt) {
88
const message = decodeURIComponent(evt.detail.value)
9-
alert(message)
10-
location.reload()
9+
window.swalAlert(message).then(() => {
10+
location.reload()
11+
})
1112
},
1213
ctm_alert_with_redirect: function (evt) {
1314
const payload = decodeURIComponent(evt.detail.value)
@@ -18,8 +19,9 @@ export function initHTMX () {
1819
const message = parts[0]
1920
const url = parts[1]
2021

21-
alert(message)
22-
location.href = url
22+
window.swalAlert(message).then(() => {
23+
location.href = url
24+
})
2325
},
2426
ctm_toast_success: function (evt) {
2527
const message = decodeURIComponent(evt.detail.value)
@@ -43,10 +45,18 @@ export function initHTMX () {
4345
document.addEventListener(key, triggers[key])
4446
}
4547

46-
/*
47-
This fixes this issue:
48-
https://stackoverflow.com/questions/73658449/htmx-request-not-firing-when-hx-attributes-are-added-dynamically-from-javascrip
49-
*/
48+
// Add trigger to use sweetalert2 for confirms
49+
document.addEventListener('htmx:confirm', function (e) {
50+
if (!e.detail.target.hasAttribute('hx-confirm')) return
51+
52+
e.preventDefault()
53+
window.swalConfirm(e.detail.question).then(function (result) {
54+
if (result.isConfirmed) e.detail.issueRequest(true)
55+
})
56+
})
57+
58+
// This fixes this issue:
59+
// https://stackoverflow.com/questions/73658449/htmx-request-not-firing-when-hx-attributes-are-added-dynamically-from-javascrip
5060
const observer = new MutationObserver((mutations) => {
5161
mutations.forEach((mutation) => {
5262
mutation.addedNodes.forEach((node) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export function initSweetAlert2 () {
2+
// Docs at https://sweetalert2.github.io/#configuration
3+
const defaultConfig = {
4+
icon: 'info',
5+
confirmButtonText: 'Okay',
6+
cancelButtonText: 'Cancel',
7+
customClass: {
8+
popup: 'rounded-box bg-base-100 text-base-content',
9+
confirmButton: 'btn btn-primary',
10+
denyButton: 'btn btn-warning',
11+
cancelButton: 'btn btn-error'
12+
}
13+
}
14+
15+
async function swalAlert (text) {
16+
return Swal.fire({
17+
...defaultConfig,
18+
title: text
19+
})
20+
}
21+
22+
async function swalConfirm (text) {
23+
return Swal.fire({
24+
...defaultConfig,
25+
icon: 'question',
26+
title: text,
27+
confirmButtonText: 'Confirm',
28+
showCancelButton: true
29+
})
30+
}
31+
32+
window.swalAlert = swalAlert
33+
window.swalConfirm = swalConfirm
34+
}

internal/view/web/layout/dashboard.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func Dashboard(params DashboardParams) gomponents.Node {
2929
html.Script(html.Src("/libs/htmx/htmx-2.0.1.min.js"), html.Defer()),
3030
html.Script(html.Src("/libs/alpinejs/alpinejs-3.14.1.min.js"), html.Defer()),
3131
html.Script(html.Src("/libs/theme-change/theme-change-2.0.2.min.js")),
32+
html.Script(html.Src("/libs/sweetalert2/sweetalert2-11.13.1.min.js")),
3233

3334
html.Link(html.Rel("stylesheet"), html.Href("/libs/notyf/notyf-3.10.0.min.css")),
3435
html.Script(html.Src("/libs/notyf/notyf-3.10.0.min.js")),

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
"standard": {
1515
"globals": [
1616
"localStorage",
17-
"alert",
1817
"location",
1918
"toaster",
2019
"Alpine",
2120
"Notyf",
2221
"htmx",
23-
"MutationObserver"
22+
"MutationObserver",
23+
"Swal"
2424
]
2525
}
2626
}

0 commit comments

Comments
 (0)