-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pattern Library: Use new API endpoint in
usePatternCategories
(#88197)
* Use `/ptk/categories` API endpoint in `usePatternCategories` * Fix 404 page * Fix test * No fiddling with aspect-ratios in this PR * Address review suggestions * Fix import * Use `label` instead of `title` * More label
- Loading branch information
1 parent
52a957f
commit 642ef7a
Showing
10 changed files
with
117 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
import { PATTERN_CATEGORIES } from 'calypso/my-sites/patterns/hooks/use-pattern-categories'; | ||
|
||
export const RENDERER_SITE_ID = 226011606; // assemblerdemo | ||
|
||
export function getPatternCategorySlugs() { | ||
return PATTERN_CATEGORIES.join( '|' ); | ||
} |
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,75 +1,34 @@ | ||
import { useQuery, UseQueryOptions } from '@tanstack/react-query'; | ||
import wpcom from 'calypso/lib/wp'; | ||
import type { Category } from 'calypso/my-sites/patterns/types'; | ||
|
||
export const PATTERN_CATEGORIES = [ | ||
'featured', | ||
'intro', | ||
'about', | ||
// 'buttons', | ||
// 'banner', | ||
// 'query', | ||
'blog', | ||
'posts', | ||
// 'call-to-action', | ||
// 'columns', | ||
// 'coming-soon', | ||
'contact', | ||
'footer', | ||
'forms', | ||
'gallery', | ||
'header', | ||
// 'link-in-bio', | ||
// 'media', | ||
'newsletter', | ||
// 'podcast', | ||
'portfolio', // For page patterns only in v1 | ||
// 'quotes', | ||
'services', | ||
'store', | ||
// 'team', | ||
'testimonials', // Reused as "Quotes" | ||
// 'text', | ||
]; | ||
import type { Category, CategorySnakeCase } from 'calypso/my-sites/patterns/types'; | ||
|
||
export function getPatternCategoriesQueryOptions( | ||
locale: string, | ||
siteId: undefined | number, | ||
queryOptions: Omit< UseQueryOptions< Category[] >, 'queryKey' > = {} | ||
): UseQueryOptions< Category[] > { | ||
return { | ||
queryKey: [ locale, siteId, 'pattern-library', 'categories' ], | ||
queryKey: [ 'pattern-library', 'categories', locale ], | ||
queryFn() { | ||
return wpcom.req.get( | ||
`/sites/${ encodeURIComponent( siteId ?? '' ) }/block-patterns/categories`, | ||
{ | ||
apiNamespace: 'wp/v2', | ||
_locale: locale, | ||
} | ||
); | ||
}, | ||
select( categories ) { | ||
const result = []; | ||
|
||
for ( const name of PATTERN_CATEGORIES ) { | ||
const category = categories.find( ( category ) => category.name === name ); | ||
if ( category ) { | ||
result.push( category ); | ||
} | ||
} | ||
|
||
return result; | ||
return wpcom.req | ||
.get( `/ptk/categories/${ locale }` ) | ||
.then( ( categories: CategorySnakeCase[] ) => { | ||
return categories.map( | ||
( { regular_cattern_count, page_pattern_count, ...restCategory } ) => ( { | ||
...restCategory, | ||
pagePatternCount: page_pattern_count, | ||
regularPatternCount: regular_cattern_count, | ||
} ) | ||
); | ||
} ); | ||
}, | ||
staleTime: Infinity, | ||
...queryOptions, | ||
enabled: !! siteId, | ||
}; | ||
} | ||
|
||
export function usePatternCategories( | ||
locale: string, | ||
siteId: undefined | number, | ||
queryOptions: Omit< UseQueryOptions< Category[] >, 'queryKey' > = {} | ||
) { | ||
return useQuery< Category[] >( getPatternCategoriesQueryOptions( locale, siteId, queryOptions ) ); | ||
return useQuery< Category[] >( getPatternCategoriesQueryOptions( locale, queryOptions ) ); | ||
} |
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
Oops, something went wrong.