Software: Apache. PHP/7.3.33 uname -a: Linux acloudg.aryanict.com 4.18.0-513.9.1.lve.el8.x86_64 #1 SMP Mon Dec 4 15:01:22 UTC uid=1095(katebhospital) gid=1098(katebhospital) groups=1098(katebhospital) Safe-mode: OFF (not secure) /var/softaculous/sitepad/editor/site-data/plugins/kkart-pro/packages/kkart-blocks/assets/js/hocs/ drwxr-xr-x |
Viewing file: Select action/file-type: /** * External dependencies */ import { useState, useEffect } from '@wordpress/element'; import { getAttributes, getTerms } from '@kkart/editor-components/utils'; /** * Internal dependencies */ import { formatError } from '../base/utils/errors.js'; /** * Get attribute data (name, taxonomy etc) from server data. * * @param {number} attributeId Attribute ID to look for. * @param {Array|null} attributeList List of attributes. * @param {string} matchField Field to match on. e.g. id or slug. */ const getAttributeData = ( attributeId, attributeList, matchField = 'id' ) => { return Array.isArray( attributeList ) ? attributeList.find( ( attr ) => attr[ matchField ] === attributeId ) : null; }; /** * HOC that calls the useAttributes hook. * * @param {Function} OriginalComponent Component being wrapped. */ const withAttributes = ( OriginalComponent ) => { return ( props ) => { const { selected = [] } = props; const selectedSlug = selected.length ? selected[ 0 ].attr_slug : null; const [ attributes, setAttributes ] = useState( null ); const [ expandedAttribute, setExpandedAttribute ] = useState( 0 ); const [ termsList, setTermsList ] = useState( {} ); const [ loading, setLoading ] = useState( true ); const [ termsLoading, setTermsLoading ] = useState( false ); const [ error, setError ] = useState( null ); useEffect( () => { if ( attributes === null ) { getAttributes() .then( ( newAttributes ) => { newAttributes = newAttributes.map( ( attribute ) => ( { ...attribute, parent: 0, } ) ); setAttributes( newAttributes ); if ( selectedSlug ) { const selectedAttributeFromTerm = getAttributeData( selectedSlug, newAttributes, 'taxonomy' ); if ( selectedAttributeFromTerm ) { setExpandedAttribute( selectedAttributeFromTerm.id ); } } } ) .catch( async ( e ) => { setError( await formatError( e ) ); } ) .finally( () => { setLoading( false ); } ); } }, [ attributes, selectedSlug ] ); useEffect( () => { const attributeData = getAttributeData( expandedAttribute, attributes ); if ( ! attributeData ) { return; } setTermsLoading( true ); getTerms( expandedAttribute ) .then( ( newTerms ) => { newTerms = newTerms.map( ( term ) => ( { ...term, parent: expandedAttribute, attr_slug: attributeData.taxonomy, } ) ); setTermsList( ( previousTermsList ) => ( { ...previousTermsList, [ expandedAttribute ]: newTerms, } ) ); } ) .catch( async ( e ) => { setError( await formatError( e ) ); } ) .finally( () => { setTermsLoading( false ); } ); }, [ expandedAttribute, attributes ] ); return ( <OriginalComponent { ...props } attributes={ attributes || [] } error={ error } expandedAttribute={ expandedAttribute } onExpandAttribute={ setExpandedAttribute } isLoading={ loading } termsAreLoading={ termsLoading } termsList={ termsList } /> ); }; }; export default withAttributes; |
:: Command execute :: | |
--[ c99shell v. 2.0 [PHP 7 Update] [25.02.2019] maintained by KaizenLouie | C99Shell Github | Generation time: 0.0037 ]-- |