2023-08-08 14:29:08 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-08-09 13:04:35 +00:00
|
|
|
import { Product } from '../components/product-list/types';
|
2023-08-08 14:29:08 +00:00
|
|
|
import { MARKETPLACE_URL } from '../components/constants';
|
2023-08-09 13:04:35 +00:00
|
|
|
import { CategoryAPIItem } from '../components/category-selector/types';
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch data for the discover page from the WooCommerce.com API
|
|
|
|
const fetchDiscoverPageData = async (): Promise< Array< ProductGroup > > => {
|
|
|
|
const fetchUrl = MARKETPLACE_URL + '/wp-json/wccom-extensions/2.0/featured';
|
|
|
|
|
|
|
|
return fetch( fetchUrl )
|
|
|
|
.then( ( response ) => {
|
|
|
|
if ( ! response.ok ) {
|
|
|
|
throw new Error( response.statusText );
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
} )
|
|
|
|
.then( ( json ) => {
|
|
|
|
return json;
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
return [];
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2023-08-09 13:04:35 +00:00
|
|
|
function fetchCategories(): Promise< CategoryAPIItem[] > {
|
|
|
|
return fetch( MARKETPLACE_URL + '/wp-json/wccom-extensions/1.0/categories' )
|
|
|
|
.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
|
|
|
|
const appendUTMParams = (
|
|
|
|
url: string,
|
|
|
|
utmParams: Array< [ string, string ] >
|
|
|
|
): string => {
|
|
|
|
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,
|
|
|
|
appendUTMParams,
|
|
|
|
};
|