2023-08-17 08:54:51 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
2023-08-08 14:29:08 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-10-04 16:59:34 +00:00
|
|
|
import { Product, ProductType } from '../components/product-list/types';
|
2023-09-04 14:45:24 +00:00
|
|
|
import {
|
|
|
|
MARKETPLACE_HOST,
|
2023-09-04 15:00:30 +00:00
|
|
|
MARKETPLACE_CATEGORY_API_PATH,
|
2023-09-04 14:45:24 +00:00
|
|
|
} from '../components/constants';
|
2023-08-09 13:04:35 +00:00
|
|
|
import { CategoryAPIItem } from '../components/category-selector/types';
|
2023-08-17 08:54:51 +00:00
|
|
|
import { LOCALE } from '../../utils/admin-settings';
|
2023-08-08 14:29:08 +00:00
|
|
|
|
|
|
|
interface ProductGroup {
|
2023-08-15 12:37:50 +00:00
|
|
|
id: string;
|
2023-08-08 14:29:08 +00:00
|
|
|
title: string;
|
|
|
|
items: Product[];
|
|
|
|
url: string;
|
2023-10-04 16:59:34 +00:00
|
|
|
itemType: ProductType;
|
2023-08-08 14:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch data for the discover page from the WooCommerce.com API
|
2023-08-17 08:54:51 +00:00
|
|
|
async function fetchDiscoverPageData(): Promise< ProductGroup[] > {
|
|
|
|
let url = '/wc/v3/marketplace/featured';
|
2023-08-08 14:29:08 +00:00
|
|
|
|
2023-08-17 08:54:51 +00:00
|
|
|
if ( LOCALE.userLocale ) {
|
|
|
|
url = `${ url }?locale=${ LOCALE.userLocale }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await apiFetch( { path: url.toString() } );
|
|
|
|
} catch ( error ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 14:29:08 +00:00
|
|
|
|
2023-10-04 16:59:34 +00:00
|
|
|
function fetchCategories( type: ProductType ): Promise< CategoryAPIItem[] > {
|
|
|
|
const url = new URL( MARKETPLACE_HOST + MARKETPLACE_CATEGORY_API_PATH );
|
2023-08-17 08:54:51 +00:00
|
|
|
|
|
|
|
if ( LOCALE.userLocale ) {
|
2023-10-04 16:59:34 +00:00
|
|
|
url.searchParams.set( 'locale', LOCALE.userLocale );
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't define parent for extensions since that is provided by default
|
|
|
|
// This is to ensure the old marketplace continues to work when this isn't defined
|
|
|
|
if ( type === ProductType.theme ) {
|
|
|
|
url.searchParams.set( 'parent', 'themes' );
|
2023-08-17 08:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fetch( url.toString() )
|
2023-08-09 13:04:35 +00:00
|
|
|
.then( ( response ) => {
|
|
|
|
if ( ! response.ok ) {
|
|
|
|
throw new Error( response.statusText );
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
} )
|
|
|
|
.then( ( json ) => {
|
|
|
|
return json;
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
return [];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-08-15 08:03:27 +00:00
|
|
|
// Append UTM parameters to a URL, being aware of existing query parameters
|
2023-09-18 08:10:56 +00:00
|
|
|
const appendURLParams = (
|
2023-08-15 08:03:27 +00:00
|
|
|
url: string,
|
|
|
|
utmParams: Array< [ string, string ] >
|
|
|
|
): string => {
|
2023-10-04 16:59:34 +00:00
|
|
|
if ( ! url ) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2023-08-15 08:03:27 +00:00
|
|
|
const urlObject = new URL( url );
|
|
|
|
if ( ! urlObject ) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
utmParams.forEach( ( [ key, value ] ) => {
|
|
|
|
urlObject.searchParams.set( key, value );
|
|
|
|
} );
|
|
|
|
return urlObject.toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
fetchDiscoverPageData,
|
|
|
|
fetchCategories,
|
|
|
|
ProductGroup,
|
2023-09-18 08:10:56 +00:00
|
|
|
appendURLParams,
|
2023-08-15 08:03:27 +00:00
|
|
|
};
|