This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Products block Attributes Filter Inspector Controls (#8583)
This PR is meant to improve the UI and UX behind the Attributes filter within the Inspector Controls of the “Products (Beta)“ block. Also included: * Refactor `useProductAttributes` hook * Move it into the shared hooks. * Fetch both terms AND attributes via the API (previously, we got the attributes from the settings, but we'd get partial objects compared to the API? Maybe a follow-up to this could be to check why the attributes stored in the settings are incomplete?) * Make sure the return values match the ones expected from search items. * Remove attribute-related types from PQ directory * Improve functionality of `SearchListControl` * Allow the search input to be a Token based input. * Allow for search input to search even collapsed properties. * Use core `CheckboxControl` instead of radio buttons for items having children (includes undeterminated state). * Enable removal of tokens from the input * Improve styles: * Refactor classnames for `SearchItem`. * Add more semantic classes. * Align count label and caret to the right. * Make caret switch direction on expanded. * `cursor: pointer` on collapsible items. * Indent children of collapsible items. * Correctly pass through class names to search item * Enable keyboard navigation for collapsible items * Add link to manage attributes * Change label inside the inspector controls * Make search list attached when token type * Implement more sophisticated behavior of parent checkbox * If indeterminate or unchecked, it will check all children. * If checked, it will uncheck all children. * Remove hardcoded `isSingle` from `expandableSearchListItem`
- Loading branch information
1 parent
cb8b1ed
commit dab5622
Showing
25 changed files
with
814 additions
and
749 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useEffect, useRef, useState } from '@wordpress/element'; | ||
import { getAttributes, getTerms } from '@woocommerce/editor-components/utils'; | ||
import { | ||
AttributeObject, | ||
AttributeTerm, | ||
AttributeWithTerms, | ||
} from '@woocommerce/types'; | ||
import { formatError } from '@woocommerce/base-utils'; | ||
|
||
export default function useProductAttributes( shouldLoadAttributes: boolean ) { | ||
const [ errorLoadingAttributes, setErrorLoadingAttributes ] = | ||
useState< Awaited< ReturnType< typeof formatError > > | null >( null ); | ||
const [ isLoadingAttributes, setIsLoadingAttributes ] = useState( false ); | ||
const [ productsAttributes, setProductsAttributes ] = useState< | ||
AttributeWithTerms[] | ||
>( [] ); | ||
const hasLoadedAttributes = useRef( false ); | ||
|
||
useEffect( () => { | ||
if ( | ||
! shouldLoadAttributes || | ||
isLoadingAttributes || | ||
hasLoadedAttributes.current | ||
) | ||
return; | ||
|
||
async function fetchAttributesWithTerms() { | ||
setIsLoadingAttributes( true ); | ||
|
||
try { | ||
const attributes: AttributeObject[] = await getAttributes(); | ||
const attributesWithTerms: AttributeWithTerms[] = []; | ||
|
||
for ( const attribute of attributes ) { | ||
const terms: AttributeTerm[] = await getTerms( | ||
attribute.id | ||
); | ||
|
||
attributesWithTerms.push( { | ||
...attribute, | ||
// Manually adding the parent id because of a Rest API bug | ||
// returning always `0` as parent. | ||
// see https://github.com/woocommerce/woocommerce-blocks/issues/8501 | ||
parent: 0, | ||
terms: terms.map( ( term ) => ( { | ||
...term, | ||
attr_slug: attribute.taxonomy, | ||
parent: attribute.id, | ||
} ) ), | ||
} ); | ||
} | ||
|
||
setProductsAttributes( attributesWithTerms ); | ||
hasLoadedAttributes.current = true; | ||
} catch ( e ) { | ||
if ( e instanceof Error ) { | ||
setErrorLoadingAttributes( await formatError( e ) ); | ||
} | ||
} finally { | ||
setIsLoadingAttributes( false ); | ||
} | ||
} | ||
|
||
fetchAttributesWithTerms(); | ||
|
||
return () => { | ||
hasLoadedAttributes.current = true; | ||
}; | ||
}, [ isLoadingAttributes, shouldLoadAttributes ] ); | ||
|
||
return { | ||
errorLoadingAttributes, | ||
isLoadingAttributes, | ||
productsAttributes, | ||
}; | ||
} |
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
Oops, something went wrong.