-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: guido <[email protected]>
- Loading branch information
Showing
14 changed files
with
225 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import EntitiesSearch from '@types'; | ||
import { OrderedSet } from 'immutable'; | ||
|
||
import { useEntityRecords } from './use-entity-records'; | ||
|
||
/** | ||
* Hook to obtain the `viewable` taxonomies only. | ||
* This is an api on top of `useEntityRecords` to facilitate the usage of the `viewable` taxonomies. | ||
* | ||
* @public | ||
*/ | ||
export function useQueryViewableTaxonomies(): EntitiesSearch.EntitiesRecords<EntitiesSearch.ViewableTaxonomy> { | ||
const entitiesRecords = useEntityRecords<EntitiesSearch.Taxonomy<'edit'>>( | ||
'root', | ||
'taxonomy', | ||
{ per_page: -1 } | ||
); | ||
|
||
const viewableTaxonomies = entitiesRecords | ||
.records() | ||
.filter( | ||
(taxonomy) => taxonomy.visibility.publicly_queryable | ||
) as OrderedSet<EntitiesSearch.ViewableTaxonomy>; | ||
|
||
return { | ||
...entitiesRecords, | ||
records: () => OrderedSet(viewableTaxonomies), | ||
}; | ||
} |
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
18 changes: 18 additions & 0 deletions
18
sources/php/src/Modules/E2e/resources/js/taxonomies-example-block/block.json
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"apiVersion": 3, | ||
"title": "Taxonomies Example Block", | ||
"name": "widoz-entities-search/taxonomies-example-block", | ||
"category": "uncategorized", | ||
"icon": "wordpress", | ||
"editorScript": "widoz-entities-search-e2e-taxonomies-example-block", | ||
"attributes": { | ||
"taxonomy": { | ||
"type": "string", | ||
"default": "category" | ||
}, | ||
"terms": { | ||
"type": "array", | ||
"default": [] | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
sources/php/src/Modules/E2e/resources/js/taxonomies-example-block/index.js
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const { wp, entitiesSearch, Immutable } = window; | ||
|
||
const { createElement } = wp.element; | ||
const { registerBlockType } = wp.blocks; | ||
const { useBlockProps } = wp.blockEditor; | ||
const { Spinner } = wp.components; | ||
|
||
const { | ||
searchEntities, | ||
PostTypeRadio, | ||
PostsToggle, | ||
Search, | ||
CompositePostsPostTypes, | ||
useQueryViewableTaxonomies, | ||
convertEntitiesToControlOptions, | ||
convertPostEntitiesToControlOptions, | ||
} = entitiesSearch; | ||
|
||
// TODO Check why the object form does not work. | ||
registerBlockType('widoz-entities-search/taxonomies-example-block', { | ||
apiVersion: 2, | ||
title: 'Taxonomies Example Block', | ||
category: 'uncategorized', | ||
icon: 'wordpress', | ||
editorScript: 'widoz-entities-search-e2e-taxonomies-example-block', | ||
edit: function Edit(props) { | ||
const blockProps = useBlockProps({ | ||
className: 'widoz-entities-search-e2e-taxonomies-example-block', | ||
}); | ||
|
||
const taxonomiesEntities = useQueryViewableTaxonomies(); | ||
|
||
if (taxonomiesEntities.isResolving()) { | ||
return Spinner(); | ||
} | ||
|
||
const TermTaxonomiesControllerElement = createElement( | ||
CompositePostsPostTypes, | ||
{ | ||
// TODO Wrap around a throttle or debounce function | ||
searchEntities: async ( | ||
phrase, | ||
entityName, | ||
queryArguments | ||
) => { | ||
const entities = await searchEntities( | ||
'term', | ||
entityName, | ||
phrase, | ||
queryArguments | ||
); | ||
return convertPostEntitiesToControlOptions( | ||
entities, | ||
'title', | ||
'id' | ||
); | ||
}, | ||
posts: { | ||
value: Immutable.Set(props.attributes.terms), | ||
onChange: (terms) => | ||
props.setAttributes({ terms: terms?.toArray() }), | ||
}, | ||
postType: { | ||
value: props.attributes.taxonomy, | ||
options: convertEntitiesToControlOptions( | ||
taxonomiesEntities.records() | ||
), | ||
onChange: (taxonomy) => | ||
props.setAttributes({ taxonomy }), | ||
}, | ||
}, | ||
(posts, type, search) => { | ||
return [ | ||
createElement(PostTypeRadio, { | ||
...type, | ||
key: 'taxonomy-radio', | ||
}), | ||
createElement(Search, { search, key: 'search' }), | ||
createElement(PostsToggle, { | ||
...posts, | ||
key: 'terms-toggle', | ||
}), | ||
]; | ||
} | ||
); | ||
|
||
return createElement( | ||
'div', | ||
blockProps, | ||
TermTaxonomiesControllerElement | ||
); | ||
}, | ||
save: () => null, | ||
}); | ||
}); |
Oops, something went wrong.