Use hyperscript for rendering notifications

This commit is contained in:
Peter Stockings
2023-07-26 14:23:57 +10:00
parent bc7c15144a
commit 95b89ef9e8
3 changed files with 11 additions and 39 deletions

View File

@@ -150,17 +150,12 @@
</div> </div>
<div class="absolute top-16 right-4 m-4"> <div class="absolute top-16 right-4 m-4">
<div class="bg-white rounded shadow-md w-64" id="notifications-container"> <div class="bg-white rounded shadow-md w-64" id="notifications-container">
</div> </div>
</div> </div>
<script type="text/javascript"> <template id="notification-template">
const displayNotification = (message) => { <div class="bg-white rounded shadow-md w-64" _="init wait 2s then remove me end on click remove me">
const notifications_container_el = document.getElementById('notifications-container');
const notificiation_html = `<div class="bg-white rounded shadow-md w-64">
<div class="flex items-center w-full max-w-xs p-4 mb-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800" <div class="flex items-center w-full max-w-xs p-4 mb-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800"
role="alert"> role="alert">
<div <div
@@ -187,28 +182,8 @@
</svg> </svg>
</button> </button>
</div> </div>
</div>` </div>
</template>
createAndRemoveElement(notificiation_html, notifications_container_el, 3);
}
const createAndRemoveElement = (html, parentElement, delay) => {
// Create new element
var newElement = document.createElement('div');
newElement.innerHTML = html;
// Add click handler that deletes the element
newElement.addEventListener('click', function () {
newElement.parentNode.removeChild(newElement);
});
// Add the new element to the DOM
parentElement.appendChild(newElement);
// Remove the new element after 'delay' seconds
setTimeout(() => newElement?.parentNode?.removeChild(newElement), delay * 1000); // Convert delay from seconds to milliseconds
}
</script>
<script src="/static/js/htmx.min.js"></script> <script src="/static/js/htmx.min.js"></script>
<script src="/static/js/hyperscript.min.js"></script> <script src="/static/js/hyperscript.min.js"></script>

View File

@@ -87,9 +87,8 @@
hx-post="{{ url_for('create_topset', person_id=workout['PersonId'], workout_id=workout['WorkoutId']) }}" hx-post="{{ url_for('create_topset', person_id=workout['PersonId'], workout_id=workout['WorkoutId']) }}"
hx-swap="beforeend" hx-target="#new-workout" _="on htmx:afterOnLoad if #no-workouts add .hidden to #no-workouts end hx-swap="beforeend" hx-target="#new-workout" _="on htmx:afterOnLoad if #no-workouts add .hidden to #no-workouts end
on htmx:afterRequest on htmx:afterRequest
js render #notification-template with (message: 'Topset added') then append it to #notifications-container
displayNotification('Topset added') then call _hyperscript.processNode(#notifications-container)
end
then reset() me"> then reset() me">
<div class="flex flex-wrap -mx-3 mb-2"> <div class="flex flex-wrap -mx-3 mb-2">

View File

@@ -61,9 +61,8 @@
<form class="w-full mt-3" hx-post="{{ url_for('create_person') }}" hx-swap="beforeend" hx-target="#new-person" <form class="w-full mt-3" hx-post="{{ url_for('create_person') }}" hx-swap="beforeend" hx-target="#new-person"
_="on htmx:afterRequest _="on htmx:afterRequest
js render #notification-template with (message: 'User added') then append it to #notifications-container
displayNotification('User added') then call _hyperscript.processNode(#notifications-container)
end
then reset() me"> then reset() me">
<div class="flex flex-wrap -mx-3 mb-2"> <div class="flex flex-wrap -mx-3 mb-2">
<div class="grow px-3"> <div class="grow px-3">
@@ -151,9 +150,8 @@
<form class="w-full mt-8" hx-post="{{ url_for('create_exercise') }}" hx-swap="beforeend" <form class="w-full mt-8" hx-post="{{ url_for('create_exercise') }}" hx-swap="beforeend"
hx-target="#new-exercise" _="on htmx:afterRequest hx-target="#new-exercise" _="on htmx:afterRequest
js render #notification-template with (message: 'Exercise added') then append it to #notifications-container
displayNotification('Exercise added') then call _hyperscript.processNode(#notifications-container)
end
then reset() me"> then reset() me">
<div class="flex flex-wrap -mx-3 mb-2"> <div class="flex flex-wrap -mx-3 mb-2">
<div class="grow px-3"> <div class="grow px-3">