Skip to content

Commit

Permalink
Add error message when loading images
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpereira committed Sep 7, 2023
1 parent 85030a1 commit 19a7f39
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/components/Gallery/GalleryMainImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
<ClientOnly>
<VSpinner v-if="isLoading" />
</ClientOnly>
<span
v-if="errorMessage"
v-text="errorMessage"
/>
<img
v-show="!errorMessage"
ref="imageElement"
class="max-h-80 h-max w-100 cursor-zoom-in m-auto object-contain"
:src="image.original"
Expand All @@ -26,17 +31,31 @@ const emit = defineEmits(['open:viewer'])
const isLoading = ref(false)
const imageElement = ref(null)
const errorMessage = ref(null)
watch(
() => props.image,
(newVal) => {
if (newVal.id) {
if (newVal.original) {
errorMessage.value = null
isLoading.value = true
}
}
)
function handleError(e) {
e.preventDefault()
isLoading.value = false
errorMessage.value = 'Image was not found or format is not supported'
}
function handleLoad() {
isLoading.value = false
}
onMounted(() => {
imageElement.value.addEventListener('load', () => (isLoading.value = false))
imageElement.value.addEventListener('load', handleLoad)
imageElement.value.addEventListener('error', handleError)
})
</script>
24 changes: 22 additions & 2 deletions src/components/ImageViewer/ImageViewer.global.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
class="absolute rounded-t-lg w-auto max-h-full h-auto top-12 bottom-44 left-0 right-0 flex justify-center align-middle"
>
<VSpinner v-if="isLoading" />
<div
class="flex flex-col justify-center"
v-if="errorMessage"
v-text="errorMessage"
/>
<img
v-show="!errorMessage"
ref="imageElement"
class="mx-auto cursor-zoom-out w-auto max-w-full max-h-full h-auto my-auto"
:alt="image.depictions.map((d) => d.label).join(';')"
Expand Down Expand Up @@ -125,12 +131,23 @@ const handleKeyboard = ({ key }) => {
const imageElement = ref(null)
const isLoading = ref(false)
const errorMessage = ref(null)
const image = computed(() => props.images[props.index])
document.addEventListener('keyup', handleKeyboard)
function handleError() {
isLoading.value = false
errorMessage.value = 'Image was not found or format is not supported'
}
function handleLoad() {
isLoading.value = false
}
onMounted(() => {
imageElement.value.addEventListener('load', () => (isLoading.value = false))
imageElement.value.addEventListener('load', handleLoad)
imageElement.value.addEventListener('error', handleError)
document.body.classList.add('overflow-hidden')
})
Expand All @@ -141,6 +158,9 @@ onUnmounted(() => {
watch(
() => props.index,
() => (isLoading.value = true)
() => {
errorMessage.value = null
isLoading.value = true
}
)
</script>

0 comments on commit 19a7f39

Please sign in to comment.