woocommerce/plugins/woocommerce-admin/client/marketplace/utils/functions.tsx

94 lines
2.1 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
import { Product, ProductType } from '../components/product-list/types';
import {
MARKETPLACE_HOST,
MARKETPLACE_CATEGORY_API_PATH,
} from '../components/constants';
2023-08-09 13:04:35 +00:00
import { CategoryAPIItem } from '../components/category-selector/types';
import { LOCALE } from '../../utils/admin-settings';
interface ProductGroup {
2023-08-15 12:37:50 +00:00
id: string;
title: string;
items: Product[];
url: string;
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
itemType: ProductType;
}
// Fetch data for the discover page from the WooCommerce.com API
async function fetchDiscoverPageData(): Promise< ProductGroup[] > {
let url = '/wc/v3/marketplace/featured';
if ( LOCALE.userLocale ) {
url = `${ url }?locale=${ LOCALE.userLocale }`;
}
try {
return await apiFetch( { path: url.toString() } );
} catch ( error ) {
return [];
}
}
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
function fetchCategories( type: ProductType ): Promise< CategoryAPIItem[] > {
const url = new URL( MARKETPLACE_HOST + MARKETPLACE_CATEGORY_API_PATH );
if ( LOCALE.userLocale ) {
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
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' );
}
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 [];
} );
}
// Append UTM parameters to a URL, being aware of existing query parameters
const appendURLParams = (
url: string,
utmParams: Array< [ string, string ] >
): string => {
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
if ( ! url ) {
return url;
}
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,
appendURLParams,
};