2023-07-20 13:53:06 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-08-08 14:29:08 +00:00
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
2023-07-20 13:53:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-08-08 14:29:08 +00:00
|
|
|
import ProductList from '../product-list/product-list';
|
|
|
|
import { fetchDiscoverPageData, ProductGroup } from '../../utils/functions';
|
2023-08-17 08:54:51 +00:00
|
|
|
import ProductLoader from '../product-loader/product-loader';
|
2023-07-04 12:08:50 +00:00
|
|
|
import './discover.scss';
|
2023-07-20 13:53:06 +00:00
|
|
|
|
2023-08-08 14:29:08 +00:00
|
|
|
export default function Discover(): JSX.Element | null {
|
|
|
|
const [ productGroups, setProductGroups ] = useState<
|
|
|
|
Array< ProductGroup >
|
|
|
|
>( [] );
|
2023-08-17 08:54:51 +00:00
|
|
|
const [ isLoading, setIsLoading ] = useState( false );
|
2023-08-08 14:29:08 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
2023-08-17 08:54:51 +00:00
|
|
|
setIsLoading( true );
|
|
|
|
|
|
|
|
fetchDiscoverPageData()
|
|
|
|
.then( ( products: Array< ProductGroup > ) => {
|
|
|
|
setProductGroups( products );
|
|
|
|
} )
|
|
|
|
.finally( () => {
|
|
|
|
setIsLoading( false );
|
|
|
|
} );
|
2023-08-08 14:29:08 +00:00
|
|
|
}, [] );
|
|
|
|
|
2023-08-17 08:54:51 +00:00
|
|
|
if ( isLoading ) {
|
|
|
|
return <ProductLoader />;
|
2023-08-08 14:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const groupsList = productGroups.flatMap( ( group ) => group );
|
2023-07-20 13:53:06 +00:00
|
|
|
return (
|
2023-07-04 12:08:50 +00:00
|
|
|
<div className="woocommerce-marketplace__discover">
|
2023-08-08 14:29:08 +00:00
|
|
|
{ groupsList.map( ( groups ) => (
|
|
|
|
<ProductList
|
|
|
|
key={ groups.id }
|
|
|
|
title={ groups.title }
|
|
|
|
products={ groups.items }
|
|
|
|
groupURL={ groups.url }
|
|
|
|
/>
|
|
|
|
) ) }
|
2023-07-20 13:53:06 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|