Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Fetch single image result in `asyncData` hook instead of `fetch` (#982)

Krystle Salazar67f0677eb662
diff --git a/src/components/VContentReport/VContentReportForm.vue b/src/components/VContentReport/VContentReportForm.vue
index 42e3fab5e..b345f099f 100644
--- a/src/components/VContentReport/VContentReportForm.vue
+++ b/src/components/VContentReport/VContentReportForm.vue
@@ -132,7 +132,12 @@ export default defineComponent({
VDmcaNotice,
VReportDescForm,
},
- props: ['media', 'providerName', 'reportService', 'closeFn'],
+ props: {
+ media: { required: true },
+ providerName: { required: true },
+ reportService: { required: false },
+ closeFn: { required: true },
+ },
setup(props) {
const service = props.reportService || ReportService
diff --git a/src/components/VContentReport/VContentReportPopover.vue b/src/components/VContentReport/VContentReportPopover.vue
index ecbbf1fa6..91edb4789 100644
--- a/src/components/VContentReport/VContentReportPopover.vue
+++ b/src/components/VContentReport/VContentReportPopover.vue
@@ -43,6 +43,7 @@ export default defineComponent({
*/
media: {
type: Object,
+ required: true,
},
},
setup(props) {
diff --git a/src/locales/po-files/openverse.pot b/src/locales/po-files/openverse.pot
index 6f290f6d2..2d108f600 100644
--- a/src/locales/po-files/openverse.pot
+++ b/src/locales/po-files/openverse.pot
@@ -4,7 +4,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Openverse \n"
"Report-Msgid-Bugs-To: https://github.com/wordpress/openverse/issues \n"
-"POT-Creation-Date: 2022-02-23T20:16:53+00:00\n"
+"POT-Creation-Date: 2022-02-24T14:41:46+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
@@ -1091,13 +1091,13 @@ msgid "An error occurred"
msgstr ""
#. Do not translate words between ### ###.
-#: src/pages/image/_id.vue:142
+#: src/pages/image/_id.vue:144
msgctxt "error.image-not-found"
msgid "Couldn't find image with id ###id###"
msgstr ""
#. Do not translate words between ### ###.
-#: src/pages/audio/_id.vue:69
+#: src/pages/audio/_id.vue:66
msgctxt "error.media-not-found"
msgid "Couldn't find ###mediaType### with id ###id###"
msgstr ""
diff --git a/src/pages/audio/_id.vue b/src/pages/audio/_id.vue
index ec9953c5e..25b2bb18b 100644
--- a/src/pages/audio/_id.vue
+++ b/src/pages/audio/_id.vue
@@ -34,9 +34,7 @@ const AudioDetailPage = {
mixins: [iframeHeight],
data() {
return {
- thumbnailURL: null,
showBackToSearchLink: false,
- id: null,
}
},
computed: {
@@ -53,14 +51,13 @@ const AudioDetailPage = {
this.id = newAudio.id
},
},
- async asyncData({ env, store, route, error, app }) {
+ async asyncData({ store, route, error, app }) {
try {
await store.dispatch(`${MEDIA}/${FETCH_MEDIA_ITEM}`, {
id: route.params.id,
mediaType: AUDIO,
})
return {
- thumbnailURL: `${env.apiUrl}audio/${route.params.id}/thumb/`,
id: route.params.id,
}
} catch (err) {
diff --git a/src/pages/audio/index.vue b/src/pages/audio/index.vue
deleted file mode 100644
index adc3a3d7b..000000000
--- a/src/pages/audio/index.vue
+++ /dev/null
@@ -1,11 +0,0 @@
-<template><VFourOhFour /></template>
-
-<script>
-import { defineComponent } from '@vue/composition-api'
-import VFourOhFour from '~/components/VFourOhFour.vue'
-
-export default defineComponent({
- components: { VFourOhFour },
- layout: 'blank',
-})
-</script>
diff --git a/src/pages/image/_id.vue b/src/pages/image/_id.vue
index 1c7058381..c957ee012 100644
--- a/src/pages/image/_id.vue
+++ b/src/pages/image/_id.vue
@@ -11,7 +11,7 @@
<img
v-if="!sketchFabUid"
id="main-image"
- :src="image.url"
+ :src="isLoadingFullImage ? image.thumbnail : image.url"
:alt="image.title"
class="h-full max-h-[500px] mx-auto rounded-t-sm"
@load="onImageLoaded"
@@ -111,11 +111,11 @@ const VImageDetailsPage = {
},
data() {
return {
- showBackToSearchLink: false,
imageWidth: 0,
imageHeight: 0,
imageType: 'Unknown',
- imageId: null,
+ isLoadingFullImage: true,
+ showBackToSearchLink: false,
sketchFabfailure: false,
}
},
@@ -130,19 +130,21 @@ const VImageDetailsPage = {
.split('/')[0]
},
},
- async asyncData({ route }) {
- return {
- imageId: route.params.id,
- }
- },
- async fetch() {
+ async asyncData({ app, error, route, store }) {
+ const imageId = route.params.id
try {
- await this.fetchItem({ id: this.imageId, mediaType: IMAGE })
+ await store.dispatch(`${MEDIA}/${FETCH_MEDIA_ITEM}`, {
+ id: imageId,
+ mediaType: IMAGE,
+ })
+ return {
+ imageId: imageId,
+ }
} catch (err) {
- const errorMessage = this.$t('error.image-not-found', {
- id: this.imageId,
+ const errorMessage = app.i18n.t('error.image-not-found', {
+ id: imageId,
})
- this.$nuxt.error({
+ error({
statusCode: 404,
message: errorMessage,
})
@@ -159,7 +161,6 @@ const VImageDetailsPage = {
})
},
methods: {
- ...mapActions(MEDIA, { fetchItem: FETCH_MEDIA_ITEM }),
...mapActions(USAGE_DATA, { sendEvent: SEND_DETAIL_PAGE_EVENT }),
onImageLoaded(event) {
this.imageWidth = this.image.width || event.target.naturalWidth
@@ -171,6 +172,7 @@ const VImageDetailsPage = {
this.imageType = res.headers['content-type']
})
}
+ this.isLoadingFullImage = false
},
onSourceLinkClicked() {
this.sendEvent({
diff --git a/src/pages/image/index.vue b/src/pages/image/index.vue
deleted file mode 100644
index adc3a3d7b..000000000
--- a/src/pages/image/index.vue
+++ /dev/null
@@ -1,11 +0,0 @@
-<template><VFourOhFour /></template>
-
-<script>
-import { defineComponent } from '@vue/composition-api'
-import VFourOhFour from '~/components/VFourOhFour.vue'
-
-export default defineComponent({
- components: { VFourOhFour },
- layout: 'blank',
-})
-</script>
diff --git a/test/e2e/image-detail.spec.js b/test/e2e/image-detail.spec.js
index a0a5886fe..9adfb0317 100644
--- a/test/e2e/image-detail.spec.js
+++ b/test/e2e/image-detail.spec.js
@@ -1,15 +1,24 @@
const { test, expect } = require('@playwright/test')
+const { mockImages } = require('./utils')
-test.beforeEach(async ({ context, page }) => {
- // Block any image request for each test in this file.
- await context.route(/\.(png|jpeg|jpg|svg)$/, (route) => route.abort())
-
+const goToCustomImagePage = async (page) => {
// Test in a custom image detail page, it should apply the same for any image.
// TODO: Make these tests independent of the live API.
await page.goto('image/e9d97a98-621b-4ec2-bf70-f47a74380452')
+}
+
+const showsErrorPage = async (page) => {
+ await expect(page.locator('h1')).toHaveText(
+ /The content you’re looking for seems to have disappeared/
+ )
+}
+
+test.beforeEach(async ({ context }) => {
+ await mockImages(context)
})
test('shows the author and title of the image', async ({ page }) => {
+ await goToCustomImagePage(page)
const author = page.locator('a[aria-label^="author"]')
await expect(author).toBeVisible()
const imgTitle = page.locator('h1')
@@ -17,6 +26,7 @@ test('shows the author and title of the image', async ({ page }) => {
})
test('shows the main image with its title as alt text', async ({ page }) => {
+ await goToCustomImagePage(page)
const imgTitle = await page.locator('h1').innerText()
const img = page.locator('id=main-image')
await expect(img).toBeVisible()
@@ -24,6 +34,7 @@ test('shows the main image with its title as alt text', async ({ page }) => {
})
test('does not show back to search results breadcrumb', async ({ page }) => {
+ await goToCustomImagePage(page)
await expect(page.locator('text="Back to search results"')).not.toBeVisible()
})
@@ -32,3 +43,13 @@ test('redirects from old /photos/:id route to /image/:id', async ({ page }) => {
await page.goto('photos/' + uuid)
await expect(page).toHaveURL('image/' + uuid)
})
+
+test('shows the 404 error page when no valid id', async ({ page }) => {
+ await page.goto('image/foo')
+ await showsErrorPage(page)
+})
+
+test('shows the 404 error page when no id', async ({ page }) => {
+ await page.goto('image/')
+ await showsErrorPage(page)
+})
diff --git a/test/e2e/utils.js b/test/e2e/utils.js
index 3a6e85650..55b306470 100644
--- a/test/e2e/utils.js
+++ b/test/e2e/utils.js
@@ -99,4 +99,5 @@ module.exports = {
assertCheckboxCheckedStatus,
mockAllSearch,
mockThumbnails,
+ mockImages,
}