Better toast behavior.

This commit is contained in:
Yaro Kasear 2025-10-07 13:38:13 -05:00
parent eff6da0a3b
commit 0dbf246bdb
2 changed files with 58 additions and 20 deletions

View file

@ -56,7 +56,7 @@
{% block postmain %} {% block postmain %}
{% endblock %} {% endblock %}
<div class="toast-container position-fixed bottom-0 end p-3" id="toastContainer"></div> <div class="toast-container position-fixed bottom-0 end-0 p-3" id="toastContainer"></div>
<footer class="bg-body-tertiary border border-bottom-0 position-fixed bottom-0 w-100 pb-1"> <footer class="bg-body-tertiary border border-bottom-0 position-fixed bottom-0 w-100 pb-1">
<small> <small>
@ -77,24 +77,28 @@
{% endblock %} {% endblock %}
<script> <script>
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
searchText = document.getElementById('searchText'); const searchText = document.getElementById('searchText');
searchButton = document.getElementById('searchButton'); const searchButton = document.getElementById('searchButton');
searchText.addEventListener('input', () => { if (searchText && searchButton) {
searchButton.disabled = searchText.value == ""; searchButton.disabled = (searchText.value === "");
});
searchText.addEventListener('keypress', (event) => { searchText.addEventListener('input', () => {
if (event.key === "Enter") { searchButton.disabled = searchText.value == "";
location.href = `{{ url_for('search.search') }}?q=${searchText.value}`; });
}
});
searchButton.addEventListener('click', () => { searchText.addEventListener('keypress', (event) => {
location.href = `{{ url_for('search.search') }}?q=${searchText.value}`; if (event.key === "Enter") {
}); location.href = `{{ url_for('search.search') }}?q=${encodeURIComponent(searchText.value)}`;
}
});
toastNumber = 0; searchButton.addEventListener('click', () => {
location.href = `{{ url_for('search.search') }}?q=${encodeURIComponent(searchText.value)}`;
});
}
window.toastNumber = 0;
window.toastMessage = function (message, type = 'info') { window.toastMessage = function (message, type = 'info') {
const container = document.getElementById('toastContainer'); const container = document.getElementById('toastContainer');
@ -114,9 +118,29 @@
toast.show(); toast.show();
}; };
window.queueToast = function (message, type = 'info') {
try {
const key = 'flashToasts';
const list = JSON.parse(sessionStorage.getItem(key) || '[]');
list.push({ message, type });
sessionStorage.setItem(key, JSON.stringify(list));
} catch {}
};
{% block script %} {% block script %}
{% endblock %} {% endblock %}
})
try {
const key = 'flashToasts';
const raw = sessionStorage.getItem(key);
if (!raw) return;
const list = JSON.parse(raw) || [];
sessionStorage.removeItem(key);
for (const t of list) {
window.toastMessage(t.message, t.type || 'info');
}
} catch {}
});
</script> </script>
</body> </body>

View file

@ -88,12 +88,26 @@
}); });
const reply = await res.json(); const reply = await res.json();
if (reply.status === 'success') { if (reply.status === 'success') {
toastMessage('This entry has been successfully saved!', 'success'); if (!hasId && reply.id) {
window.queueToast('Created successfully.', 'success');
window.location.href - `/entry/${model}/${reply.id}`;
return;
} else {
window.queueToast('Updated successfully.', 'success');
if (model === 'worklog') {
for (const id of collectDeletedIds()) {
const li = document.getElementById(`note-${id}`);
if (li) li.remove();
}
}
}
window.newDrafts = []; window.newDrafts = [];
window.deletedIds = []; window.deletedIds = [];
if (!hasId && reply.id) {
window.location.href = `/entry/${model}/${reply.id}`; window.location.replace(window.location.href);
} return;
} else { } else {
toastMessage(`Unable to save entry: ${reply.error}`, 'danger'); toastMessage(`Unable to save entry: ${reply.error}`, 'danger');
} }