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

Commit

Permalink
Load provider filters (without local file) (#1460)
Browse files Browse the repository at this point in the history
* Updates the provider stats through the search store instead of the provider store

* Call fetchMediaProviders in the middleware, update search filters on search.vue page

* Init filters in the correct order

* Add an e2e test for provider filters from URL
  • Loading branch information
obulat authored May 25, 2022
1 parent b3807a1 commit b54b834
Show file tree
Hide file tree
Showing 10 changed files with 32,906 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const middleware: Middleware = async ({ app, query, route, $pinia }) => {
}

/* Provider store */

const providerStore = useProviderStore($pinia)
await providerStore.fetchMediaProviders()

Expand Down
1 change: 1 addition & 0 deletions src/pages/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default defineComponent({
async asyncData({ route, $pinia }) {
const searchStore = useSearchStore($pinia)
const mediaStore = useMediaStore($pinia)
await searchStore.initProviderFilters()
searchStore.setSearchStateFromUrl({
path: route.path,
urlQuery: route.query,
Expand Down
17 changes: 9 additions & 8 deletions src/stores/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
initialFetchState,
updateFetchState,
} from '~/composables/use-fetch-state'
import { useSearchStore } from '~/stores/search'

export interface ProviderState {
providers: {
Expand Down Expand Up @@ -62,6 +61,11 @@ export const useProviderStore = defineStore('provider', {
}),

actions: {
async getProviders() {
await this.fetchMediaProviders()
return this.providers
},

_updateFetchState(
mediaType: SupportedMediaType,
action: 'end' | 'start',
Expand Down Expand Up @@ -103,8 +107,8 @@ export const useProviderStore = defineStore('provider', {
},

/**
* Fetches providers for a set media type, and initializes the provider filters
* by calling the search store `initProviderFilters` method.
* Fetches provider stats for a set media type.
* Does not update provider stats if there's an error.
*/
async fetchMediaTypeProviders(
mediaType: SupportedMediaType
Expand All @@ -114,11 +118,6 @@ export const useProviderStore = defineStore('provider', {
try {
const res = await providerServices[mediaType].getProviderStats()
sortedProviders = sortProviders(res.data)
const searchStore = useSearchStore()
searchStore.initProviderFilters({
mediaType,
providers: sortedProviders,
})
this._updateFetchState(mediaType, 'end')
} catch (error: unknown) {
let errorMessage = `There was an error fetching media providers for ${mediaType}`
Expand All @@ -129,6 +128,8 @@ export const useProviderStore = defineStore('provider', {
: `${errorMessage}: ${error?.message}`
}
warn(errorMessage)
// Fallback on existing providers if there was an error
sortedProviders = this.providers[mediaType]
this._updateFetchState(mediaType, 'end', errorMessage)
} finally {
this.providers[mediaType] = sortedProviders
Expand Down
16 changes: 15 additions & 1 deletion src/stores/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
IMAGE,
SearchType,
SupportedMediaType,
supportedMediaTypes,
SupportedSearchType,
supportedSearchTypes,
isAdditionalSearchType,
Expand All @@ -27,6 +28,7 @@ import {
mediaFilterKeys,
mediaUniqueFilterKeys,
} from '~/constants/filters'
import { useProviderStore } from '~/stores/provider'

import { useFeatureFlagStore } from '~/stores/feature-flag'

Expand Down Expand Up @@ -177,11 +179,23 @@ export const useSearchStore = defineStore('search', {
imageProviders: resetProviders(IMAGE),
}
},

async initProviderFilters() {
const providerStore = useProviderStore()
const providers = await providerStore.getProviders()

for (const mediaType of supportedMediaTypes) {
this.updateProviderFilters({
mediaType,
providers: providers[mediaType],
})
}
},
/**
* Merge providers from API response with the filters that came from the browse URL search query string
* and match the checked properties in the store.
*/
initProviderFilters({
updateProviderFilters({
mediaType,
providers,
}: {
Expand Down
16 changes: 16 additions & 0 deletions test/playwright/e2e/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,19 @@ test('new media request is sent when a filter is selected', async ({
const queryString = response.url().split('/images/')[1]
expect(queryString).toEqual('?q=cat&license=cc0')
})

for (const [searchType, source] of [
['audio', 'Freesound'],
['image', 'Flickr'],
]) {
test(`Provider filters are correctly initialized from the URL: ${source} - ${searchType}`, async ({
page,
}) => {
await page.goto(
`/search/${searchType}?q=birds&source=${source.toLowerCase()}`
)
await openFilters(page)

await assertCheckboxStatus(page, source, 'checked')
})
}
Loading

0 comments on commit b54b834

Please sign in to comment.