Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the sources table crash #4725

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 }) => {
obulat marked this conversation as resolved.
Show resolved Hide resolved
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()
})
Loading