forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Marketplace: add useGetRelatedPlugins hook (Automattic#80749)
* Marketplace: add useGetRelatedPlugins hook * Map the results to RelatedPlugin type * use apiVersion instead of apiNamespace This is needed to use wpcom-xhr-request in Rest API mode. See https://github.com/Automattic/wpcom-xhr-request/blob/3868e4371216c4d2c51366350f494f3ad59f73f2/index.js#L221 --------- Co-authored-by: Gergely Csécsey <gergely.csecsey@automattic.com>
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
useQuery, | ||
QueryKey, | ||
QueryFunction, | ||
UseQueryOptions, | ||
UseQueryResult, | ||
} from '@tanstack/react-query'; | ||
import { getPreinstalledPremiumPluginsVariations } from 'calypso/lib/plugins/utils'; | ||
import wpcom from 'calypso/lib/wp'; | ||
import { BASE_STALE_TIME } from 'calypso/state/initial-state'; | ||
import { ESRelatedPluginsResult, RelatedPlugin } from './types'; | ||
|
||
const mapESDataToReatedPluginData = ( results: Array< ESRelatedPluginsResult > = [] ) => { | ||
return results.map( ( result ) => { | ||
const esPlugin = result.fields; | ||
|
||
const relatedPlugin: RelatedPlugin = { | ||
categories: esPlugin.categories, | ||
excerpt: esPlugin.excerpt, | ||
icon: esPlugin.icon, | ||
marketplaceSlug: esPlugin.marketplace_slug, | ||
productSlug: esPlugin.product_slug, | ||
slug: esPlugin.slug, | ||
title: esPlugin.title, | ||
}; | ||
|
||
relatedPlugin.variations = getPreinstalledPremiumPluginsVariations( esPlugin ); | ||
|
||
return relatedPlugin; | ||
} ); | ||
}; | ||
|
||
const getRelatedPluginsQueryParams = ( | ||
pluginSlug: string, | ||
size: number | ||
): { queryKey: QueryKey; queryFn: QueryFunction } => { | ||
const queryKey: QueryKey = [ 'related-plugins', pluginSlug, size ]; | ||
const queryFn = () => { | ||
return wpcom.req | ||
.get( | ||
{ | ||
path: `/marketplace/${ pluginSlug }/related`, | ||
}, | ||
{ size, apiVersion: '1.3' } | ||
) | ||
.then( ( { data }: { data: Array< ESRelatedPluginsResult > } ) => | ||
mapESDataToReatedPluginData( data ) | ||
); | ||
}; | ||
return { queryKey, queryFn }; | ||
}; | ||
|
||
export const useGetRelatedPlugins = ( | ||
pluginSlug: string, | ||
size = 4, | ||
{ enabled = true, staleTime = BASE_STALE_TIME, refetchOnMount = true }: UseQueryOptions = {} | ||
): UseQueryResult => { | ||
return useQuery( { | ||
...getRelatedPluginsQueryParams( pluginSlug, size ), | ||
enabled: enabled, | ||
staleTime: staleTime, | ||
refetchOnMount: refetchOnMount, | ||
} ); | ||
}; |