Skip to content

Commit 325f52e

Browse files
authored
Add Error Toast (#33)
* Add Error Toast * Cleanup * Add Mouseover to Close after 1s
1 parent 5f6ffb1 commit 325f52e

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/css/popup.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ body {
1313
max-height: 44vh;
1414
background-color: #303030;
1515
}
16+
17+
#toast-container {
18+
z-index: 4;
19+
}

src/html/popup.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
</div>
2424

2525
<div class="container-fluid p-2">
26-
2726
<div class=" mb-1">
2827
<div class="form-check form-switch float-start form-select-lg me-2" style="padding-top: 1px; padding-bottom: 0;"
2928
data-bs-title="Disable/Enable Mouseover Preview." data-bs-placement="bottom" data-bs-toggle="tooltip" data-bs-trigger="hover">
@@ -70,6 +69,19 @@ <h3 class="my-0">Django Files</h3>
7069
</table>
7170
</div>
7271

72+
<div aria-live="polite" aria-atomic="true" class="">
73+
<div id="toast-container" class="toast-container bottom-0 end-0 p-3"></div>
74+
</div>
75+
76+
<div class="d-none">
77+
<div class="toast align-items-center border-0 mt-3" role="alert" aria-live="assertive" aria-atomic="true" data-bs-delay="5000">
78+
<div class="d-flex">
79+
<div class="toast-body small"></div>
80+
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
81+
</div>
82+
</div>
83+
</div>
84+
7385
<div class="modal fade" id="delete-modal" tabindex="-1" aria-hidden="true">
7486
<div class="modal-dialog modal-dialog-centered">
7587
<div class="modal-content">

src/js/popup.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ function updateTable(data) {
349349
*/
350350
async function deleteClick(event) {
351351
console.log('deleteClick:', event)
352-
const closest = event.target?.closest('tr').querySelector('.file-link')
352+
const closest = event.target?.closest('tr')?.querySelector('.file-link')
353353
const name = closest.dataset?.name
354354
console.log('name:', name)
355355
if (!name) {
@@ -373,16 +373,17 @@ async function deleteConfirm(event) {
373373
console.log('deleteConfirm:', event)
374374
const name = deleteName.textContent
375375
console.log(`Deleting File: ${name}`)
376-
// TODO: Catch Error...
376+
// TODO: Catch Error? Throw should happen during init...
377377
const response = await deleteFile(name)
378378
console.log('response:', response)
379379
if (response.ok) {
380380
mediaOuter.classList.add('d-none')
381381
deleteModal.hide()
382382
await initPopup()
383383
} else {
384-
// TODO: Handle Error...
385-
console.error('Error Deleting File: name, response:', name, response)
384+
console.error(`Error Deleting File: "${name}", response:`, response)
385+
showToast(`Error Deleting: <strong>${name}</strong>`, 'danger')
386+
deleteModal.hide()
386387
}
387388
}
388389

@@ -395,16 +396,34 @@ async function deleteConfirm(event) {
395396
async function deleteFile(name) {
396397
console.log(`deleteFile: ${name}`)
397398
const { options } = await chrome.storage.sync.get(['options'])
398-
console.log('options:', options)
399+
// console.log('options:', options)
399400
const headers = { Authorization: options.authToken }
400401
const opts = {
401402
method: 'DELETE',
402403
headers: headers,
403404
}
404405
const apiUrl = `${options.siteUrl}/api/delete/${name}`
405-
const response = await fetch(apiUrl, opts)
406-
console.log('response:', response)
407-
return response
406+
return await fetch(apiUrl, opts)
407+
}
408+
409+
/**
410+
* Show Bootstrap Toast
411+
* @function showToast
412+
* @param {String} message
413+
* @param {String} type
414+
*/
415+
function showToast(message, type = 'success') {
416+
console.log(`showToast: ${type}:`, message)
417+
const element = document.querySelector('.d-none .toast').cloneNode(true)
418+
element.classList.add(`text-bg-${type}`)
419+
element.querySelector('.toast-body').innerHTML = message
420+
document.getElementById('toast-container').appendChild(element)
421+
const toast = new bootstrap.Toast(element)
422+
toast.show()
423+
const callback = () => {
424+
element.addEventListener('mouseover', () => toast.hide())
425+
}
426+
setTimeout(callback, 1000)
408427
}
409428

410429
/**
@@ -466,8 +485,8 @@ function initPopupMouseover() {
466485
clearTimeout(timeoutID)
467486
}
468487
})
469-
mediaImage.addEventListener('error', (event) => {
470-
console.log('mediaError:', event)
488+
mediaImage.addEventListener('error', () => {
489+
// console.log('mediaError:', event)
471490
mediaImage.classList.add('d-none')
472491
mediaError.classList.remove('d-none')
473492
mediaImage.src = '../media/loading.gif'

0 commit comments

Comments
 (0)