Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Show 404 page when requested image id doesn't exist #50

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/layouts/error.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<div class="grid-container full">
<main class="not-found">
<h1 v-if="error.statusCode === 404">
{{ $t('not-found') }}
{{ $t('error.not-found') }}
</h1>
<h1 v-else>{{ $t('error-occurred') }}</h1>
<h1 v-else>{{ $t('error.occurred') }}</h1>
<p>{{ error.message }}</p>
</main>
</div>
</template>
Expand Down
7 changes: 5 additions & 2 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@
"other": "Other Collections",
"collection-size": "Collection size: {count} images"
},
"not-found": "Page Not Found",
"error-occurred": "An error occurred",
"error": {
"not-found": "Page Not Found",
"occurred": "An error occurred",
"image-not-found": "Couldn't find image with id {id}"
},
"search-tab": {
"image": "Image",
"audio": "Audio",
Expand Down
32 changes: 19 additions & 13 deletions src/pages/photos/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,30 @@ const PhotoDetailPage = {
async asyncData({ env, route }) {
return { thumbnailURL: `${env.apiUrl}thumbs/${route.params.id}` }
},
async fetch() {
async fetch({ store, route, error, app }) {
// Clear related images if present
if (this.relatedImages && this.relatedImages.length > 0) {
this.SET_RELATED_IMAGES({ relatedImages: [], relatedImageCount: 0 })
if (store.state.relatedImages && store.state.relatedImages.length > 0) {
await store.dispatch(SET_RELATED_IMAGES, {
relatedImages: [],
relatedImageCount: 0,
})
}
try {
// Load the image + related images in parallel
await Promise.all([
store.dispatch(FETCH_IMAGE, { id: route.params.id }),
store.dispatch(FETCH_RELATED_IMAGES, { id: route.params.id }),
])
} catch (err) {
error({
statusCode: 404,
message: app.i18n.t('error.image-not-found', { id: route.params.id }),
})
}

// Load the image + related images in parallel
await Promise.all([
this.loadImage(this.$route.params.id),
this[FETCH_RELATED_IMAGES]({ id: this.$route.params.id }),
])
},
beforeRouteEnter(to, from, nextPage) {
nextPage((_this) => {
if (from.path === '/search' || from.path === '/search/image') {
if (from.path === '/search/' || from.path === '/search/image') {
_this.shouldShowBreadcrumb = true
_this.breadCrumbURL = from.fullPath
}
Expand All @@ -112,9 +121,6 @@ const PhotoDetailPage = {
this[FETCH_RELATED_IMAGES]({ id: this.image.id })
}
},
loadImage(id) {
return this[FETCH_IMAGE]({ id })
},
},
}

Expand Down
3 changes: 1 addition & 2 deletions src/store-modules/search-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ const mutations = {
[SET_QUERY](_state, params) {
setQuery(_state, params)
},
// @todo: fix
[IMAGE_NOT_FOUND]() {
// redirect({ path: '/not-found' }, true)
throw new Error('Image not found')
},
}

Expand Down