-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use query parameters for additional search views in Nuxt (#3866)
* Use query params for the frontend collections Signed-off-by: Olga Bulat <[email protected]> * Update link in sources table Signed-off-by: Olga Bulat <[email protected]> * Update tests and tapes Signed-off-by: Olga Bulat <[email protected]> * Simplify collection path and query building Signed-off-by: Olga Bulat <[email protected]> * Simplify collection middleware conditionals Signed-off-by: Olga Bulat <[email protected]> * Document why test assertions are commented out Signed-off-by: Olga Bulat <[email protected]> --------- Signed-off-by: Olga Bulat <[email protected]>
- Loading branch information
Showing
32 changed files
with
5,836 additions
and
3,310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,99 @@ | ||
import { useFeatureFlagStore } from "~/stores/feature-flag" | ||
import { useProviderStore } from "~/stores/provider" | ||
import { useSearchStore } from "~/stores/search" | ||
|
||
import type { Middleware } from "@nuxt/types" | ||
import { queryDictionaryToQueryParams } from "~/utils/search-query-transform" | ||
import { | ||
isSupportedMediaType, | ||
type SupportedMediaType, | ||
} from "~/constants/media" | ||
import type { | ||
CollectionParams, | ||
CreatorCollection, | ||
SourceCollection, | ||
TagCollection, | ||
} from "~/types/search" | ||
|
||
import type { Context, Middleware } from "@nuxt/types" | ||
import type { Dictionary } from "vue-router/types/router" | ||
|
||
const queryToCollectionParams = ( | ||
query: Dictionary<string | (string | null)[]> | ||
): CollectionParams | undefined => { | ||
query = queryDictionaryToQueryParams(query) | ||
if ("tag" in query) { | ||
return { | ||
collection: "tag", | ||
tag: query.tag, | ||
} as TagCollection | ||
} | ||
|
||
if ("creator" in query && "source" in query) { | ||
return { | ||
collection: "creator", | ||
creator: query.creator, | ||
source: query.source, | ||
} as CreatorCollection | ||
} | ||
|
||
if ("source" in query) { | ||
return { | ||
collection: "source", | ||
source: query.source, | ||
} as SourceCollection | ||
} | ||
return undefined | ||
} | ||
|
||
const routeNameToMediaType = ( | ||
route: Context["route"] | ||
): SupportedMediaType | null => { | ||
const firstPart = route.name?.split("-")[0] | ||
return firstPart && isSupportedMediaType(firstPart) ? firstPart : null | ||
} | ||
|
||
/** | ||
* Middleware for the collection routes. | ||
* Checks that the feature flag is enabled and that the route (name and query) is valid. | ||
* Extracts the collectionParams from the route and updates the search store. | ||
* If the source name does not exist in the provider store, it will throw a 404 error. | ||
*/ | ||
|
||
export const collectionMiddleware: Middleware = async ({ | ||
$pinia, | ||
error: nuxtError, | ||
route, | ||
}) => { | ||
if (!useFeatureFlagStore($pinia).isOn("additional_search_views")) { | ||
nuxtError({ | ||
statusCode: 404, | ||
message: "Additional search views are not enabled", | ||
}) | ||
return | ||
} | ||
|
||
const searchStore = useSearchStore($pinia) | ||
// Route name has the locale in it, e.g. `audio-collection__en` | ||
const mediaType = routeNameToMediaType(route) | ||
const collectionParams = queryToCollectionParams(route.query) | ||
|
||
if (mediaType === null || collectionParams === undefined) { | ||
nuxtError({ | ||
statusCode: 404, | ||
message: "Invalid collection route", | ||
}) | ||
return | ||
} | ||
|
||
if ("source" in collectionParams) { | ||
const providerStore = useProviderStore($pinia) | ||
if (!providerStore.isSourceNameValid(mediaType, collectionParams.source)) { | ||
nuxtError({ | ||
statusCode: 404, | ||
message: `Invalid source name ${collectionParams.source} for media type ${mediaType}`, | ||
}) | ||
} | ||
} | ||
|
||
searchStore.setCollectionState(collectionParams, mediaType) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.