-
Notifications
You must be signed in to change notification settings - Fork 6
Get list of pattern categories via pattern category registry (Patterns view) #96
Changes from 9 commits
7f6872f
cb6c53a
99781cb
dd13f09
335caf5
03397fb
cdd08cd
00eee00
72f842c
d0c544b
0a22897
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,13 +21,14 @@ | |
*/ | ||
function get_app_state() { | ||
return array( | ||
'patterns' => \PatternManager\PatternDataHandlers\get_theme_patterns_with_editor_links(), | ||
'apiNonce' => wp_create_nonce( 'wp_rest' ), | ||
'apiEndpoints' => array( | ||
'patterns' => \PatternManager\PatternDataHandlers\get_theme_patterns_with_editor_links(), | ||
'patternCategories' => \PatternManager\ApiData\get_registered_pattern_categories(), | ||
'apiNonce' => wp_create_nonce( 'wp_rest' ), | ||
'apiEndpoints' => array( | ||
'deletePatternEndpoint' => get_rest_url( false, 'pattern-manager/v1/delete-pattern/' ), | ||
), | ||
'siteUrl' => get_bloginfo( 'url' ), | ||
'adminUrl' => admin_url(), | ||
'siteUrl' => get_bloginfo( 'url' ), | ||
'adminUrl' => admin_url(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This diff looks more extensive than it is due to linting. |
||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { useState, useRef } from '@wordpress/element'; | ||
import { useState } from '@wordpress/element'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes!!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An award whenever you remove 🎉 |
||
import { __ } from '@wordpress/i18n'; | ||
import { patternManager } from '../globals'; | ||
import getHeaders from '../utils/getHeaders'; | ||
|
@@ -20,5 +20,6 @@ export default function usePatterns( initialPatterns: Patterns ) { | |
return { | ||
data: patternsData, | ||
deletePattern, | ||
patternCategories: patternManager.patternCategories, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially, I was getting the list of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, it's simpler this way. |
||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,35 +4,42 @@ export type PatternManagerViews = 'theme_patterns' | 'pattern_editor'; | |
|
||
export type InitialContext = { | ||
apiEndpoints: InitialPatternManager[ 'apiEndpoints' ]; | ||
patternCategories: InitialPatternManager[ 'patternCategories' ]; | ||
patterns: ReturnType< typeof usePatterns >; | ||
siteUrl: InitialPatternManager[ 'siteUrl' ]; | ||
}; | ||
|
||
export type InitialPatternManager = { | ||
adminUrl: string; | ||
apiEndpoints: { | ||
deletePatternEndpoint: string; | ||
}; | ||
apiNonce: string; | ||
siteUrl: string; | ||
adminUrl: string; | ||
patternCategories: QueriedCategories; | ||
patterns: Patterns; | ||
siteUrl: string; | ||
}; | ||
|
||
export type Pattern = { | ||
content: string; | ||
editorLink: string; | ||
name: string; | ||
title: string; | ||
slug: string; | ||
description?: string; | ||
title: string; | ||
blockTypes?: string[]; | ||
categories?: string[]; | ||
description?: string; | ||
inserter?: boolean; | ||
keywords?: string[]; | ||
blockTypes?: string[]; | ||
postTypes?: string[]; | ||
inserter?: boolean; | ||
viewportWidth?: number; | ||
}; | ||
Comment on lines
12
to
36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This diff also looks more extensive than it is since I alphabetized the list of properties. |
||
|
||
export type Patterns = { | ||
[ key: string ]: Pattern; | ||
}; | ||
|
||
export type QueriedCategories = { | ||
label: string; | ||
name: string; | ||
}[]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,13 @@ import { __ } from '@wordpress/i18n'; | |
*/ | ||
import convertToUpperCase from './convertToUpperCase'; | ||
import sortAlphabetically from './sortAlphabetically'; | ||
import type { Patterns } from '../types'; | ||
import type { Patterns, QueriedCategories } from '../types'; | ||
|
||
/** Create a mapping of unique categories for a dropdown or other list. */ | ||
export default function getUniquePatternCategories( patterns: Patterns ) { | ||
export default function getUniquePatternCategories( | ||
patterns: Patterns, | ||
queriedCategories: QueriedCategories | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) { | ||
return [ | ||
// Keep all-patterns at top of list. | ||
{ | ||
|
@@ -34,12 +37,18 @@ export default function getUniquePatternCategories( patterns: Patterns ) { | |
]; | ||
}, [] ) | ||
// Map the array to expected object shape. | ||
.map( ( category: string ) => ( { | ||
label: convertToUpperCase( | ||
category.replace( /[-_]/g, ' ' ) | ||
), | ||
name: category, | ||
} ) ), | ||
.map( ( categoryName: string ) => { | ||
return { | ||
label: | ||
queriedCategories.find( | ||
( { name } ) => name === categoryName | ||
)?.label || | ||
convertToUpperCase( | ||
categoryName.replace( /[-_]/g, ' ' ) | ||
), | ||
name: categoryName, | ||
}; | ||
} ), | ||
], | ||
// Sort by name property. | ||
'name' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,15 @@ | |
* Internal dependencies | ||
*/ | ||
import getUniquePatternCategories from '../getUniquePatternCategories'; | ||
import type { Patterns } from '../../types'; | ||
import type { Patterns, QueriedCategories } from '../../types'; | ||
|
||
type UniqueCategories = QueriedCategories; | ||
|
||
describe( 'getUniquePatternCategories', () => { | ||
it.each< [ Patterns, { label: string; name: string }[] ] >( [ | ||
it.each< [ Patterns, QueriedCategories, UniqueCategories ] >( [ | ||
[ | ||
{}, | ||
[], | ||
[ | ||
{ | ||
label: 'All Patterns', | ||
|
@@ -30,6 +33,7 @@ describe( 'getUniquePatternCategories', () => { | |
content: 'Here is some content', | ||
}, | ||
}, | ||
[], | ||
[ | ||
{ | ||
label: 'All Patterns', | ||
|
@@ -53,13 +57,19 @@ describe( 'getUniquePatternCategories', () => { | |
content: 'Here is some content', | ||
}, | ||
}, | ||
[ | ||
{ | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
], | ||
[ | ||
{ | ||
label: 'All Patterns', | ||
name: 'all-patterns', | ||
}, | ||
{ | ||
label: 'Some category', | ||
label: 'Some Category', | ||
Comment on lines
-62
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, any word succeeding the first would have been lowercase. Now they are capitalized since the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea to get the labels from the back end. |
||
name: 'some-category', | ||
}, | ||
], | ||
|
@@ -88,13 +98,19 @@ describe( 'getUniquePatternCategories', () => { | |
categories: [ 'some-category' ], | ||
}, | ||
}, | ||
[ | ||
{ | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
], | ||
[ | ||
{ | ||
label: 'All Patterns', | ||
name: 'all-patterns', | ||
}, | ||
{ | ||
label: 'Some category', | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
], | ||
|
@@ -123,17 +139,27 @@ describe( 'getUniquePatternCategories', () => { | |
categories: [ 'some-category' ], | ||
}, | ||
}, | ||
[ | ||
{ | ||
label: 'Another Category', | ||
name: 'another-category', | ||
}, | ||
{ | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
], | ||
[ | ||
{ | ||
label: 'All Patterns', | ||
name: 'all-patterns', | ||
}, | ||
{ | ||
label: 'Another category', | ||
label: 'Another Category', | ||
name: 'another-category', | ||
}, | ||
{ | ||
label: 'Some category', | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
], | ||
|
@@ -161,13 +187,23 @@ describe( 'getUniquePatternCategories', () => { | |
content: 'Here is more content', | ||
}, | ||
}, | ||
[ | ||
{ | ||
label: 'Category to Skip', | ||
name: 'category-to-skip', | ||
}, | ||
{ | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
], | ||
[ | ||
{ | ||
label: 'All Patterns', | ||
name: 'all-patterns', | ||
}, | ||
{ | ||
label: 'Some category', | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
{ | ||
|
@@ -213,17 +249,35 @@ describe( 'getUniquePatternCategories', () => { | |
content: "Wow. That's a lot of content!", | ||
}, | ||
}, | ||
[ | ||
{ | ||
label: 'Another Category', | ||
name: 'another-category', | ||
}, | ||
{ | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
{ | ||
label: 'Third Category', | ||
name: 'third-category', | ||
}, | ||
{ | ||
label: 'Fourth Category', | ||
name: 'fourth-category', | ||
}, | ||
], | ||
[ | ||
{ | ||
label: 'All Patterns', | ||
name: 'all-patterns', | ||
}, | ||
{ | ||
label: 'Some category', | ||
label: 'Some Category', | ||
name: 'some-category', | ||
}, | ||
{ | ||
label: 'Third category', | ||
label: 'Third Category', | ||
name: 'third-category', | ||
}, | ||
{ | ||
|
@@ -234,10 +288,14 @@ describe( 'getUniquePatternCategories', () => { | |
], | ||
] )( | ||
'should return unique pattern categories', | ||
( patterns: Patterns, expected: { label: string; name: string }[] ) => { | ||
expect( getUniquePatternCategories( patterns ) ).toEqual( | ||
expected | ||
); | ||
( | ||
patterns: Patterns, | ||
queriedCategories, | ||
expected: { label: string; name: string }[] | ||
) => { | ||
expect( | ||
getUniquePatternCategories( patterns, queriedCategories ) | ||
).toEqual( expected ); | ||
} | ||
); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not common to do a REST request in PHP to the same site.
Could this simply do a similar thing the request does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great feedback! It is working well to just simply use that registry instead of creating a REST request — thank you for the sugggestion.
This is covered by d0c544b.