This commit is contained in:
@@ -21,6 +21,11 @@ export const useGetCatalog = () => {
|
||||
});
|
||||
};
|
||||
|
||||
const catalogMongoIdPattern = /^[a-f\d]{24}$/i;
|
||||
|
||||
export const isCatalogMongoId = (value: string) =>
|
||||
catalogMongoIdPattern.test(value);
|
||||
|
||||
export const useGetCatalogById = (id: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["catalog", id],
|
||||
@@ -31,6 +36,15 @@ export const useGetCatalogById = (id: string) => {
|
||||
staleTime: 5 * 60 * 1000,
|
||||
});
|
||||
};
|
||||
|
||||
/** Route param may be Mongo id (editor/admin) or slug (public viewer-style URLs). */
|
||||
export const useGetCatalogByIdOrSlug = (param: string) => {
|
||||
const isId = isCatalogMongoId(param);
|
||||
const byId = useGetCatalogById(isId ? param : "");
|
||||
const bySlug = useGetCatalogBySlug(isId ? "" : param);
|
||||
return isId ? byId : bySlug;
|
||||
};
|
||||
|
||||
export const useGetCatalogBySlug = (slug: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["catalog", slug],
|
||||
|
||||
Reference in New Issue
Block a user