Skip to content

Commit

Permalink
Fix the sources table crash
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <[email protected]>
  • Loading branch information
obulat committed Aug 7, 2024
1 parent 056dc7e commit 145e5b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
35 changes: 18 additions & 17 deletions frontend/src/components/VSourcesTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</tr>
</thead>
<tbody>
<tr v-for="provider in sortedProviders" :key="provider.display_name">
<tr v-for="provider in providers" :key="provider.display_name">
<td>
<VLink :href="providerViewUrl(provider)">{{
provider.display_name
Expand All @@ -60,7 +60,7 @@

<section role="region" class="mobile-source-table md:hidden">
<article
v-for="provider in sortedProviders"
v-for="provider in providers"
:key="provider.display_name"
:title="provider.display_name"
>
Expand All @@ -87,7 +87,7 @@
</template>

<script lang="ts">
import { computed, defineComponent, type PropType, reactive } from "vue"
import { defineComponent, type PropType, reactive, ref } from "vue"
import { useProviderStore } from "~/stores/provider"
import { useGetLocaleFormattedNumber } from "~/composables/use-get-locale-formatted-number"
Expand All @@ -113,20 +113,29 @@ export default defineComponent({
},
},
setup(props) {
const sorting = reactive({
direction: "asc",
field: "display_name" as keyof Omit<MediaProvider, "logo_url">,
})
const providerStore = useProviderStore()
const searchStore = useSearchStore()
const providers = ref<MediaProvider[]>(providerStore.providers[props.media])
function sortTable(field: keyof Omit<MediaProvider, "logo_url">) {
let direction = "asc"
let direction = field === "media_count" ? "desc" : "asc"
if (field === sorting.field) {
direction = sorting.direction === "asc" ? "desc" : "asc"
}
sorting.direction = direction
sorting.field = field
const sortedProviders = providers.value.sort(compareProviders)
providers.value =
direction === "asc" ? sortedProviders : sortedProviders.reverse()
}
const sorting = reactive({
direction: "asc",
field: "display_name" as keyof Omit<MediaProvider, "logo_url">,
})
function cleanSourceUrlForPresentation(url: string) {
const stripProtocol = (s: string) => s.replace(/https?:\/\//, "")
Expand All @@ -138,7 +147,6 @@ export default defineComponent({
}
const getLocaleFormattedNumber = useGetLocaleFormattedNumber()
const providerStore = useProviderStore()
function compareProviders(prov1: MediaProvider, prov2: MediaProvider) {
let field1 = prov1[sorting.field]
Expand All @@ -161,13 +169,6 @@ export default defineComponent({
return 0
}
const sortedProviders = computed<MediaProvider[]>(() => {
const providers = providerStore.providers[props.media]
providers.sort(compareProviders)
return sorting.direction === "asc" ? providers : providers.reverse()
})
const searchStore = useSearchStore()
const providerViewUrl = (provider: MediaProvider) => {
return searchStore.getCollectionPath({
type: props.media,
Expand All @@ -179,7 +180,7 @@ export default defineComponent({
}
return {
getLocaleFormattedNumber,
sortedProviders,
providers,
sorting,
sortTable,
cleanSourceUrlForPresentation,
Expand Down
14 changes: 14 additions & 0 deletions frontend/test/playwright/e2e/sources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ test("sources table has links to source pages", async ({ page }) => {

await expect(getH1(page, "Flickr")).toBeVisible()
})

// Tests the fix for https://github.com/WordPress/openverse/issues/4724
test("sources table can be sorted", async ({ page }) => {
await preparePageForTests(page, "xl")
await page.goto("/sources")

const totalItems = page.getByRole("cell", { name: /total items/i }).first()
await totalItems.click()
await totalItems.click()
await totalItems.click()

const firstRow = page.getByRole("row", { name: "Flickr" })
await expect(firstRow).toBeVisible()
})

0 comments on commit 145e5b7

Please sign in to comment.